Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file
@ 2023-04-13 14:11 Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 02/25] fftools/ffmpeg_mux_init: restructure output stream creation Anton Khirnov
                   ` (23 more replies)
  0 siblings, 24 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:11 UTC (permalink / raw)
  To: ffmpeg-devel

Reduces the diff in the following commit.

Temporarily add a forward declaration for new_output_stream(), it will
be removed in the next commit.
---
 fftools/ffmpeg_mux_init.c | 481 +++++++++++++++++++-------------------
 1 file changed, 242 insertions(+), 239 deletions(-)

diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index 62e5643a04..69c1532b9e 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -419,245 +419,7 @@ static MuxStream *mux_stream_alloc(Muxer *mux, enum AVMediaType type)
 }
 
 static OutputStream *new_output_stream(Muxer *mux, const OptionsContext *o,
-                                       enum AVMediaType type, InputStream *ist)
-{
-    AVFormatContext *oc = mux->fc;
-    MuxStream     *ms;
-    OutputStream *ost;
-    const AVCodec *enc;
-    AVStream *st = avformat_new_stream(oc, NULL);
-    int ret = 0;
-    const char *bsfs = NULL, *time_base = NULL;
-    char *next, *codec_tag = NULL;
-    double qscale = -1;
-    int i;
-
-    if (!st)
-        report_and_exit(AVERROR(ENOMEM));
-
-    if (oc->nb_streams - 1 < o->nb_streamid_map)
-        st->id = o->streamid_map[oc->nb_streams - 1];
-
-    ms  = mux_stream_alloc(mux, type);
-    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->st         = st;
-    ost->ist        = ist;
-    ost->kf.ref_pts = AV_NOPTS_VALUE;
-    st->codecpar->codec_type = type;
-
-    ret = choose_encoder(o, oc, ost, &enc);
-    if (ret < 0) {
-        av_log(ost, AV_LOG_FATAL, "Error selecting an encoder\n");
-        exit_program(1);
-    }
-
-    if (enc) {
-        ost->enc_ctx = avcodec_alloc_context3(enc);
-        if (!ost->enc_ctx)
-            report_and_exit(AVERROR(ENOMEM));
-
-        ret = enc_alloc(&ost->enc, enc);
-        if (ret < 0)
-            report_and_exit(ret);
-
-        av_strlcat(ms->log_name, "/",       sizeof(ms->log_name));
-        av_strlcat(ms->log_name, enc->name, sizeof(ms->log_name));
-    } else {
-        av_strlcat(ms->log_name, "/copy", sizeof(ms->log_name));
-    }
-
-    ost->filtered_frame = av_frame_alloc();
-    if (!ost->filtered_frame)
-        report_and_exit(AVERROR(ENOMEM));
-
-    ost->pkt = av_packet_alloc();
-    if (!ost->pkt)
-        report_and_exit(AVERROR(ENOMEM));
-
-    if (ost->enc_ctx) {
-        AVCodecContext *enc = ost->enc_ctx;
-        AVIOContext *s = NULL;
-        char *buf = NULL, *arg = NULL, *preset = NULL;
-        const char *enc_stats_pre = NULL, *enc_stats_post = NULL, *mux_stats = NULL;
-
-        ost->encoder_opts = filter_codec_opts(o->g->codec_opts, enc->codec_id,
-                                              oc, st, enc->codec);
-
-        MATCH_PER_STREAM_OPT(presets, str, preset, oc, st);
-        ost->autoscale = 1;
-        MATCH_PER_STREAM_OPT(autoscale, i, ost->autoscale, oc, st);
-        if (preset && (!(ret = get_preset_file_2(preset, enc->codec->name, &s)))) {
-            AVBPrint bprint;
-            av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
-            do  {
-                av_bprint_clear(&bprint);
-                buf = get_line(s, &bprint);
-                if (!buf[0] || buf[0] == '#')
-                    continue;
-                if (!(arg = strchr(buf, '='))) {
-                    av_log(ost, AV_LOG_FATAL, "Invalid line found in the preset file.\n");
-                    exit_program(1);
-                }
-                *arg++ = 0;
-                av_dict_set(&ost->encoder_opts, buf, arg, AV_DICT_DONT_OVERWRITE);
-            } while (!s->eof_reached);
-            av_bprint_finalize(&bprint, NULL);
-            avio_closep(&s);
-        }
-        if (ret) {
-            av_log(ost, AV_LOG_FATAL,
-                   "Preset %s specified, but could not be opened.\n", preset);
-            exit_program(1);
-        }
-
-        MATCH_PER_STREAM_OPT(enc_stats_pre, str, enc_stats_pre, oc, st);
-        if (enc_stats_pre &&
-            (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
-            const char *format = "{fidx} {sidx} {n} {t}";
-
-            MATCH_PER_STREAM_OPT(enc_stats_pre_fmt, str, format, oc, st);
-
-            ret = enc_stats_init(ost, &ost->enc_stats_pre, 1, enc_stats_pre, format);
-            if (ret < 0)
-                exit_program(1);
-        }
-
-        MATCH_PER_STREAM_OPT(enc_stats_post, str, enc_stats_post, oc, st);
-        if (enc_stats_post &&
-            (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
-            const char *format = "{fidx} {sidx} {n} {t}";
-
-            MATCH_PER_STREAM_OPT(enc_stats_post_fmt, str, format, oc, st);
-
-            ret = enc_stats_init(ost, &ost->enc_stats_post, 0, enc_stats_post, format);
-            if (ret < 0)
-                exit_program(1);
-        }
-
-        MATCH_PER_STREAM_OPT(mux_stats, str, mux_stats, oc, st);
-        if (mux_stats &&
-            (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
-            const char *format = "{fidx} {sidx} {n} {t}";
-
-            MATCH_PER_STREAM_OPT(mux_stats_fmt, str, format, oc, st);
-
-            ret = enc_stats_init(ost, &ms->stats, 0, mux_stats, format);
-            if (ret < 0)
-                exit_program(1);
-        }
-    } else {
-        ost->encoder_opts = filter_codec_opts(o->g->codec_opts, AV_CODEC_ID_NONE, oc, st, NULL);
-    }
-
-
-    if (o->bitexact) {
-        ost->bitexact        = 1;
-    } else if (ost->enc_ctx) {
-        ost->bitexact        = check_opt_bitexact(ost->enc_ctx, ost->encoder_opts, "flags",
-                                                  AV_CODEC_FLAG_BITEXACT);
-    }
-
-    MATCH_PER_STREAM_OPT(time_bases, str, time_base, oc, st);
-    if (time_base) {
-        AVRational q;
-        if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 ||
-            q.num <= 0 || q.den <= 0) {
-            av_log(ost, AV_LOG_FATAL, "Invalid time base: %s\n", time_base);
-            exit_program(1);
-        }
-        st->time_base = q;
-    }
-
-    MATCH_PER_STREAM_OPT(enc_time_bases, str, time_base, oc, st);
-    if (time_base) {
-        AVRational q;
-        if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 ||
-            q.den <= 0) {
-            av_log(ost, AV_LOG_FATAL, "Invalid time base: %s\n", time_base);
-            exit_program(1);
-        }
-        ost->enc_timebase = q;
-    }
-
-    ms->max_frames = INT64_MAX;
-    MATCH_PER_STREAM_OPT(max_frames, i64, ms->max_frames, oc, st);
-    for (i = 0; i<o->nb_max_frames; i++) {
-        char *p = o->max_frames[i].specifier;
-        if (!*p && type != AVMEDIA_TYPE_VIDEO) {
-            av_log(ost, AV_LOG_WARNING, "Applying unspecific -frames to non video streams, maybe you meant -vframes ?\n");
-            break;
-        }
-    }
-
-    ost->copy_prior_start = -1;
-    MATCH_PER_STREAM_OPT(copy_prior_start, i, ost->copy_prior_start, oc ,st);
-
-    MATCH_PER_STREAM_OPT(bitstream_filters, str, bsfs, oc, st);
-    if (bsfs && *bsfs) {
-        ret = av_bsf_list_parse_str(bsfs, &ms->bsf_ctx);
-        if (ret < 0) {
-            av_log(ost, AV_LOG_ERROR, "Error parsing bitstream filter sequence '%s': %s\n", bsfs, av_err2str(ret));
-            exit_program(1);
-        }
-    }
-
-    MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, oc, st);
-    if (codec_tag) {
-        uint32_t tag = strtol(codec_tag, &next, 0);
-        if (*next)
-            tag = AV_RL32(codec_tag);
-        ost->st->codecpar->codec_tag = tag;
-        if (ost->enc_ctx)
-            ost->enc_ctx->codec_tag = tag;
-    }
-
-    MATCH_PER_STREAM_OPT(qscale, dbl, qscale, oc, st);
-    if (ost->enc_ctx && qscale >= 0) {
-        ost->enc_ctx->flags |= AV_CODEC_FLAG_QSCALE;
-        ost->enc_ctx->global_quality = FF_QP2LAMBDA * qscale;
-    }
-
-    ms->max_muxing_queue_size = 128;
-    MATCH_PER_STREAM_OPT(max_muxing_queue_size, i, ms->max_muxing_queue_size, oc, st);
-
-    ms->muxing_queue_data_threshold = 50*1024*1024;
-    MATCH_PER_STREAM_OPT(muxing_queue_data_threshold, i, ms->muxing_queue_data_threshold, oc, st);
-
-    MATCH_PER_STREAM_OPT(bits_per_raw_sample, i, ost->bits_per_raw_sample,
-                         oc, st);
-
-    MATCH_PER_STREAM_OPT(fix_sub_duration_heartbeat, i, ost->fix_sub_duration_heartbeat,
-                         oc, st);
-
-    if (oc->oformat->flags & AVFMT_GLOBALHEADER && ost->enc_ctx)
-        ost->enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
-
-    av_dict_copy(&ost->sws_dict, o->g->sws_dict, 0);
-
-    av_dict_copy(&ost->swr_opts, o->g->swr_opts, 0);
-    if (ost->enc_ctx && av_get_exact_bits_per_sample(ost->enc_ctx->codec_id) == 24)
-        av_dict_set(&ost->swr_opts, "output_sample_bits", "24", 0);
-
-    if (ost->ist) {
-        ost->ist->discard = 0;
-        ost->ist->st->discard = ost->ist->user_set_discard;
-
-        if (!(ost->enc && (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)))
-            ist_output_add(ost->ist, ost);
-    }
-    ost->last_mux_dts = AV_NOPTS_VALUE;
-
-    MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i,
-                         ost->copy_initial_nonkeyframes, oc, st);
-
-    return ost;
-}
+                                       enum AVMediaType type, InputStream *ist);
 
 static char *get_ost_filters(const OptionsContext *o, AVFormatContext *oc,
                              OutputStream *ost)
@@ -1098,6 +860,247 @@ static OutputStream *new_subtitle_stream(Muxer *mux, const OptionsContext *o, In
     return ost;
 }
 
+static OutputStream *new_output_stream(Muxer *mux, const OptionsContext *o,
+                                       enum AVMediaType type, InputStream *ist)
+{
+    AVFormatContext *oc = mux->fc;
+    MuxStream     *ms;
+    OutputStream *ost;
+    const AVCodec *enc;
+    AVStream *st = avformat_new_stream(oc, NULL);
+    int ret = 0;
+    const char *bsfs = NULL, *time_base = NULL;
+    char *next, *codec_tag = NULL;
+    double qscale = -1;
+    int i;
+
+    if (!st)
+        report_and_exit(AVERROR(ENOMEM));
+
+    if (oc->nb_streams - 1 < o->nb_streamid_map)
+        st->id = o->streamid_map[oc->nb_streams - 1];
+
+    ms  = mux_stream_alloc(mux, type);
+    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->st         = st;
+    ost->ist        = ist;
+    ost->kf.ref_pts = AV_NOPTS_VALUE;
+    st->codecpar->codec_type = type;
+
+    ret = choose_encoder(o, oc, ost, &enc);
+    if (ret < 0) {
+        av_log(ost, AV_LOG_FATAL, "Error selecting an encoder\n");
+        exit_program(1);
+    }
+
+    if (enc) {
+        ost->enc_ctx = avcodec_alloc_context3(enc);
+        if (!ost->enc_ctx)
+            report_and_exit(AVERROR(ENOMEM));
+
+        ret = enc_alloc(&ost->enc, enc);
+        if (ret < 0)
+            report_and_exit(ret);
+
+        av_strlcat(ms->log_name, "/",       sizeof(ms->log_name));
+        av_strlcat(ms->log_name, enc->name, sizeof(ms->log_name));
+    } else {
+        av_strlcat(ms->log_name, "/copy", sizeof(ms->log_name));
+    }
+
+    ost->filtered_frame = av_frame_alloc();
+    if (!ost->filtered_frame)
+        report_and_exit(AVERROR(ENOMEM));
+
+    ost->pkt = av_packet_alloc();
+    if (!ost->pkt)
+        report_and_exit(AVERROR(ENOMEM));
+
+    if (ost->enc_ctx) {
+        AVCodecContext *enc = ost->enc_ctx;
+        AVIOContext *s = NULL;
+        char *buf = NULL, *arg = NULL, *preset = NULL;
+        const char *enc_stats_pre = NULL, *enc_stats_post = NULL, *mux_stats = NULL;
+
+        ost->encoder_opts = filter_codec_opts(o->g->codec_opts, enc->codec_id,
+                                              oc, st, enc->codec);
+
+        MATCH_PER_STREAM_OPT(presets, str, preset, oc, st);
+        ost->autoscale = 1;
+        MATCH_PER_STREAM_OPT(autoscale, i, ost->autoscale, oc, st);
+        if (preset && (!(ret = get_preset_file_2(preset, enc->codec->name, &s)))) {
+            AVBPrint bprint;
+            av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
+            do  {
+                av_bprint_clear(&bprint);
+                buf = get_line(s, &bprint);
+                if (!buf[0] || buf[0] == '#')
+                    continue;
+                if (!(arg = strchr(buf, '='))) {
+                    av_log(ost, AV_LOG_FATAL, "Invalid line found in the preset file.\n");
+                    exit_program(1);
+                }
+                *arg++ = 0;
+                av_dict_set(&ost->encoder_opts, buf, arg, AV_DICT_DONT_OVERWRITE);
+            } while (!s->eof_reached);
+            av_bprint_finalize(&bprint, NULL);
+            avio_closep(&s);
+        }
+        if (ret) {
+            av_log(ost, AV_LOG_FATAL,
+                   "Preset %s specified, but could not be opened.\n", preset);
+            exit_program(1);
+        }
+
+        MATCH_PER_STREAM_OPT(enc_stats_pre, str, enc_stats_pre, oc, st);
+        if (enc_stats_pre &&
+            (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
+            const char *format = "{fidx} {sidx} {n} {t}";
+
+            MATCH_PER_STREAM_OPT(enc_stats_pre_fmt, str, format, oc, st);
+
+            ret = enc_stats_init(ost, &ost->enc_stats_pre, 1, enc_stats_pre, format);
+            if (ret < 0)
+                exit_program(1);
+        }
+
+        MATCH_PER_STREAM_OPT(enc_stats_post, str, enc_stats_post, oc, st);
+        if (enc_stats_post &&
+            (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
+            const char *format = "{fidx} {sidx} {n} {t}";
+
+            MATCH_PER_STREAM_OPT(enc_stats_post_fmt, str, format, oc, st);
+
+            ret = enc_stats_init(ost, &ost->enc_stats_post, 0, enc_stats_post, format);
+            if (ret < 0)
+                exit_program(1);
+        }
+
+        MATCH_PER_STREAM_OPT(mux_stats, str, mux_stats, oc, st);
+        if (mux_stats &&
+            (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
+            const char *format = "{fidx} {sidx} {n} {t}";
+
+            MATCH_PER_STREAM_OPT(mux_stats_fmt, str, format, oc, st);
+
+            ret = enc_stats_init(ost, &ms->stats, 0, mux_stats, format);
+            if (ret < 0)
+                exit_program(1);
+        }
+    } else {
+        ost->encoder_opts = filter_codec_opts(o->g->codec_opts, AV_CODEC_ID_NONE, oc, st, NULL);
+    }
+
+
+    if (o->bitexact) {
+        ost->bitexact        = 1;
+    } else if (ost->enc_ctx) {
+        ost->bitexact        = check_opt_bitexact(ost->enc_ctx, ost->encoder_opts, "flags",
+                                                  AV_CODEC_FLAG_BITEXACT);
+    }
+
+    MATCH_PER_STREAM_OPT(time_bases, str, time_base, oc, st);
+    if (time_base) {
+        AVRational q;
+        if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 ||
+            q.num <= 0 || q.den <= 0) {
+            av_log(ost, AV_LOG_FATAL, "Invalid time base: %s\n", time_base);
+            exit_program(1);
+        }
+        st->time_base = q;
+    }
+
+    MATCH_PER_STREAM_OPT(enc_time_bases, str, time_base, oc, st);
+    if (time_base) {
+        AVRational q;
+        if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 ||
+            q.den <= 0) {
+            av_log(ost, AV_LOG_FATAL, "Invalid time base: %s\n", time_base);
+            exit_program(1);
+        }
+        ost->enc_timebase = q;
+    }
+
+    ms->max_frames = INT64_MAX;
+    MATCH_PER_STREAM_OPT(max_frames, i64, ms->max_frames, oc, st);
+    for (i = 0; i<o->nb_max_frames; i++) {
+        char *p = o->max_frames[i].specifier;
+        if (!*p && type != AVMEDIA_TYPE_VIDEO) {
+            av_log(ost, AV_LOG_WARNING, "Applying unspecific -frames to non video streams, maybe you meant -vframes ?\n");
+            break;
+        }
+    }
+
+    ost->copy_prior_start = -1;
+    MATCH_PER_STREAM_OPT(copy_prior_start, i, ost->copy_prior_start, oc ,st);
+
+    MATCH_PER_STREAM_OPT(bitstream_filters, str, bsfs, oc, st);
+    if (bsfs && *bsfs) {
+        ret = av_bsf_list_parse_str(bsfs, &ms->bsf_ctx);
+        if (ret < 0) {
+            av_log(ost, AV_LOG_ERROR, "Error parsing bitstream filter sequence '%s': %s\n", bsfs, av_err2str(ret));
+            exit_program(1);
+        }
+    }
+
+    MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, oc, st);
+    if (codec_tag) {
+        uint32_t tag = strtol(codec_tag, &next, 0);
+        if (*next)
+            tag = AV_RL32(codec_tag);
+        ost->st->codecpar->codec_tag = tag;
+        if (ost->enc_ctx)
+            ost->enc_ctx->codec_tag = tag;
+    }
+
+    MATCH_PER_STREAM_OPT(qscale, dbl, qscale, oc, st);
+    if (ost->enc_ctx && qscale >= 0) {
+        ost->enc_ctx->flags |= AV_CODEC_FLAG_QSCALE;
+        ost->enc_ctx->global_quality = FF_QP2LAMBDA * qscale;
+    }
+
+    ms->max_muxing_queue_size = 128;
+    MATCH_PER_STREAM_OPT(max_muxing_queue_size, i, ms->max_muxing_queue_size, oc, st);
+
+    ms->muxing_queue_data_threshold = 50*1024*1024;
+    MATCH_PER_STREAM_OPT(muxing_queue_data_threshold, i, ms->muxing_queue_data_threshold, oc, st);
+
+    MATCH_PER_STREAM_OPT(bits_per_raw_sample, i, ost->bits_per_raw_sample,
+                         oc, st);
+
+    MATCH_PER_STREAM_OPT(fix_sub_duration_heartbeat, i, ost->fix_sub_duration_heartbeat,
+                         oc, st);
+
+    if (oc->oformat->flags & AVFMT_GLOBALHEADER && ost->enc_ctx)
+        ost->enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
+
+    av_dict_copy(&ost->sws_dict, o->g->sws_dict, 0);
+
+    av_dict_copy(&ost->swr_opts, o->g->swr_opts, 0);
+    if (ost->enc_ctx && av_get_exact_bits_per_sample(ost->enc_ctx->codec_id) == 24)
+        av_dict_set(&ost->swr_opts, "output_sample_bits", "24", 0);
+
+    if (ost->ist) {
+        ost->ist->discard = 0;
+        ost->ist->st->discard = ost->ist->user_set_discard;
+
+        if (!(ost->enc && (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)))
+            ist_output_add(ost->ist, ost);
+    }
+    ost->last_mux_dts = AV_NOPTS_VALUE;
+
+    MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i,
+                         ost->copy_initial_nonkeyframes, oc, st);
+
+    return ost;
+}
+
 static void init_output_filter(OutputFilter *ofilter, const OptionsContext *o,
                                Muxer *mux)
 {
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 02/25] fftools/ffmpeg_mux_init: restructure output stream creation
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 03/25] fftools/ffmpeg: move init_output_stream_streamcopy() to ffmpeg_mux_init Anton Khirnov
                   ` (22 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

Creating a new output stream of a given type is currently done by
calling new_<type>_stream(), which all start by calling
new_output_stream() to allocate the stream and do common init, followed
by type-specific init.

Reverse this structure - the caller now calls the common function
ost_add() with the type as a parameter, which then calls the
type-specific function internally. This will allow adding common code
that runs after type-specific code in future commits.
---
 fftools/ffmpeg_mux_init.c | 89 +++++++++++++++------------------------
 1 file changed, 35 insertions(+), 54 deletions(-)

diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index 69c1532b9e..714315cfd0 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -418,9 +418,6 @@ static MuxStream *mux_stream_alloc(Muxer *mux, enum AVMediaType type)
     return ms;
 }
 
-static OutputStream *new_output_stream(Muxer *mux, const OptionsContext *o,
-                                       enum AVMediaType type, InputStream *ist);
-
 static char *get_ost_filters(const OptionsContext *o, AVFormatContext *oc,
                              OutputStream *ost)
 {
@@ -471,14 +468,13 @@ static void parse_matrix_coeffs(void *logctx, uint16_t *dest, const char *str)
     }
 }
 
-static OutputStream *new_video_stream(Muxer *mux, const OptionsContext *o, InputStream *ist)
+static void new_stream_video(Muxer *mux, const OptionsContext *o,
+                             OutputStream *ost)
 {
     AVFormatContext *oc = mux->fc;
     AVStream *st;
-    OutputStream *ost;
     char *frame_rate = NULL, *max_frame_rate = NULL, *frame_aspect_ratio = NULL;
 
-    ost = new_output_stream(mux, o, AVMEDIA_TYPE_VIDEO, ist);
     st  = ost->st;
 
     MATCH_PER_STREAM_OPT(frame_rates, str, frame_rate, oc, st);
@@ -703,17 +699,14 @@ static OutputStream *new_video_stream(Muxer *mux, const OptionsContext *o, Input
             exit_program(1);
     } else
         check_streamcopy_filters(o, oc, ost, AVMEDIA_TYPE_VIDEO);
-
-    return ost;
 }
 
-static OutputStream *new_audio_stream(Muxer *mux, const OptionsContext *o, InputStream *ist)
+static void new_stream_audio(Muxer *mux, const OptionsContext *o,
+                             OutputStream *ost)
 {
     AVFormatContext *oc = mux->fc;
     AVStream *st;
-    OutputStream *ost;
 
-    ost = new_output_stream(mux, o, AVMEDIA_TYPE_AUDIO, ist);
     st  = ost->st;
 
 
@@ -801,49 +794,37 @@ static OutputStream *new_audio_stream(Muxer *mux, const OptionsContext *o, Input
 #endif
     } else
         check_streamcopy_filters(o, oc, ost, AVMEDIA_TYPE_AUDIO);
-
-    return ost;
 }
 
-static OutputStream *new_data_stream(Muxer *mux, const OptionsContext *o, InputStream *ist)
+static void new_stream_data(Muxer *mux, const OptionsContext *o,
+                            OutputStream *ost)
 {
-    OutputStream *ost;
-
-    ost = new_output_stream(mux, o, AVMEDIA_TYPE_DATA, ist);
     if (ost->enc_ctx) {
         av_log(ost, AV_LOG_FATAL, "Data stream encoding not supported yet (only streamcopy)\n");
         exit_program(1);
     }
-
-    return ost;
 }
 
-static OutputStream *new_unknown_stream(Muxer *mux, const OptionsContext *o, InputStream *ist)
+static void new_stream_unknown(Muxer *mux, const OptionsContext *o,
+                               OutputStream *ost)
 {
-    OutputStream *ost;
-
-    ost = new_output_stream(mux, o, AVMEDIA_TYPE_UNKNOWN, ist);
     if (ost->enc_ctx) {
         av_log(ost, AV_LOG_FATAL, "Unknown stream encoding not supported yet (only streamcopy)\n");
         exit_program(1);
     }
-
-    return ost;
 }
 
-static OutputStream *new_attachment_stream(Muxer *mux, const OptionsContext *o, InputStream *ist)
+static void new_stream_attachment(Muxer *mux, const OptionsContext *o,
+                                  OutputStream *ost)
 {
-    OutputStream *ost = new_output_stream(mux, o, AVMEDIA_TYPE_ATTACHMENT, ist);
     ost->finished    = 1;
-    return ost;
 }
 
-static OutputStream *new_subtitle_stream(Muxer *mux, const OptionsContext *o, InputStream *ist)
+static void new_stream_subtitle(Muxer *mux, const OptionsContext *o,
+                                OutputStream *ost)
 {
     AVStream *st;
-    OutputStream *ost;
 
-    ost = new_output_stream(mux, o, AVMEDIA_TYPE_SUBTITLE, ist);
     st  = ost->st;
 
     if (ost->enc_ctx) {
@@ -856,12 +837,10 @@ static OutputStream *new_subtitle_stream(Muxer *mux, const OptionsContext *o, In
             exit_program(1);
         }
     }
-
-    return ost;
 }
 
-static OutputStream *new_output_stream(Muxer *mux, const OptionsContext *o,
-                                       enum AVMediaType type, InputStream *ist)
+static OutputStream *ost_add(Muxer *mux, const OptionsContext *o,
+                             enum AVMediaType type, InputStream *ist)
 {
     AVFormatContext *oc = mux->fc;
     MuxStream     *ms;
@@ -1098,6 +1077,15 @@ static OutputStream *new_output_stream(Muxer *mux, const OptionsContext *o,
     MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i,
                          ost->copy_initial_nonkeyframes, oc, st);
 
+    switch (type) {
+    case AVMEDIA_TYPE_VIDEO:      new_stream_video     (mux, o, ost); break;
+    case AVMEDIA_TYPE_AUDIO:      new_stream_audio     (mux, o, ost); break;
+    case AVMEDIA_TYPE_SUBTITLE:   new_stream_subtitle  (mux, o, ost); break;
+    case AVMEDIA_TYPE_DATA:       new_stream_data      (mux, o, ost); break;
+    case AVMEDIA_TYPE_ATTACHMENT: new_stream_attachment(mux, o, ost); break;
+    default:                      new_stream_unknown   (mux, o, ost); break;
+    }
+
     return ost;
 }
 
@@ -1107,8 +1095,8 @@ static void init_output_filter(OutputFilter *ofilter, const OptionsContext *o,
     OutputStream *ost;
 
     switch (ofilter->type) {
-    case AVMEDIA_TYPE_VIDEO: ost = new_video_stream(mux, o, NULL); break;
-    case AVMEDIA_TYPE_AUDIO: ost = new_audio_stream(mux, o, NULL); break;
+    case AVMEDIA_TYPE_VIDEO: ost = ost_add(mux, o, AVMEDIA_TYPE_VIDEO, NULL); break;
+    case AVMEDIA_TYPE_AUDIO: ost = ost_add(mux, o, AVMEDIA_TYPE_AUDIO, NULL); break;
     default:
         av_log(mux, AV_LOG_FATAL, "Only video and audio filters are supported "
                "currently.\n");
@@ -1190,7 +1178,7 @@ static void map_auto_video(Muxer *mux, const OptionsContext *o)
        }
     }
     if (best_ist)
-        new_video_stream(mux, o, best_ist);
+        ost_add(mux, o, AVMEDIA_TYPE_VIDEO, best_ist);
 }
 
 static void map_auto_audio(Muxer *mux, const OptionsContext *o)
@@ -1232,7 +1220,7 @@ static void map_auto_audio(Muxer *mux, const OptionsContext *o)
        }
     }
     if (best_ist)
-        new_audio_stream(mux, o, best_ist);
+        ost_add(mux, o, AVMEDIA_TYPE_AUDIO, best_ist);
 }
 
 static void map_auto_subtitle(Muxer *mux, const OptionsContext *o)
@@ -1267,7 +1255,7 @@ static void map_auto_subtitle(Muxer *mux, const OptionsContext *o)
                 input_descriptor && output_descriptor &&
                 (!input_descriptor->props ||
                  !output_descriptor->props)) {
-                new_subtitle_stream(mux, o, ist);
+                ost_add(mux, o, AVMEDIA_TYPE_SUBTITLE, ist);
                 break;
             }
         }
@@ -1287,7 +1275,7 @@ static void map_auto_data(Muxer *mux, const OptionsContext *o)
             continue;
         if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA &&
             ist->st->codecpar->codec_id == codec_id )
-            new_data_stream(mux, o, ist);
+            ost_add(mux, o, AVMEDIA_TYPE_DATA, ist);
     }
 }
 
@@ -1336,18 +1324,8 @@ loop_end:
         if(o->    data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
             return;
 
-        switch (ist->st->codecpar->codec_type) {
-        case AVMEDIA_TYPE_VIDEO:      new_video_stream     (mux, o, ist); break;
-        case AVMEDIA_TYPE_AUDIO:      new_audio_stream     (mux, o, ist); break;
-        case AVMEDIA_TYPE_SUBTITLE:   new_subtitle_stream  (mux, o, ist); break;
-        case AVMEDIA_TYPE_DATA:       new_data_stream      (mux, o, ist); break;
-        case AVMEDIA_TYPE_ATTACHMENT: new_attachment_stream(mux, o, ist); break;
-        case AVMEDIA_TYPE_UNKNOWN:
-            if (copy_unknown_streams) {
-                new_unknown_stream   (mux, o, ist);
-                break;
-            }
-        default:
+        if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_UNKNOWN &&
+            !copy_unknown_streams) {
             av_log(mux, ignore_unknown_streams ? AV_LOG_WARNING : AV_LOG_FATAL,
                    "Cannot map stream #%d:%d - unsupported type.\n",
                    map->file_index, map->stream_index);
@@ -1358,7 +1336,10 @@ loop_end:
                        "If you want them copied, please use -copy_unknown\n");
                 exit_program(1);
             }
+            return;
         }
+
+        ost_add(mux, o, ist->st->codecpar->codec_type, ist);
     }
 }
 
@@ -1392,7 +1373,7 @@ static void of_add_attachments(Muxer *mux, const OptionsContext *o)
         avio_read(pb, attachment, len);
         memset(attachment + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
 
-        ost = new_attachment_stream(mux, o, NULL);
+        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;
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 03/25] fftools/ffmpeg: move init_output_stream_streamcopy() to ffmpeg_mux_init
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 02/25] fftools/ffmpeg_mux_init: restructure output stream creation Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 04/25] fftools/ffmpeg_mux_init: remove a redundant check Anton Khirnov
                   ` (21 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

Everything in it can be done immediately when creating the output
stream, there is no reason to postpone it.
---
 fftools/ffmpeg.c          | 136 ------------------------------------
 fftools/ffmpeg_mux_init.c | 143 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 143 insertions(+), 136 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 6e44542776..c1070485be 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1985,136 +1985,6 @@ static int init_input_stream(InputStream *ist, char *error, int error_len)
     return 0;
 }
 
-static int init_output_stream_streamcopy(OutputStream *ost)
-{
-    OutputFile *of = output_files[ost->file_index];
-    InputStream *ist = ost->ist;
-    InputFile *ifile = input_files[ist->file_index];
-    AVCodecParameters *par = ost->st->codecpar;
-    AVCodecContext *codec_ctx;
-    AVRational sar;
-    int i, ret;
-    uint32_t codec_tag = par->codec_tag;
-
-    av_assert0(ist && !ost->filter);
-
-    codec_ctx = avcodec_alloc_context3(NULL);
-    if (!codec_ctx)
-        return AVERROR(ENOMEM);
-
-    ret = avcodec_parameters_to_context(codec_ctx, ist->par);
-    if (ret >= 0)
-        ret = av_opt_set_dict(codec_ctx, &ost->encoder_opts);
-    if (ret < 0) {
-        av_log(ost, AV_LOG_FATAL,
-               "Error setting up codec context options.\n");
-        avcodec_free_context(&codec_ctx);
-        return ret;
-    }
-
-    ret = avcodec_parameters_from_context(par, codec_ctx);
-    avcodec_free_context(&codec_ctx);
-    if (ret < 0) {
-        av_log(ost, AV_LOG_FATAL,
-               "Error getting reference codec parameters.\n");
-        return ret;
-    }
-
-    if (!codec_tag) {
-        unsigned int codec_tag_tmp;
-        if (!of->format->codec_tag ||
-            av_codec_get_id (of->format->codec_tag, par->codec_tag) == par->codec_id ||
-            !av_codec_get_tag2(of->format->codec_tag, par->codec_id, &codec_tag_tmp))
-            codec_tag = par->codec_tag;
-    }
-
-    par->codec_tag = codec_tag;
-
-    if (!ost->frame_rate.num)
-        ost->frame_rate = ist->framerate;
-
-    if (ost->frame_rate.num)
-        ost->st->avg_frame_rate = ost->frame_rate;
-    else
-        ost->st->avg_frame_rate = ist->st->avg_frame_rate;
-
-    ret = avformat_transfer_internal_stream_timing_info(of->format, ost->st, ist->st, copy_tb);
-    if (ret < 0)
-        return ret;
-
-    // copy timebase while removing common factors
-    if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0) {
-        if (ost->frame_rate.num)
-            ost->st->time_base = av_inv_q(ost->frame_rate);
-        else
-            ost->st->time_base = av_add_q(av_stream_get_codec_timebase(ost->st), (AVRational){0, 1});
-    }
-
-    // copy estimated duration as a hint to the muxer
-    if (ost->st->duration <= 0 && ist->st->duration > 0)
-        ost->st->duration = av_rescale_q(ist->st->duration, ist->st->time_base, ost->st->time_base);
-
-    if (!ost->copy_prior_start) {
-        ost->ts_copy_start = (of->start_time == AV_NOPTS_VALUE) ?
-                             0 : of->start_time;
-        if (copy_ts && ifile->start_time != AV_NOPTS_VALUE) {
-            ost->ts_copy_start = FFMAX(ost->ts_copy_start,
-                                       ifile->start_time + ifile->ts_offset);
-        }
-    }
-
-    if (ist->st->nb_side_data) {
-        for (i = 0; i < ist->st->nb_side_data; i++) {
-            const AVPacketSideData *sd_src = &ist->st->side_data[i];
-            uint8_t *dst_data;
-
-            dst_data = av_stream_new_side_data(ost->st, sd_src->type, sd_src->size);
-            if (!dst_data)
-                return AVERROR(ENOMEM);
-            memcpy(dst_data, sd_src->data, sd_src->size);
-        }
-    }
-
-#if FFMPEG_ROTATION_METADATA
-    if (ost->rotate_overridden) {
-        uint8_t *sd = av_stream_new_side_data(ost->st, AV_PKT_DATA_DISPLAYMATRIX,
-                                              sizeof(int32_t) * 9);
-        if (sd)
-            av_display_rotation_set((int32_t *)sd, -ost->rotate_override_value);
-    }
-#endif
-
-    switch (par->codec_type) {
-    case AVMEDIA_TYPE_AUDIO:
-        if ((par->block_align == 1 || par->block_align == 1152 || par->block_align == 576) &&
-            par->codec_id == AV_CODEC_ID_MP3)
-            par->block_align = 0;
-        if (par->codec_id == AV_CODEC_ID_AC3)
-            par->block_align = 0;
-        break;
-    case AVMEDIA_TYPE_VIDEO:
-        if (ost->frame_aspect_ratio.num) { // overridden by the -aspect cli option
-            sar =
-                av_mul_q(ost->frame_aspect_ratio,
-                         (AVRational){ par->height, par->width });
-            av_log(ost, AV_LOG_WARNING, "Overriding aspect ratio "
-                   "with stream copy may produce invalid files\n");
-            }
-        else if (ist->st->sample_aspect_ratio.num)
-            sar = ist->st->sample_aspect_ratio;
-        else
-            sar = par->sample_aspect_ratio;
-        ost->st->sample_aspect_ratio = par->sample_aspect_ratio = sar;
-        ost->st->avg_frame_rate = ist->st->avg_frame_rate;
-        ost->st->r_frame_rate = ist->st->r_frame_rate;
-        break;
-    }
-
-    ost->mux_timebase = ist->st->time_base;
-
-    return 0;
-}
-
 static int init_output_stream_nofilter(OutputStream *ost)
 {
     int ret = 0;
@@ -2124,12 +1994,6 @@ static int init_output_stream_nofilter(OutputStream *ost)
         if (ret < 0)
             return ret;
     } else {
-        if (ost->ist) {
-            ret = init_output_stream_streamcopy(ost);
-            if (ret < 0)
-                return ret;
-        }
-
         ret = of_stream_init(output_files[ost->file_index], ost);
         if (ret < 0)
             return ret;
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index 714315cfd0..06a6b3fd2d 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -37,6 +37,7 @@
 #include "libavutil/avutil.h"
 #include "libavutil/bprint.h"
 #include "libavutil/dict.h"
+#include "libavutil/display.h"
 #include "libavutil/getenv_utf8.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
@@ -839,6 +840,142 @@ static void new_stream_subtitle(Muxer *mux, const OptionsContext *o,
     }
 }
 
+static int streamcopy_init(const Muxer *mux, const OptionsContext *o,
+                           OutputStream *ost)
+{
+    const InputStream   *ist        = ost->ist;
+    const InputFile     *ifile      = input_files[ist->file_index];
+
+    AVCodecParameters   *par        = ost->st->codecpar;
+    uint32_t             codec_tag  = par->codec_tag;
+
+    AVCodecContext      *codec_ctx  = NULL;
+    AVDictionary        *codec_opts = NULL;
+    int ret = 0;
+
+    codec_ctx = avcodec_alloc_context3(NULL);
+    if (!codec_ctx)
+        return AVERROR(ENOMEM);
+
+    ret = avcodec_parameters_to_context(codec_ctx, ist->par);
+    if (ret >= 0)
+        ret = av_opt_set_dict(codec_ctx, &ost->encoder_opts);
+    if (ret < 0) {
+        av_log(ost, AV_LOG_FATAL,
+               "Error setting up codec context options.\n");
+        goto fail;
+    }
+
+    ret = avcodec_parameters_from_context(par, codec_ctx);
+    if (ret < 0) {
+        av_log(ost, AV_LOG_FATAL,
+               "Error getting reference codec parameters.\n");
+        goto fail;
+    }
+
+    if (!codec_tag) {
+        const struct AVCodecTag * const *ct = mux->fc->oformat->codec_tag;
+        unsigned int codec_tag_tmp;
+        if (!ct || av_codec_get_id (ct, par->codec_tag) == par->codec_id ||
+            !av_codec_get_tag2(ct, par->codec_id, &codec_tag_tmp))
+            codec_tag = par->codec_tag;
+    }
+
+    par->codec_tag = codec_tag;
+
+    if (!ost->frame_rate.num)
+        ost->frame_rate = ist->framerate;
+
+    if (ost->frame_rate.num)
+        ost->st->avg_frame_rate = ost->frame_rate;
+    else
+        ost->st->avg_frame_rate = ist->st->avg_frame_rate;
+
+    ret = avformat_transfer_internal_stream_timing_info(mux->fc->oformat,
+                                                        ost->st, ist->st, copy_tb);
+    if (ret < 0)
+        goto fail;
+
+    // copy timebase while removing common factors
+    if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0) {
+        if (ost->frame_rate.num)
+            ost->st->time_base = av_inv_q(ost->frame_rate);
+        else
+            ost->st->time_base = av_add_q(av_stream_get_codec_timebase(ost->st), (AVRational){0, 1});
+    }
+
+    // copy estimated duration as a hint to the muxer
+    if (ost->st->duration <= 0 && ist->st->duration > 0)
+        ost->st->duration = av_rescale_q(ist->st->duration, ist->st->time_base, ost->st->time_base);
+
+    if (!ost->copy_prior_start) {
+        ost->ts_copy_start = (mux->of.start_time == AV_NOPTS_VALUE) ?
+                             0 : mux->of.start_time;
+        if (copy_ts && ifile->start_time != AV_NOPTS_VALUE) {
+            ost->ts_copy_start = FFMAX(ost->ts_copy_start,
+                                       ifile->start_time + ifile->ts_offset);
+        }
+    }
+
+    if (ist->st->nb_side_data) {
+        for (int i = 0; i < ist->st->nb_side_data; i++) {
+            const AVPacketSideData *sd_src = &ist->st->side_data[i];
+            uint8_t *dst_data;
+
+            dst_data = av_stream_new_side_data(ost->st, sd_src->type, sd_src->size);
+            if (!dst_data) {
+                ret = AVERROR(ENOMEM);
+                goto fail;
+            }
+            memcpy(dst_data, sd_src->data, sd_src->size);
+        }
+    }
+
+#if FFMPEG_ROTATION_METADATA
+    if (ost->rotate_overridden) {
+        uint8_t *sd = av_stream_new_side_data(ost->st, AV_PKT_DATA_DISPLAYMATRIX,
+                                              sizeof(int32_t) * 9);
+        if (sd)
+            av_display_rotation_set((int32_t *)sd, -ost->rotate_override_value);
+    }
+#endif
+
+    switch (par->codec_type) {
+    case AVMEDIA_TYPE_AUDIO:
+        if ((par->block_align == 1 || par->block_align == 1152 || par->block_align == 576) &&
+            par->codec_id == AV_CODEC_ID_MP3)
+            par->block_align = 0;
+        if (par->codec_id == AV_CODEC_ID_AC3)
+            par->block_align = 0;
+        break;
+    case AVMEDIA_TYPE_VIDEO: {
+        AVRational sar;
+        if (ost->frame_aspect_ratio.num) { // overridden by the -aspect cli option
+            sar =
+                av_mul_q(ost->frame_aspect_ratio,
+                         (AVRational){ par->height, par->width });
+            av_log(ost, AV_LOG_WARNING, "Overriding aspect ratio "
+                   "with stream copy may produce invalid files\n");
+            }
+        else if (ist->st->sample_aspect_ratio.num)
+            sar = ist->st->sample_aspect_ratio;
+        else
+            sar = par->sample_aspect_ratio;
+        ost->st->sample_aspect_ratio = par->sample_aspect_ratio = sar;
+        ost->st->avg_frame_rate = ist->st->avg_frame_rate;
+        ost->st->r_frame_rate = ist->st->r_frame_rate;
+        break;
+        }
+    }
+
+    ost->mux_timebase = ist->st->time_base;
+
+fail:
+    avcodec_free_context(&codec_ctx);
+    av_dict_free(&codec_opts);
+    return ret;
+}
+
 static OutputStream *ost_add(Muxer *mux, const OptionsContext *o,
                              enum AVMediaType type, InputStream *ist)
 {
@@ -1086,6 +1223,12 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o,
     default:                      new_stream_unknown   (mux, o, ost); break;
     }
 
+    if (ost->ist && !ost->enc) {
+        ret = streamcopy_init(mux, o, ost);
+        if (ret < 0)
+            exit_program(1);
+    }
+
     return ost;
 }
 
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 04/25] fftools/ffmpeg_mux_init: remove a redundant check
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 02/25] fftools/ffmpeg_mux_init: restructure output stream creation Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 03/25] fftools/ffmpeg: move init_output_stream_streamcopy() to ffmpeg_mux_init Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 05/25] fftools/ffmpeg: move a miplaced assignment Anton Khirnov
                   ` (20 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

---
 fftools/ffmpeg_mux_init.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index 06a6b3fd2d..e3d66b9897 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -917,18 +917,16 @@ static int streamcopy_init(const Muxer *mux, const OptionsContext *o,
         }
     }
 
-    if (ist->st->nb_side_data) {
-        for (int i = 0; i < ist->st->nb_side_data; i++) {
-            const AVPacketSideData *sd_src = &ist->st->side_data[i];
-            uint8_t *dst_data;
-
-            dst_data = av_stream_new_side_data(ost->st, sd_src->type, sd_src->size);
-            if (!dst_data) {
-                ret = AVERROR(ENOMEM);
-                goto fail;
-            }
-            memcpy(dst_data, sd_src->data, sd_src->size);
+    for (int i = 0; i < ist->st->nb_side_data; i++) {
+        const AVPacketSideData *sd_src = &ist->st->side_data[i];
+        uint8_t *dst_data;
+
+        dst_data = av_stream_new_side_data(ost->st, sd_src->type, sd_src->size);
+        if (!dst_data) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
         }
+        memcpy(dst_data, sd_src->data, sd_src->size);
     }
 
 #if FFMPEG_ROTATION_METADATA
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 05/25] fftools/ffmpeg: move a miplaced assignment
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (2 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 04/25] fftools/ffmpeg_mux_init: remove a redundant check Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 06/25] fftools/ffmpeg: move a check to a more appropriate place Anton Khirnov
                   ` (19 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

Changing AVCodecContext.sample_aspect_ratio after the encoder was opened
is by itself questionable, but if anywhere it belongs in encoding rather
than filtering code.
---
 fftools/ffmpeg.c     | 3 ---
 fftools/ffmpeg_enc.c | 3 +++
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index c1070485be..700fa5fbe1 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -711,9 +711,6 @@ static int reap_filters(int flush)
 
             switch (av_buffersink_get_type(filter)) {
             case AVMEDIA_TYPE_VIDEO:
-                if (!ost->frame_aspect_ratio.num)
-                    enc->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
-
                 enc_frame(ost, filtered_frame);
                 break;
             case AVMEDIA_TYPE_AUDIO:
diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index a0779c45ae..0236e50e38 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -653,6 +653,9 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
                    av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
                    enc->time_base.num, enc->time_base.den);
         }
+
+        if (frame->sample_aspect_ratio.num && !ost->frame_aspect_ratio.num)
+            enc->sample_aspect_ratio = frame->sample_aspect_ratio;
     }
 
     update_benchmark(NULL);
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 06/25] fftools/ffmpeg: move a check to a more appropriate place
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (3 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 05/25] fftools/ffmpeg: move a miplaced assignment Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 07/25] fftools/ffmpeg: drop unnecessary indirection Anton Khirnov
                   ` (18 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

reap_filters() no longer needs to access the encoding context.
---
 fftools/ffmpeg.c     | 21 +--------------------
 fftools/ffmpeg_enc.c |  7 +++++++
 2 files changed, 8 insertions(+), 20 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 700fa5fbe1..4d4cbf6aaf 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -669,7 +669,6 @@ static int reap_filters(int flush)
     /* Reap all buffers present in the buffer sinks */
     for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
         AVFilterContext *filter;
-        AVCodecContext *enc = ost->enc_ctx;
         int ret = 0;
 
         if (!ost->filter || !ost->filter->graph->graph)
@@ -709,25 +708,7 @@ static int reap_filters(int flush)
                            tb.num, tb.den);
             }
 
-            switch (av_buffersink_get_type(filter)) {
-            case AVMEDIA_TYPE_VIDEO:
-                enc_frame(ost, filtered_frame);
-                break;
-            case AVMEDIA_TYPE_AUDIO:
-                if (!(enc->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE) &&
-                    avcodec_is_open(enc) &&
-                    enc->ch_layout.nb_channels != filtered_frame->ch_layout.nb_channels) {
-                    av_log(NULL, AV_LOG_ERROR,
-                           "Audio filter graph output is not normalized and encoder does not support parameter changes\n");
-                    break;
-                }
-                enc_frame(ost, filtered_frame);
-                break;
-            default:
-                // TODO support subtitle filters
-                av_assert0(0);
-            }
-
+            enc_frame(ost, filtered_frame);
             av_frame_unref(filtered_frame);
         }
     }
diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index 0236e50e38..063053623f 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -789,6 +789,13 @@ static void do_audio_out(OutputFile *of, OutputStream *ost,
     AVCodecContext *enc = ost->enc_ctx;
     int ret;
 
+    if (!(enc->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE) &&
+        enc->ch_layout.nb_channels != frame->ch_layout.nb_channels) {
+        av_log(ost, AV_LOG_ERROR,
+               "Audio channel count changed and encoder does not support parameter changes\n");
+        return;
+    }
+
     if (frame->pts == AV_NOPTS_VALUE)
         frame->pts = e->next_pts;
     else {
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 07/25] fftools/ffmpeg: drop unnecessary indirection
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (4 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 06/25] fftools/ffmpeg: move a check to a more appropriate place Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 08/25] fftools/ffmpeg_mux_init: consolidate input stream flagging code Anton Khirnov
                   ` (17 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

init_input_stream() can print log messages directly, there is no need to
ship them to the caller.

Also, log errors to the InputStream and avoid duplicate information in
the message.
---
 fftools/ffmpeg.c | 26 +++++++++++---------------
 1 file changed, 11 insertions(+), 15 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 4d4cbf6aaf..57bdc12473 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1907,15 +1907,16 @@ static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat
     return *p;
 }
 
-static int init_input_stream(InputStream *ist, char *error, int error_len)
+static int init_input_stream(InputStream *ist)
 {
     int ret;
 
     if (ist->decoding_needed) {
         const AVCodec *codec = ist->dec;
         if (!codec) {
-            snprintf(error, error_len, "Decoder (codec %s) not found for input stream #%d:%d",
-                    avcodec_get_name(ist->dec_ctx->codec_id), ist->file_index, ist->st->index);
+            av_log(ist, AV_LOG_ERROR,
+                   "Decoding requested, but no decoder found for: %s\n",
+                    avcodec_get_name(ist->dec_ctx->codec_id));
             return AVERROR(EINVAL);
         }
 
@@ -1941,9 +1942,9 @@ static int init_input_stream(InputStream *ist, char *error, int error_len)
 
         ret = hw_device_setup_for_decode(ist);
         if (ret < 0) {
-            snprintf(error, error_len, "Device setup failed for "
-                     "decoder on input stream #%d:%d : %s",
-                     ist->file_index, ist->st->index, av_err2str(ret));
+            av_log(ist, AV_LOG_ERROR,
+                   "Hardware device setup failed for decoder: %s\n",
+                   av_err2str(ret));
             return ret;
         }
 
@@ -1951,10 +1952,8 @@ static int init_input_stream(InputStream *ist, char *error, int error_len)
             if (ret == AVERROR_EXPERIMENTAL)
                 abort_codec_experimental(codec, 0);
 
-            snprintf(error, error_len,
-                     "Error while opening decoder for input stream "
-                     "#%d:%d : %s",
-                     ist->file_index, ist->st->index, av_err2str(ret));
+            av_log(ist, AV_LOG_ERROR, "Error while opening decoder: %s\n",
+                   av_err2str(ret));
             return ret;
         }
         assert_avoptions(ist->decoder_opts);
@@ -1983,7 +1982,6 @@ static int init_output_stream_nofilter(OutputStream *ost)
 static int transcode_init(void)
 {
     int ret = 0;
-    char error[1024] = {0};
 
     /* init framerate emulation */
     for (int i = 0; i < nb_input_files; i++) {
@@ -1995,7 +1993,7 @@ static int transcode_init(void)
 
     /* init input streams */
     for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist))
-        if ((ret = init_input_stream(ist, error, sizeof(error))) < 0)
+        if ((ret = init_input_stream(ist)) < 0)
             goto dump_format;
 
     /*
@@ -2105,10 +2103,8 @@ static int transcode_init(void)
         av_log(NULL, AV_LOG_INFO, "\n");
     }
 
-    if (ret) {
-        av_log(NULL, AV_LOG_ERROR, "%s\n", error);
+    if (ret)
         return ret;
-    }
 
     atomic_store(&transcode_init_done, 1);
 
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 08/25] fftools/ffmpeg_mux_init: consolidate input stream flagging code
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (5 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 07/25] fftools/ffmpeg: drop unnecessary indirection Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 09/25] fftools/ffmpeg: add a function adding a destination filter for InputStream Anton Khirnov
                   ` (16 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

Makes it easier to see where the input stream is modified from muxer
code.
---
 fftools/ffmpeg_mux_init.c | 42 ++++++++++++++++++---------------------
 1 file changed, 19 insertions(+), 23 deletions(-)

diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index e3d66b9897..dc1dd834e0 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -1200,13 +1200,6 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o,
     if (ost->enc_ctx && av_get_exact_bits_per_sample(ost->enc_ctx->codec_id) == 24)
         av_dict_set(&ost->swr_opts, "output_sample_bits", "24", 0);
 
-    if (ost->ist) {
-        ost->ist->discard = 0;
-        ost->ist->st->discard = ost->ist->user_set_discard;
-
-        if (!(ost->enc && (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)))
-            ist_output_add(ost->ist, ost);
-    }
     ost->last_mux_dts = AV_NOPTS_VALUE;
 
     MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i,
@@ -1221,6 +1214,25 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o,
     default:                      new_stream_unknown   (mux, o, ost); break;
     }
 
+    if (ost->ist) {
+        ost->ist->discard = 0;
+        ost->ist->st->discard = ost->ist->user_set_discard;
+
+        if (ost->enc)
+            ost->ist->decoding_needed |= DECODING_FOR_OST;
+
+        if (ost->enc &&
+            (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
+            ret = init_simple_filtergraph(ost->ist, ost);
+            if (ret < 0) {
+                av_log(ost, AV_LOG_ERROR,
+                       "Error initializing a simple filtergraph\n");
+                exit_program(1);
+            }
+        } else
+            ist_output_add(ost->ist, ost);
+    }
+
     if (ost->ist && !ost->enc) {
         ret = streamcopy_init(mux, o, ost);
         if (ret < 0)
@@ -2407,25 +2419,9 @@ int of_open(const OptionsContext *o, const char *filename)
     /* check if all codec options have been used */
     validate_enc_avopt(mux, o->g->codec_opts);
 
-    /* set the decoding_needed flags and create simple filtergraphs */
     for (int i = 0; i < of->nb_streams; i++) {
         OutputStream *ost = of->streams[i];
 
-        if (ost->enc_ctx && ost->ist) {
-            InputStream *ist = ost->ist;
-            ist->decoding_needed |= DECODING_FOR_OST;
-
-            if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
-                ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
-                err = init_simple_filtergraph(ist, ost);
-                if (err < 0) {
-                    av_log(ost, AV_LOG_ERROR,
-                           "Error initializing a simple filtergraph\n");
-                    exit_program(1);
-                }
-            }
-        }
-
         /* set the filter output constraints */
         if (ost->filter) {
             const AVCodec *c = ost->enc_ctx->codec;
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 09/25] fftools/ffmpeg: add a function adding a destination filter for InputStream
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (6 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 08/25] fftools/ffmpeg_mux_init: consolidate input stream flagging code Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 10/25] fftools/ffmpeg: stop setting InputStream fields from muxing/filtering code Anton Khirnov
                   ` (15 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

This way filtering code does not directly mess with InputStream
internals. Will become more useful in following commits.
---
 fftools/ffmpeg.h        | 1 +
 fftools/ffmpeg_demux.c  | 6 ++++++
 fftools/ffmpeg_filter.c | 8 +++-----
 3 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 17076f018d..d25377514e 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -880,6 +880,7 @@ void ifile_close(InputFile **f);
 int ifile_get_packet(InputFile *f, AVPacket **pkt);
 
 void ist_output_add(InputStream *ist, OutputStream *ost);
+void ist_filter_add(InputStream *ist, InputFilter *ifilter);
 
 /* iterate over all input streams in all input files;
  * pass NULL to start iteration */
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index b9849d1669..2d46dbf876 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -567,6 +567,12 @@ void ist_output_add(InputStream *ist, OutputStream *ost)
     ist->outputs[ist->nb_outputs - 1] = ost;
 }
 
+void ist_filter_add(InputStream *ist, InputFilter *ifilter)
+{
+    GROW_ARRAY(ist->filters, ist->nb_filters);
+    ist->filters[ist->nb_filters - 1] = ifilter;
+}
+
 static const AVCodec *choose_decoder(const OptionsContext *o, AVFormatContext *s, AVStream *st,
                                      enum HWAccelID hwaccel_id, enum AVHWDeviceType hwaccel_device_type)
 
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 584f51ac3f..d2a185cf98 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -212,12 +212,11 @@ int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
     if (!ifilter->frame_queue)
         report_and_exit(AVERROR(ENOMEM));
 
-    GROW_ARRAY(ist->filters, ist->nb_filters);
-    ist->filters[ist->nb_filters - 1] = ifilter;
-
     GROW_ARRAY(filtergraphs, nb_filtergraphs);
     filtergraphs[nb_filtergraphs - 1] = fg;
 
+    ist_filter_add(ist, ifilter);
+
     return 0;
 }
 
@@ -319,8 +318,7 @@ static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
     if (!ifilter->frame_queue)
         report_and_exit(AVERROR(ENOMEM));
 
-    GROW_ARRAY(ist->filters, ist->nb_filters);
-    ist->filters[ist->nb_filters - 1] = ifilter;
+    ist_filter_add(ist, ifilter);
 }
 
 static int read_binary(const char *path, uint8_t **data, int *len)
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 10/25] fftools/ffmpeg: stop setting InputStream fields from muxing/filtering code
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (7 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 09/25] fftools/ffmpeg: add a function adding a destination filter for InputStream Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 11/25] fftools/ffmpeg: move opening decoders to a new file Anton Khirnov
                   ` (14 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

Set InputStream.decoding_needed/discard/etc. only from
ist_{filter,output},add() functions. Reduces the knowledge of
InputStream internals in muxing/filtering code.
---
 fftools/ffmpeg.h          |  2 +-
 fftools/ffmpeg_demux.c    | 13 ++++++++++++-
 fftools/ffmpeg_filter.c   |  8 ++------
 fftools/ffmpeg_mux_init.c |  6 ------
 4 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index d25377514e..c73fc8a459 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -880,7 +880,7 @@ void ifile_close(InputFile **f);
 int ifile_get_packet(InputFile *f, AVPacket **pkt);
 
 void ist_output_add(InputStream *ist, OutputStream *ost);
-void ist_filter_add(InputStream *ist, InputFilter *ifilter);
+void ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple);
 
 /* iterate over all input streams in all input files;
  * pass NULL to start iteration */
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index 2d46dbf876..d15a714e90 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -561,14 +561,25 @@ void ifile_close(InputFile **pf)
     av_freep(pf);
 }
 
+static void ist_use(InputStream *ist, int decoding_needed)
+{
+    ist->discard          = 0;
+    ist->st->discard      = ist->user_set_discard;
+    ist->decoding_needed |= decoding_needed;
+}
+
 void ist_output_add(InputStream *ist, OutputStream *ost)
 {
+    ist_use(ist, ost->enc ? DECODING_FOR_OST : 0);
+
     GROW_ARRAY(ist->outputs, ist->nb_outputs);
     ist->outputs[ist->nb_outputs - 1] = ost;
 }
 
-void ist_filter_add(InputStream *ist, InputFilter *ifilter)
+void ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple)
 {
+    ist_use(ist, is_simple ? DECODING_FOR_OST : DECODING_FOR_FILTER);
+
     GROW_ARRAY(ist->filters, ist->nb_filters);
     ist->filters[ist->nb_filters - 1] = ifilter;
 }
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index d2a185cf98..1d88d2e3b1 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -215,7 +215,7 @@ int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
     GROW_ARRAY(filtergraphs, nb_filtergraphs);
     filtergraphs[nb_filtergraphs - 1] = fg;
 
-    ist_filter_add(ist, ifilter);
+    ist_filter_add(ist, ifilter, 1);
 
     return 0;
 }
@@ -303,10 +303,6 @@ static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
     }
     av_assert0(ist);
 
-    ist->discard         = 0;
-    ist->decoding_needed |= DECODING_FOR_FILTER;
-    ist->st->discard = AVDISCARD_NONE;
-
     ifilter = ALLOC_ARRAY_ELEM(fg->inputs, fg->nb_inputs);
     ifilter->ist    = ist;
     ifilter->graph  = fg;
@@ -318,7 +314,7 @@ static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
     if (!ifilter->frame_queue)
         report_and_exit(AVERROR(ENOMEM));
 
-    ist_filter_add(ist, ifilter);
+    ist_filter_add(ist, ifilter, 0);
 }
 
 static int read_binary(const char *path, uint8_t **data, int *len)
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index dc1dd834e0..e3d8f5e386 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -1215,12 +1215,6 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o,
     }
 
     if (ost->ist) {
-        ost->ist->discard = 0;
-        ost->ist->st->discard = ost->ist->user_set_discard;
-
-        if (ost->enc)
-            ost->ist->decoding_needed |= DECODING_FOR_OST;
-
         if (ost->enc &&
             (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
             ret = init_simple_filtergraph(ost->ist, ost);
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 11/25] fftools/ffmpeg: move opening decoders to a new file
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (8 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 10/25] fftools/ffmpeg: stop setting InputStream fields from muxing/filtering code Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 12/25] fftools/ffmpeg_dec: reindent after previous commit Anton Khirnov
                   ` (13 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

All decoding code will be moved to this file in the future.
---
 fftools/Makefile     |   1 +
 fftools/ffmpeg.c     | 111 +----------------------------------
 fftools/ffmpeg.h     |   2 +
 fftools/ffmpeg_dec.c | 135 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 139 insertions(+), 110 deletions(-)
 create mode 100644 fftools/ffmpeg_dec.c

diff --git a/fftools/Makefile b/fftools/Makefile
index 9939c7095c..56820e6bc8 100644
--- a/fftools/Makefile
+++ b/fftools/Makefile
@@ -10,6 +10,7 @@ ALLAVPROGS   = $(AVBASENAMES:%=%$(PROGSSUF)$(EXESUF))
 ALLAVPROGS_G = $(AVBASENAMES:%=%$(PROGSSUF)_g$(EXESUF))
 
 OBJS-ffmpeg +=                  \
+    fftools/ffmpeg_dec.o        \
     fftools/ffmpeg_demux.o      \
     fftools/ffmpeg_enc.o        \
     fftools/ffmpeg_filter.o     \
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 57bdc12473..d8b06eb724 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -621,11 +621,6 @@ void assert_avoptions(AVDictionary *m)
     }
 }
 
-static void abort_codec_experimental(const AVCodec *c, int encoder)
-{
-    exit_program(1);
-}
-
 void update_benchmark(const char *fmt, ...)
 {
     if (do_benchmark_all) {
@@ -1858,110 +1853,6 @@ static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eo
     return !eof_reached;
 }
 
-static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
-{
-    InputStream *ist = s->opaque;
-    const enum AVPixelFormat *p;
-    int ret;
-
-    for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
-        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
-        const AVCodecHWConfig  *config = NULL;
-        int i;
-
-        if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
-            break;
-
-        if (ist->hwaccel_id == HWACCEL_GENERIC ||
-            ist->hwaccel_id == HWACCEL_AUTO) {
-            for (i = 0;; i++) {
-                config = avcodec_get_hw_config(s->codec, i);
-                if (!config)
-                    break;
-                if (!(config->methods &
-                      AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
-                    continue;
-                if (config->pix_fmt == *p)
-                    break;
-            }
-        }
-        if (config && config->device_type == ist->hwaccel_device_type) {
-            ret = hwaccel_decode_init(s);
-            if (ret < 0) {
-                if (ist->hwaccel_id == HWACCEL_GENERIC) {
-                    av_log(NULL, AV_LOG_FATAL,
-                           "%s hwaccel requested for input stream #%d:%d, "
-                           "but cannot be initialized.\n",
-                           av_hwdevice_get_type_name(config->device_type),
-                           ist->file_index, ist->st->index);
-                    return AV_PIX_FMT_NONE;
-                }
-                continue;
-            }
-
-            ist->hwaccel_pix_fmt = *p;
-            break;
-        }
-    }
-
-    return *p;
-}
-
-static int init_input_stream(InputStream *ist)
-{
-    int ret;
-
-    if (ist->decoding_needed) {
-        const AVCodec *codec = ist->dec;
-        if (!codec) {
-            av_log(ist, AV_LOG_ERROR,
-                   "Decoding requested, but no decoder found for: %s\n",
-                    avcodec_get_name(ist->dec_ctx->codec_id));
-            return AVERROR(EINVAL);
-        }
-
-        ist->dec_ctx->opaque                = ist;
-        ist->dec_ctx->get_format            = get_format;
-
-        if (ist->dec_ctx->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
-           (ist->decoding_needed & DECODING_FOR_OST)) {
-            av_dict_set(&ist->decoder_opts, "compute_edt", "1", AV_DICT_DONT_OVERWRITE);
-            if (ist->decoding_needed & DECODING_FOR_FILTER)
-                av_log(NULL, AV_LOG_WARNING, "Warning using DVB subtitles for filtering and output at the same time is not fully supported, also see -compute_edt [0|1]\n");
-        }
-
-        /* Useful for subtitles retiming by lavf (FIXME), skipping samples in
-         * audio, and video decoders such as cuvid or mediacodec */
-        ist->dec_ctx->pkt_timebase = ist->st->time_base;
-
-        if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0))
-            av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
-        /* Attached pics are sparse, therefore we would not want to delay their decoding till EOF. */
-        if (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)
-            av_dict_set(&ist->decoder_opts, "threads", "1", 0);
-
-        ret = hw_device_setup_for_decode(ist);
-        if (ret < 0) {
-            av_log(ist, AV_LOG_ERROR,
-                   "Hardware device setup failed for decoder: %s\n",
-                   av_err2str(ret));
-            return ret;
-        }
-
-        if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
-            if (ret == AVERROR_EXPERIMENTAL)
-                abort_codec_experimental(codec, 0);
-
-            av_log(ist, AV_LOG_ERROR, "Error while opening decoder: %s\n",
-                   av_err2str(ret));
-            return ret;
-        }
-        assert_avoptions(ist->decoder_opts);
-    }
-
-    return 0;
-}
-
 static int init_output_stream_nofilter(OutputStream *ost)
 {
     int ret = 0;
@@ -1993,7 +1884,7 @@ static int transcode_init(void)
 
     /* init input streams */
     for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist))
-        if ((ret = init_input_stream(ist)) < 0)
+        if (ist->decoding_needed && (ret = dec_open(ist)) < 0)
             goto dump_format;
 
     /*
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index c73fc8a459..5833c13812 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -829,6 +829,8 @@ AVBufferRef *hw_device_for_filter(void);
 
 int hwaccel_decode_init(AVCodecContext *avctx);
 
+int dec_open(InputStream *ist);
+
 int enc_alloc(Encoder **penc, const AVCodec *codec);
 void enc_free(Encoder **penc);
 
diff --git a/fftools/ffmpeg_dec.c b/fftools/ffmpeg_dec.c
new file mode 100644
index 0000000000..ccf558649c
--- /dev/null
+++ b/fftools/ffmpeg_dec.c
@@ -0,0 +1,135 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/dict.h"
+#include "libavutil/error.h"
+#include "libavutil/log.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/pixfmt.h"
+
+#include "libavcodec/avcodec.h"
+#include "libavcodec/codec.h"
+
+#include "ffmpeg.h"
+
+static void abort_codec_experimental(const AVCodec *c, int encoder)
+{
+    exit_program(1);
+}
+
+static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
+{
+    InputStream *ist = s->opaque;
+    const enum AVPixelFormat *p;
+    int ret;
+
+    for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
+        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
+        const AVCodecHWConfig  *config = NULL;
+        int i;
+
+        if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
+            break;
+
+        if (ist->hwaccel_id == HWACCEL_GENERIC ||
+            ist->hwaccel_id == HWACCEL_AUTO) {
+            for (i = 0;; i++) {
+                config = avcodec_get_hw_config(s->codec, i);
+                if (!config)
+                    break;
+                if (!(config->methods &
+                      AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
+                    continue;
+                if (config->pix_fmt == *p)
+                    break;
+            }
+        }
+        if (config && config->device_type == ist->hwaccel_device_type) {
+            ret = hwaccel_decode_init(s);
+            if (ret < 0) {
+                if (ist->hwaccel_id == HWACCEL_GENERIC) {
+                    av_log(NULL, AV_LOG_FATAL,
+                           "%s hwaccel requested for input stream #%d:%d, "
+                           "but cannot be initialized.\n",
+                           av_hwdevice_get_type_name(config->device_type),
+                           ist->file_index, ist->st->index);
+                    return AV_PIX_FMT_NONE;
+                }
+                continue;
+            }
+
+            ist->hwaccel_pix_fmt = *p;
+            break;
+        }
+    }
+
+    return *p;
+}
+
+int dec_open(InputStream *ist)
+{
+        const AVCodec *codec = ist->dec;
+    int ret;
+
+        if (!codec) {
+            av_log(ist, AV_LOG_ERROR,
+                   "Decoding requested, but no decoder found for: %s\n",
+                    avcodec_get_name(ist->dec_ctx->codec_id));
+            return AVERROR(EINVAL);
+        }
+
+        ist->dec_ctx->opaque                = ist;
+        ist->dec_ctx->get_format            = get_format;
+
+        if (ist->dec_ctx->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
+           (ist->decoding_needed & DECODING_FOR_OST)) {
+            av_dict_set(&ist->decoder_opts, "compute_edt", "1", AV_DICT_DONT_OVERWRITE);
+            if (ist->decoding_needed & DECODING_FOR_FILTER)
+                av_log(NULL, AV_LOG_WARNING, "Warning using DVB subtitles for filtering and output at the same time is not fully supported, also see -compute_edt [0|1]\n");
+        }
+
+        /* Useful for subtitles retiming by lavf (FIXME), skipping samples in
+         * audio, and video decoders such as cuvid or mediacodec */
+        ist->dec_ctx->pkt_timebase = ist->st->time_base;
+
+        if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0))
+            av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
+        /* Attached pics are sparse, therefore we would not want to delay their decoding till EOF. */
+        if (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)
+            av_dict_set(&ist->decoder_opts, "threads", "1", 0);
+
+        ret = hw_device_setup_for_decode(ist);
+        if (ret < 0) {
+            av_log(ist, AV_LOG_ERROR,
+                   "Hardware device setup failed for decoder: %s\n",
+                   av_err2str(ret));
+            return ret;
+        }
+
+        if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
+            if (ret == AVERROR_EXPERIMENTAL)
+                abort_codec_experimental(codec, 0);
+
+            av_log(ist, AV_LOG_ERROR, "Error while opening decoder: %s\n",
+                   av_err2str(ret));
+            return ret;
+        }
+        assert_avoptions(ist->decoder_opts);
+
+    return 0;
+}
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 12/25] fftools/ffmpeg_dec: reindent after previous commit
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (9 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 11/25] fftools/ffmpeg: move opening decoders to a new file Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 13/25] fftools/ffmpeg_dec: drop useless abort_codec_experimental() Anton Khirnov
                   ` (12 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

---
 fftools/ffmpeg_dec.c | 80 ++++++++++++++++++++++----------------------
 1 file changed, 40 insertions(+), 40 deletions(-)

diff --git a/fftools/ffmpeg_dec.c b/fftools/ffmpeg_dec.c
index ccf558649c..d6fd0de126 100644
--- a/fftools/ffmpeg_dec.c
+++ b/fftools/ffmpeg_dec.c
@@ -83,53 +83,53 @@ static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat
 
 int dec_open(InputStream *ist)
 {
-        const AVCodec *codec = ist->dec;
+    const AVCodec *codec = ist->dec;
     int ret;
 
-        if (!codec) {
-            av_log(ist, AV_LOG_ERROR,
-                   "Decoding requested, but no decoder found for: %s\n",
-                    avcodec_get_name(ist->dec_ctx->codec_id));
-            return AVERROR(EINVAL);
-        }
+    if (!codec) {
+        av_log(ist, AV_LOG_ERROR,
+               "Decoding requested, but no decoder found for: %s\n",
+                avcodec_get_name(ist->dec_ctx->codec_id));
+        return AVERROR(EINVAL);
+    }
 
-        ist->dec_ctx->opaque                = ist;
-        ist->dec_ctx->get_format            = get_format;
+    ist->dec_ctx->opaque                = ist;
+    ist->dec_ctx->get_format            = get_format;
 
-        if (ist->dec_ctx->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
-           (ist->decoding_needed & DECODING_FOR_OST)) {
-            av_dict_set(&ist->decoder_opts, "compute_edt", "1", AV_DICT_DONT_OVERWRITE);
-            if (ist->decoding_needed & DECODING_FOR_FILTER)
-                av_log(NULL, AV_LOG_WARNING, "Warning using DVB subtitles for filtering and output at the same time is not fully supported, also see -compute_edt [0|1]\n");
-        }
+    if (ist->dec_ctx->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
+       (ist->decoding_needed & DECODING_FOR_OST)) {
+        av_dict_set(&ist->decoder_opts, "compute_edt", "1", AV_DICT_DONT_OVERWRITE);
+        if (ist->decoding_needed & DECODING_FOR_FILTER)
+            av_log(NULL, AV_LOG_WARNING, "Warning using DVB subtitles for filtering and output at the same time is not fully supported, also see -compute_edt [0|1]\n");
+    }
 
-        /* Useful for subtitles retiming by lavf (FIXME), skipping samples in
-         * audio, and video decoders such as cuvid or mediacodec */
-        ist->dec_ctx->pkt_timebase = ist->st->time_base;
-
-        if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0))
-            av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
-        /* Attached pics are sparse, therefore we would not want to delay their decoding till EOF. */
-        if (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)
-            av_dict_set(&ist->decoder_opts, "threads", "1", 0);
-
-        ret = hw_device_setup_for_decode(ist);
-        if (ret < 0) {
-            av_log(ist, AV_LOG_ERROR,
-                   "Hardware device setup failed for decoder: %s\n",
-                   av_err2str(ret));
-            return ret;
-        }
+    /* Useful for subtitles retiming by lavf (FIXME), skipping samples in
+     * audio, and video decoders such as cuvid or mediacodec */
+    ist->dec_ctx->pkt_timebase = ist->st->time_base;
+
+    if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0))
+        av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
+    /* Attached pics are sparse, therefore we would not want to delay their decoding till EOF. */
+    if (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)
+        av_dict_set(&ist->decoder_opts, "threads", "1", 0);
+
+    ret = hw_device_setup_for_decode(ist);
+    if (ret < 0) {
+        av_log(ist, AV_LOG_ERROR,
+               "Hardware device setup failed for decoder: %s\n",
+               av_err2str(ret));
+        return ret;
+    }
 
-        if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
-            if (ret == AVERROR_EXPERIMENTAL)
-                abort_codec_experimental(codec, 0);
+    if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
+        if (ret == AVERROR_EXPERIMENTAL)
+            abort_codec_experimental(codec, 0);
 
-            av_log(ist, AV_LOG_ERROR, "Error while opening decoder: %s\n",
-                   av_err2str(ret));
-            return ret;
-        }
-        assert_avoptions(ist->decoder_opts);
+        av_log(ist, AV_LOG_ERROR, "Error while opening decoder: %s\n",
+               av_err2str(ret));
+        return ret;
+    }
+    assert_avoptions(ist->decoder_opts);
 
     return 0;
 }
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 13/25] fftools/ffmpeg_dec: drop useless abort_codec_experimental()
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (10 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 12/25] fftools/ffmpeg_dec: reindent after previous commit Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 14/25] fftools/ffmpeg: open decoders right after we know they are needed Anton Khirnov
                   ` (11 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

It does nothing beyond exit_program().
---
 fftools/ffmpeg_dec.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/fftools/ffmpeg_dec.c b/fftools/ffmpeg_dec.c
index d6fd0de126..658e7418e9 100644
--- a/fftools/ffmpeg_dec.c
+++ b/fftools/ffmpeg_dec.c
@@ -27,11 +27,6 @@
 
 #include "ffmpeg.h"
 
-static void abort_codec_experimental(const AVCodec *c, int encoder)
-{
-    exit_program(1);
-}
-
 static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
 {
     InputStream *ist = s->opaque;
@@ -123,7 +118,7 @@ int dec_open(InputStream *ist)
 
     if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
         if (ret == AVERROR_EXPERIMENTAL)
-            abort_codec_experimental(codec, 0);
+            exit_program(1);
 
         av_log(ist, AV_LOG_ERROR, "Error while opening decoder: %s\n",
                av_err2str(ret));
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 14/25] fftools/ffmpeg: open decoders right after we know they are needed
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (11 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 13/25] fftools/ffmpeg_dec: drop useless abort_codec_experimental() Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 15/25] fftools/ffmpeg: initialize no-filter streams earlier Anton Khirnov
                   ` (10 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

Will allow initializing subtitle encoding earlier.
---
 fftools/ffmpeg.c       | 5 -----
 fftools/ffmpeg_demux.c | 6 ++++++
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index d8b06eb724..900d6e5e83 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1882,11 +1882,6 @@ static int transcode_init(void)
                 ifile->streams[j]->start = av_gettime_relative();
     }
 
-    /* init input streams */
-    for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist))
-        if (ist->decoding_needed && (ret = dec_open(ist)) < 0)
-            goto dump_format;
-
     /*
      * initialize stream copy and subtitle/data streams.
      * Encoded AVFrame based streams will get initialized when the first AVFrame
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index d15a714e90..91609509b2 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -566,6 +566,12 @@ static void ist_use(InputStream *ist, int decoding_needed)
     ist->discard          = 0;
     ist->st->discard      = ist->user_set_discard;
     ist->decoding_needed |= decoding_needed;
+
+    if (decoding_needed && !avcodec_is_open(ist->dec_ctx)) {
+        int ret = dec_open(ist);
+        if (ret < 0)
+            report_and_exit(ret);
+    }
 }
 
 void ist_output_add(InputStream *ist, OutputStream *ost)
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 15/25] fftools/ffmpeg: initialize no-filter streams earlier
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (12 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 14/25] fftools/ffmpeg: open decoders right after we know they are needed Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 16/25] fftools/ffmpeg: store stream media type in OutputStream Anton Khirnov
                   ` (9 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

There is no reason to postpone it until transcode_init() anymore, it can
be done right at the end of of_open().
---
 fftools/ffmpeg.c          | 34 ----------------------------------
 fftools/ffmpeg_mux_init.c | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 34 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 900d6e5e83..33b2f72d90 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1853,23 +1853,6 @@ static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eo
     return !eof_reached;
 }
 
-static int init_output_stream_nofilter(OutputStream *ost)
-{
-    int ret = 0;
-
-    if (ost->enc_ctx) {
-        ret = enc_open(ost, NULL);
-        if (ret < 0)
-            return ret;
-    } else {
-        ret = of_stream_init(output_files[ost->file_index], ost);
-        if (ret < 0)
-            return ret;
-    }
-
-    return ret;
-}
-
 static int transcode_init(void)
 {
     int ret = 0;
@@ -1882,22 +1865,6 @@ static int transcode_init(void)
                 ifile->streams[j]->start = av_gettime_relative();
     }
 
-    /*
-     * initialize stream copy and subtitle/data streams.
-     * Encoded AVFrame based streams will get initialized when the first AVFrame
-     * is received in do_video_out
-     */
-    for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
-        if (ost->enc_ctx &&
-            (ost->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
-             ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))
-            continue;
-
-        ret = init_output_stream_nofilter(ost);
-        if (ret < 0)
-            goto dump_format;
-    }
-
     /* discard unused programs */
     for (int i = 0; i < nb_input_files; i++) {
         InputFile *ifile = input_files[i];
@@ -1914,7 +1881,6 @@ static int transcode_init(void)
         }
     }
 
- dump_format:
     /* dump the stream mapping */
     av_log(NULL, AV_LOG_INFO, "Stream mapping:\n");
     for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist)) {
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index e3d8f5e386..bc14cabad4 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -2314,6 +2314,23 @@ static void validate_enc_avopt(Muxer *mux, const AVDictionary *codec_avopt)
     av_dict_free(&unused_opts);
 }
 
+static int init_output_stream_nofilter(OutputStream *ost)
+{
+    int ret = 0;
+
+    if (ost->enc_ctx) {
+        ret = enc_open(ost, NULL);
+        if (ret < 0)
+            return ret;
+    } else {
+        ret = of_stream_init(output_files[ost->file_index], ost);
+        if (ret < 0)
+            return ret;
+    }
+
+    return ret;
+}
+
 static const char *output_file_item_name(void *obj)
 {
     const Muxer *mux = obj;
@@ -2509,6 +2526,21 @@ int of_open(const OptionsContext *o, const char *filename)
 
     of->url        = filename;
 
+    /* initialize stream copy and subtitle/data streams.
+     * Encoded AVFrame based streams will get initialized when the first AVFrame
+     * is received in do_video_out
+     */
+    for (int i = 0; i < of->nb_streams; i++) {
+        OutputStream *ost = of->streams[i];
+
+        if (ost->filter)
+            continue;
+
+        err = init_output_stream_nofilter(ost);
+        if (err < 0)
+            report_and_exit(err);
+    }
+
     /* write the header for files with no streams */
     if (of->format->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) {
         int ret = mux_check_init(mux);
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 16/25] fftools/ffmpeg: store stream media type in OutputStream
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (13 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 15/25] fftools/ffmpeg: initialize no-filter streams earlier Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 17/25] fftools/ffmpeg: add muxer-input codec parameters to OutputStream Anton Khirnov
                   ` (8 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

Reduces access to a deeply nested muxer property
OutputStream.st->codecpar->codec_type for this fundamental and immutable
stream property.

Besides making the code shorter, this will allow making the AVStream
(OutputStream.st) private to the muxer in the future.
---
 fftools/ffmpeg.c          |  4 ++--
 fftools/ffmpeg.h          |  2 ++
 fftools/ffmpeg_mux.c      | 11 +++++------
 fftools/ffmpeg_mux_init.c | 26 ++++++++++++--------------
 4 files changed, 21 insertions(+), 22 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 33b2f72d90..30b42b4fde 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -748,12 +748,12 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
         const AVCodecContext * const enc = ost->enc_ctx;
         const float q = enc ? ost->quality / (float) FF_QP2LAMBDA : -1;
 
-        if (vid && ost->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
+        if (vid && ost->type == AVMEDIA_TYPE_VIDEO) {
             av_bprintf(&buf, "q=%2.1f ", q);
             av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n",
                        ost->file_index, ost->index, q);
         }
-        if (!vid && ost->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
+        if (!vid && ost->type == AVMEDIA_TYPE_VIDEO) {
             float fps;
             uint64_t frame_number = atomic_load(&ost->packets_written);
 
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 5833c13812..f08f5db49e 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -578,6 +578,8 @@ typedef struct Encoder Encoder;
 typedef struct OutputStream {
     const AVClass *class;
 
+    enum AVMediaType type;
+
     int file_index;          /* file index */
     int index;               /* stream index in the output file */
 
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index b316925115..eb64d8c3ff 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -62,7 +62,6 @@ static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
 {
     MuxStream *ms = ms_from_ost(ost);
     AVFormatContext *s = mux->fc;
-    AVStream *st = ost->st;
     int64_t fs;
     uint64_t frame_num;
     int ret;
@@ -74,10 +73,10 @@ static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
         goto fail;
     }
 
-    if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->vsync_method == VSYNC_DROP)
+    if (ost->type == AVMEDIA_TYPE_VIDEO && ost->vsync_method == VSYNC_DROP)
         pkt->pts = pkt->dts = AV_NOPTS_VALUE;
 
-    if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
+    if (ost->type == AVMEDIA_TYPE_VIDEO) {
         if (ost->frame_rate.num && ost->is_cfr) {
             if (pkt->duration > 0)
                 av_log(ost, AV_LOG_WARNING, "Overriding packet duration by frame rate, this should not happen\n");
@@ -101,12 +100,12 @@ static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
                      - FFMIN3(pkt->pts, pkt->dts, ms->last_mux_dts + 1)
                      - FFMAX3(pkt->pts, pkt->dts, ms->last_mux_dts + 1);
         }
-        if ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) &&
+        if ((ost->type == AVMEDIA_TYPE_AUDIO || ost->type == AVMEDIA_TYPE_VIDEO || ost->type == AVMEDIA_TYPE_SUBTITLE) &&
             pkt->dts != AV_NOPTS_VALUE &&
             ms->last_mux_dts != AV_NOPTS_VALUE) {
             int64_t max = ms->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT);
             if (pkt->dts < max) {
-                int loglevel = max - pkt->dts > 2 || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
+                int loglevel = max - pkt->dts > 2 || ost->type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
                 if (exit_on_error)
                     loglevel = AV_LOG_ERROR;
                 av_log(s, loglevel, "Non-monotonous DTS in output stream "
@@ -136,7 +135,7 @@ static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
     if (debug_ts) {
         av_log(ost, AV_LOG_INFO, "muxer <- type:%s "
                 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s size:%d\n",
-                av_get_media_type_string(st->codecpar->codec_type),
+                av_get_media_type_string(ost->type),
                 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
                 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base),
                 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->st->time_base),
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index bc14cabad4..129973d9f0 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -108,7 +108,7 @@ static int check_opt_bitexact(void *ctx, const AVDictionary *opts,
 static int choose_encoder(const OptionsContext *o, AVFormatContext *s,
                           OutputStream *ost, const AVCodec **enc)
 {
-    enum AVMediaType type = ost->st->codecpar->codec_type;
+    enum AVMediaType type = ost->type;
     char *codec_name = NULL;
 
     *enc = NULL;
@@ -117,7 +117,7 @@ static int choose_encoder(const OptionsContext *o, AVFormatContext *s,
         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,
-                                                         NULL, ost->st->codecpar->codec_type);
+                                                         NULL, ost->type);
             *enc = avcodec_find_encoder(ost->st->codecpar->codec_id);
             if (!*enc) {
                 av_log(ost, AV_LOG_FATAL, "Automatic encoder selection failed "
@@ -127,7 +127,7 @@ static int choose_encoder(const OptionsContext *o, AVFormatContext *s,
                 return AVERROR_ENCODER_NOT_FOUND;
             }
         } else if (strcmp(codec_name, "copy")) {
-            *enc = find_codec_or_die(ost, codec_name, ost->st->codecpar->codec_type, 1);
+            *enc = find_codec_or_die(ost, codec_name, ost->type, 1);
             ost->st->codecpar->codec_id = (*enc)->id;
         }
     }
@@ -410,6 +410,7 @@ static MuxStream *mux_stream_alloc(Muxer *mux, enum AVMediaType type)
 
     ms->ost.file_index = mux->of.index;
     ms->ost.index      = mux->of.nb_streams - 1;
+    ms->ost.type       = type;
 
     ms->ost.class = &output_stream_class;
 
@@ -422,8 +423,6 @@ static MuxStream *mux_stream_alloc(Muxer *mux, enum AVMediaType type)
 static char *get_ost_filters(const OptionsContext *o, AVFormatContext *oc,
                              OutputStream *ost)
 {
-    AVStream *st = ost->st;
-
     if (ost->filters_script && ost->filters) {
         av_log(ost, AV_LOG_ERROR, "Both -filter and -filter_script set\n");
         exit_program(1);
@@ -434,8 +433,7 @@ static char *get_ost_filters(const OptionsContext *o, AVFormatContext *oc,
     else if (ost->filters)
         return av_strdup(ost->filters);
 
-    return av_strdup(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ?
-                     "null" : "anull");
+    return av_strdup(ost->type == AVMEDIA_TYPE_VIDEO ? "null" : "anull");
 }
 
 static void check_streamcopy_filters(const OptionsContext *o, AVFormatContext *oc,
@@ -1594,7 +1592,7 @@ static int setup_sync_queues(Muxer *mux, AVFormatContext *oc, int64_t buf_size_u
     for (int i = 0; i < oc->nb_streams; i++) {
         OutputStream *ost = of->streams[i];
         MuxStream     *ms = ms_from_ost(ost);
-        enum AVMediaType type = ost->st->codecpar->codec_type;
+        enum AVMediaType type = ost->type;
 
         ost->sq_idx_encode = -1;
         ost->sq_idx_mux    = -1;
@@ -1628,7 +1626,7 @@ static int setup_sync_queues(Muxer *mux, AVFormatContext *oc, int64_t buf_size_u
         for (int i = 0; i < oc->nb_streams; i++) {
             OutputStream *ost = of->streams[i];
             MuxStream     *ms = ms_from_ost(ost);
-            enum AVMediaType type = ost->st->codecpar->codec_type;
+            enum AVMediaType type = ost->type;
 
             if (!IS_AV_ENC(ost, type))
                 continue;
@@ -1657,7 +1655,7 @@ static int setup_sync_queues(Muxer *mux, AVFormatContext *oc, int64_t buf_size_u
         for (int i = 0; i < oc->nb_streams; i++) {
             OutputStream *ost = of->streams[i];
             MuxStream     *ms = ms_from_ost(ost);
-            enum AVMediaType type = ost->st->codecpar->codec_type;
+            enum AVMediaType type = ost->type;
 
             if (!IS_INTERLEAVED(type))
                 continue;
@@ -2113,7 +2111,7 @@ static int set_dispositions(Muxer *mux, const OptionsContext *o)
     for (int i = 0; i < ctx->nb_streams; i++) {
         OutputStream *ost = of->streams[i];
 
-        nb_streams[ost->st->codecpar->codec_type]++;
+        nb_streams[ost->type]++;
 
         MATCH_PER_STREAM_OPT(disposition, str, dispositions[i], ctx, ost->st);
 
@@ -2123,7 +2121,7 @@ static int set_dispositions(Muxer *mux, const OptionsContext *o)
             ost->st->disposition = ost->ist->st->disposition;
 
             if (ost->st->disposition & AV_DISPOSITION_DEFAULT)
-                have_default[ost->st->codecpar->codec_type] = 1;
+                have_default[ost->type] = 1;
         }
     }
 
@@ -2146,7 +2144,7 @@ static int set_dispositions(Muxer *mux, const OptionsContext *o)
         // "Suitable" means the first of that type, skipping attached pictures.
         for (int i = 0; i < ctx->nb_streams; i++) {
             OutputStream *ost = of->streams[i];
-            enum AVMediaType type = ost->st->codecpar->codec_type;
+            enum AVMediaType type = ost->type;
 
             if (nb_streams[type] < 2 || have_default[type] ||
                 ost->st->disposition & AV_DISPOSITION_ATTACHED_PIC)
@@ -2239,7 +2237,7 @@ static int process_forced_keyframes(Muxer *mux, const OptionsContext *o)
 
         MATCH_PER_STREAM_OPT(forced_key_frames, str, forced_keyframes, mux->fc, ost->st);
 
-        if (!(ost->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
+        if (!(ost->type == AVMEDIA_TYPE_VIDEO &&
               ost->enc_ctx && forced_keyframes))
             continue;
 
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 17/25] fftools/ffmpeg: add muxer-input codec parameters to OutputStream
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (14 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 16/25] fftools/ffmpeg: store stream media type in OutputStream Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 18/25] fftools/ffmpeg: move do_streamcopy() to ffmpeg_mux Anton Khirnov
                   ` (7 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

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.
---
 fftools/ffmpeg.h          |  6 ++++++
 fftools/ffmpeg_enc.c      |  2 +-
 fftools/ffmpeg_mux.c      |  6 ++++--
 fftools/ffmpeg_mux_init.c | 21 +++++++++++++--------
 4 files changed, 24 insertions(+), 11 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 129973d9f0..33d5268949 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);
@@ -1166,7 +1171,7 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o,
         uint32_t tag = strtol(codec_tag, &next, 0);
         if (*next)
             tag = AV_RL32(codec_tag);
-        ost->st->codecpar->codec_tag = tag;
+        ost->par_in->codec_tag = tag;
         if (ost->enc_ctx)
             ost->enc_ctx->codec_tag = tag;
     }
@@ -1520,8 +1525,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);
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 18/25] fftools/ffmpeg: move do_streamcopy() to ffmpeg_mux
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (15 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 17/25] fftools/ffmpeg: add muxer-input codec parameters to OutputStream Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 19/25] fftools/ffmpeg_mux: use output stream parameters in of_streamcopy() Anton Khirnov
                   ` (6 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

do_streamcopy() is muxing code, so this is a more appropriate place for
this. The last uses of InputStream in it will be removed in following
commits.
---
 fftools/ffmpeg.c     | 81 +-------------------------------------------
 fftools/ffmpeg.h     |  7 ++++
 fftools/ffmpeg_mux.c | 76 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 84 insertions(+), 80 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 30b42b4fde..92cc98b089 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -914,85 +914,6 @@ int ifilter_parameters_from_codecpar(InputFilter *ifilter, AVCodecParameters *pa
     return 0;
 }
 
-/**
- * @param dts predicted packet dts in AV_TIME_BASE_Q
- */
-static void do_streamcopy(InputStream *ist, OutputStream *ost,
-                          const AVPacket *pkt, int64_t dts)
-{
-    OutputFile *of = output_files[ost->file_index];
-    int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
-    int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->mux_timebase);
-    AVPacket *opkt = ost->pkt;
-
-    av_packet_unref(opkt);
-    // EOF: flush output bitstream filters.
-    if (!pkt) {
-        of_output_packet(of, opkt, ost, 1);
-        return;
-    }
-
-    if (!ost->streamcopy_started && !(pkt->flags & AV_PKT_FLAG_KEY) &&
-        !ost->copy_initial_nonkeyframes)
-        return;
-
-    if (!ost->streamcopy_started) {
-        if (!ost->copy_prior_start &&
-            (pkt->pts == AV_NOPTS_VALUE ?
-             dts < ost->ts_copy_start :
-             pkt->pts < av_rescale_q(ost->ts_copy_start, AV_TIME_BASE_Q, pkt->time_base)))
-            return;
-
-        if (of->start_time != AV_NOPTS_VALUE && dts < of->start_time)
-            return;
-    }
-
-    if (of->recording_time != INT64_MAX &&
-        dts >= of->recording_time + start_time) {
-        close_output_stream(ost);
-        return;
-    }
-
-    if (av_packet_ref(opkt, pkt) < 0)
-        exit_program(1);
-
-    opkt->time_base = ost->mux_timebase;
-
-    if (pkt->pts != AV_NOPTS_VALUE)
-        opkt->pts = av_rescale_q(pkt->pts, pkt->time_base, opkt->time_base) - ost_tb_start_time;
-
-    if (pkt->dts == AV_NOPTS_VALUE) {
-        opkt->dts = av_rescale_q(dts, AV_TIME_BASE_Q, opkt->time_base);
-    } else if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
-        int duration = av_get_audio_frame_duration2(ist->par, pkt->size);
-        if(!duration)
-            duration = ist->par->frame_size;
-        opkt->dts = av_rescale_delta(pkt->time_base, pkt->dts,
-                                    (AVRational){1, ist->par->sample_rate}, duration,
-                                    &ist->filter_in_rescale_delta_last, opkt->time_base);
-        /* dts will be set immediately afterwards to what pts is now */
-        opkt->pts = opkt->dts - ost_tb_start_time;
-    } else
-        opkt->dts = av_rescale_q(pkt->dts, pkt->time_base, opkt->time_base);
-    opkt->dts -= ost_tb_start_time;
-
-    opkt->duration = av_rescale_q(pkt->duration, pkt->time_base, opkt->time_base);
-
-    {
-        int ret = trigger_fix_sub_duration_heartbeat(ost, pkt);
-        if (ret < 0) {
-            av_log(NULL, AV_LOG_ERROR,
-                   "Subtitle heartbeat logic failed in %s! (%s)\n",
-                   __func__, av_err2str(ret));
-            exit_program(1);
-        }
-    }
-
-    of_output_packet(of, opkt, ost, 0);
-
-    ost->streamcopy_started = 1;
-}
-
 static void check_decode_result(InputStream *ist, int *got_output, int ret)
 {
     if (*got_output || ret<0)
@@ -1847,7 +1768,7 @@ static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eo
             continue;
         }
 
-        do_streamcopy(ist, ost, pkt, ist->dts);
+        of_streamcopy(ist, ost, pkt, ist->dts);
     }
 
     return !eof_reached;
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 8ec0f1b3f3..46b4614ec4 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -872,6 +872,13 @@ void of_enc_stats_close(void);
  * must be supplied in this case.
  */
 void of_output_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof);
+
+/**
+ * @param dts predicted packet dts in AV_TIME_BASE_Q
+ */
+void of_streamcopy(InputStream *ist, OutputStream *ost,
+                   const AVPacket *pkt, int64_t dts);
+
 int64_t of_filesize(OutputFile *of);
 
 int ifile_open(const OptionsContext *o, const char *filename);
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 40b439eea2..96ed50f73c 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -378,6 +378,82 @@ fail:
 
 }
 
+void of_streamcopy(InputStream *ist, OutputStream *ost,
+                   const AVPacket *pkt, int64_t dts)
+{
+    OutputFile *of = output_files[ost->file_index];
+    int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
+    int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->mux_timebase);
+    AVPacket *opkt = ost->pkt;
+
+    av_packet_unref(opkt);
+    // EOF: flush output bitstream filters.
+    if (!pkt) {
+        of_output_packet(of, opkt, ost, 1);
+        return;
+    }
+
+    if (!ost->streamcopy_started && !(pkt->flags & AV_PKT_FLAG_KEY) &&
+        !ost->copy_initial_nonkeyframes)
+        return;
+
+    if (!ost->streamcopy_started) {
+        if (!ost->copy_prior_start &&
+            (pkt->pts == AV_NOPTS_VALUE ?
+             dts < ost->ts_copy_start :
+             pkt->pts < av_rescale_q(ost->ts_copy_start, AV_TIME_BASE_Q, pkt->time_base)))
+            return;
+
+        if (of->start_time != AV_NOPTS_VALUE && dts < of->start_time)
+            return;
+    }
+
+    if (of->recording_time != INT64_MAX &&
+        dts >= of->recording_time + start_time) {
+        close_output_stream(ost);
+        return;
+    }
+
+    if (av_packet_ref(opkt, pkt) < 0)
+        exit_program(1);
+
+    opkt->time_base = ost->mux_timebase;
+
+    if (pkt->pts != AV_NOPTS_VALUE)
+        opkt->pts = av_rescale_q(pkt->pts, pkt->time_base, opkt->time_base) - ost_tb_start_time;
+
+    if (pkt->dts == AV_NOPTS_VALUE) {
+        opkt->dts = av_rescale_q(dts, AV_TIME_BASE_Q, opkt->time_base);
+    } else if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
+        int duration = av_get_audio_frame_duration2(ist->par, pkt->size);
+        if(!duration)
+            duration = ist->par->frame_size;
+        opkt->dts = av_rescale_delta(pkt->time_base, pkt->dts,
+                                    (AVRational){1, ist->par->sample_rate}, duration,
+                                    &ist->filter_in_rescale_delta_last, opkt->time_base);
+        /* dts will be set immediately afterwards to what pts is now */
+        opkt->pts = opkt->dts - ost_tb_start_time;
+    } else
+        opkt->dts = av_rescale_q(pkt->dts, pkt->time_base, opkt->time_base);
+    opkt->dts -= ost_tb_start_time;
+
+    opkt->duration = av_rescale_q(pkt->duration, pkt->time_base, opkt->time_base);
+
+    {
+        int ret = trigger_fix_sub_duration_heartbeat(ost, pkt);
+        if (ret < 0) {
+            av_log(NULL, AV_LOG_ERROR,
+                   "Subtitle heartbeat logic failed in %s! (%s)\n",
+                   __func__, av_err2str(ret));
+            exit_program(1);
+        }
+    }
+
+    of_output_packet(of, opkt, ost, 0);
+
+    ost->streamcopy_started = 1;
+}
+
 static int thread_stop(Muxer *mux)
 {
     void *ret;
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 19/25] fftools/ffmpeg_mux: use output stream parameters in of_streamcopy()
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (16 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 18/25] fftools/ffmpeg: move do_streamcopy() to ffmpeg_mux Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 20/25] fftools/ffmpeg_mux: stop using filter_in_rescale_delta_last for streamcopy Anton Khirnov
                   ` (5 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

They should always be the same as the input stream parameters, but this
reduces the need to access InputStream in muxing code.
---
 fftools/ffmpeg_mux.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 96ed50f73c..db6ad9d589 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -425,11 +425,11 @@ void of_streamcopy(InputStream *ist, OutputStream *ost,
     if (pkt->dts == AV_NOPTS_VALUE) {
         opkt->dts = av_rescale_q(dts, AV_TIME_BASE_Q, opkt->time_base);
     } else if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
-        int duration = av_get_audio_frame_duration2(ist->par, pkt->size);
+        int duration = av_get_audio_frame_duration2(ost->par_in, pkt->size);
         if(!duration)
-            duration = ist->par->frame_size;
+            duration = ost->par_in->frame_size;
         opkt->dts = av_rescale_delta(pkt->time_base, pkt->dts,
-                                    (AVRational){1, ist->par->sample_rate}, duration,
+                                    (AVRational){1, ost->par_in->sample_rate}, duration,
                                     &ist->filter_in_rescale_delta_last, opkt->time_base);
         /* dts will be set immediately afterwards to what pts is now */
         opkt->pts = opkt->dts - ost_tb_start_time;
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 20/25] fftools/ffmpeg_mux: stop using filter_in_rescale_delta_last for streamcopy
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (17 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 19/25] fftools/ffmpeg_mux: use output stream parameters in of_streamcopy() Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 21/25] fftools/ffmpeg_mux: make ts_copy_start private to muxing code Anton Khirnov
                   ` (4 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

That field was added to store timestamp conversion state for audio
decoding code. Later it started being used by streamcopy as well, but
that use is wrong because, for a given input stream, both decoding and
an arbitrary number of streamcopies may be performed simultaneously.
They would then all overwrite the same state variable.

Store this state in MuxStream instead.

This is the last use of InputStream in of_streamcopy(), so the ist
parameter can now be removed.
---
 fftools/ffmpeg.c     | 2 +-
 fftools/ffmpeg.h     | 3 +--
 fftools/ffmpeg_mux.c | 6 +++---
 fftools/ffmpeg_mux.h | 3 +++
 4 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 92cc98b089..67c504470a 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1768,7 +1768,7 @@ static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eo
             continue;
         }
 
-        of_streamcopy(ist, ost, pkt, ist->dts);
+        of_streamcopy(ost, pkt, ist->dts);
     }
 
     return !eof_reached;
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 46b4614ec4..6ad3245166 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -876,8 +876,7 @@ void of_output_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof)
 /**
  * @param dts predicted packet dts in AV_TIME_BASE_Q
  */
-void of_streamcopy(InputStream *ist, OutputStream *ost,
-                   const AVPacket *pkt, int64_t dts);
+void of_streamcopy(OutputStream *ost, const AVPacket *pkt, int64_t dts);
 
 int64_t of_filesize(OutputFile *of);
 
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index db6ad9d589..8c710c2eac 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -378,10 +378,10 @@ fail:
 
 }
 
-void of_streamcopy(InputStream *ist, OutputStream *ost,
-                   const AVPacket *pkt, int64_t dts)
+void of_streamcopy(OutputStream *ost, const AVPacket *pkt, int64_t dts)
 {
     OutputFile *of = output_files[ost->file_index];
+    MuxStream  *ms = ms_from_ost(ost);
     int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
     int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->mux_timebase);
     AVPacket *opkt = ost->pkt;
@@ -430,7 +430,7 @@ void of_streamcopy(InputStream *ist, OutputStream *ost,
             duration = ost->par_in->frame_size;
         opkt->dts = av_rescale_delta(pkt->time_base, pkt->dts,
                                     (AVRational){1, ost->par_in->sample_rate}, duration,
-                                    &ist->filter_in_rescale_delta_last, opkt->time_base);
+                                    &ms->ts_rescale_delta_last, opkt->time_base);
         /* dts will be set immediately afterwards to what pts is now */
         opkt->pts = opkt->dts - ost_tb_start_time;
     } else
diff --git a/fftools/ffmpeg_mux.h b/fftools/ffmpeg_mux.h
index 3fab74b2ed..e8c4ea4847 100644
--- a/fftools/ffmpeg_mux.h
+++ b/fftools/ffmpeg_mux.h
@@ -64,6 +64,9 @@ typedef struct MuxStream {
      * used for making up missing dts values */
     int64_t last_mux_dts;
 
+    // audio streamcopy - state for av_rescale_delta()
+    int64_t ts_rescale_delta_last;
+
     // combined size of all the packets sent to the muxer
     uint64_t data_size_mux;
 } MuxStream;
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 21/25] fftools/ffmpeg_mux: make ts_copy_start private to muxing code
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (18 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 20/25] fftools/ffmpeg_mux: stop using filter_in_rescale_delta_last for streamcopy Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 22/25] fftools/ffmpeg_mux: make streamcopy_started " Anton Khirnov
                   ` (3 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

It is no longer used outside of ffmpeg_mux*
---
 fftools/ffmpeg.h          |  5 -----
 fftools/ffmpeg_mux.c      |  4 ++--
 fftools/ffmpeg_mux.h      |  5 +++++
 fftools/ffmpeg_mux_init.c | 10 ++++++----
 4 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 6ad3245166..e626fac18a 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -598,11 +598,6 @@ typedef struct OutputStream {
     /* dts of the last packet sent to the muxing queue, in AV_TIME_BASE_Q */
     int64_t last_mux_dts;
 
-    // timestamp from which the streamcopied streams should start,
-    // in AV_TIME_BASE_Q;
-    // everything before it should be discarded
-    int64_t ts_copy_start;
-
     // the timebase of the packets sent to the muxer
     AVRational mux_timebase;
     AVRational enc_timebase;
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 8c710c2eac..d832116c12 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -400,8 +400,8 @@ void of_streamcopy(OutputStream *ost, const AVPacket *pkt, int64_t dts)
     if (!ost->streamcopy_started) {
         if (!ost->copy_prior_start &&
             (pkt->pts == AV_NOPTS_VALUE ?
-             dts < ost->ts_copy_start :
-             pkt->pts < av_rescale_q(ost->ts_copy_start, AV_TIME_BASE_Q, pkt->time_base)))
+             dts < ms->ts_copy_start :
+             pkt->pts < av_rescale_q(ms->ts_copy_start, AV_TIME_BASE_Q, pkt->time_base)))
             return;
 
         if (of->start_time != AV_NOPTS_VALUE && dts < of->start_time)
diff --git a/fftools/ffmpeg_mux.h b/fftools/ffmpeg_mux.h
index e8c4ea4847..25cf1b547f 100644
--- a/fftools/ffmpeg_mux.h
+++ b/fftools/ffmpeg_mux.h
@@ -60,6 +60,11 @@ typedef struct MuxStream {
     /* Threshold after which max_muxing_queue_size will be in effect */
     size_t muxing_queue_data_threshold;
 
+    // timestamp from which the streamcopied streams should start,
+    // in AV_TIME_BASE_Q;
+    // everything before it should be discarded
+    int64_t ts_copy_start;
+
     /* dts of the last packet sent to the muxer, in the stream timebase
      * used for making up missing dts values */
     int64_t last_mux_dts;
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index 33d5268949..48efaef664 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -841,6 +841,8 @@ static void new_stream_subtitle(Muxer *mux, const OptionsContext *o,
 static int streamcopy_init(const Muxer *mux, const OptionsContext *o,
                            OutputStream *ost)
 {
+    MuxStream           *ms         = ms_from_ost(ost);
+
     const InputStream   *ist        = ost->ist;
     const InputFile     *ifile      = input_files[ist->file_index];
 
@@ -907,11 +909,11 @@ static int streamcopy_init(const Muxer *mux, const OptionsContext *o,
         ost->st->duration = av_rescale_q(ist->st->duration, ist->st->time_base, ost->st->time_base);
 
     if (!ost->copy_prior_start) {
-        ost->ts_copy_start = (mux->of.start_time == AV_NOPTS_VALUE) ?
-                             0 : mux->of.start_time;
+        ms->ts_copy_start = (mux->of.start_time == AV_NOPTS_VALUE) ?
+                            0 : mux->of.start_time;
         if (copy_ts && ifile->start_time != AV_NOPTS_VALUE) {
-            ost->ts_copy_start = FFMAX(ost->ts_copy_start,
-                                       ifile->start_time + ifile->ts_offset);
+            ms->ts_copy_start = FFMAX(ms->ts_copy_start,
+                                      ifile->start_time + ifile->ts_offset);
         }
     }
 
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 22/25] fftools/ffmpeg_mux: make streamcopy_started private to muxing code
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (19 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 21/25] fftools/ffmpeg_mux: make ts_copy_start private to muxing code Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 23/25] fftools/ffmpeg_mux: make copy_prior_start " Anton Khirnov
                   ` (2 subsequent siblings)
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

It is no longer used outside of ffmpeg_mux*
---
 fftools/ffmpeg.h     | 1 -
 fftools/ffmpeg_mux.c | 6 +++---
 fftools/ffmpeg_mux.h | 2 ++
 3 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index e626fac18a..cc72b1cb6e 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -658,7 +658,6 @@ typedef struct OutputStream {
     int inputs_done;
 
     const char *attachment_filename;
-    int streamcopy_started;
     int copy_initial_nonkeyframes;
     int copy_prior_start;
 
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index d832116c12..01ccac55ce 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -393,11 +393,11 @@ void of_streamcopy(OutputStream *ost, const AVPacket *pkt, int64_t dts)
         return;
     }
 
-    if (!ost->streamcopy_started && !(pkt->flags & AV_PKT_FLAG_KEY) &&
+    if (!ms->streamcopy_started && !(pkt->flags & AV_PKT_FLAG_KEY) &&
         !ost->copy_initial_nonkeyframes)
         return;
 
-    if (!ost->streamcopy_started) {
+    if (!ms->streamcopy_started) {
         if (!ost->copy_prior_start &&
             (pkt->pts == AV_NOPTS_VALUE ?
              dts < ms->ts_copy_start :
@@ -451,7 +451,7 @@ void of_streamcopy(OutputStream *ost, const AVPacket *pkt, int64_t dts)
 
     of_output_packet(of, opkt, ost, 0);
 
-    ost->streamcopy_started = 1;
+    ms->streamcopy_started = 1;
 }
 
 static int thread_stop(Muxer *mux)
diff --git a/fftools/ffmpeg_mux.h b/fftools/ffmpeg_mux.h
index 25cf1b547f..0938a261ee 100644
--- a/fftools/ffmpeg_mux.h
+++ b/fftools/ffmpeg_mux.h
@@ -74,6 +74,8 @@ typedef struct MuxStream {
 
     // combined size of all the packets sent to the muxer
     uint64_t data_size_mux;
+
+    int streamcopy_started;
 } MuxStream;
 
 typedef struct Muxer {
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 23/25] fftools/ffmpeg_mux: make copy_prior_start private to muxing code
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (20 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 22/25] fftools/ffmpeg_mux: make streamcopy_started " Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 24/25] fftools/ffmpeg_mux: make copy_initial_nonkeyframes " Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 25/25] fftools/ffmpeg_enc: make data_size_enc private to encoding code Anton Khirnov
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

It is no longer used outside of ffmpeg_mux*
---
 fftools/ffmpeg.h          | 1 -
 fftools/ffmpeg_mux.c      | 2 +-
 fftools/ffmpeg_mux.h      | 1 +
 fftools/ffmpeg_mux_init.c | 6 +++---
 4 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index cc72b1cb6e..25c33cf207 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -659,7 +659,6 @@ typedef struct OutputStream {
 
     const char *attachment_filename;
     int copy_initial_nonkeyframes;
-    int copy_prior_start;
 
     int keep_pix_fmt;
 
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 01ccac55ce..f52205cf30 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -398,7 +398,7 @@ void of_streamcopy(OutputStream *ost, const AVPacket *pkt, int64_t dts)
         return;
 
     if (!ms->streamcopy_started) {
-        if (!ost->copy_prior_start &&
+        if (!ms->copy_prior_start &&
             (pkt->pts == AV_NOPTS_VALUE ?
              dts < ms->ts_copy_start :
              pkt->pts < av_rescale_q(ms->ts_copy_start, AV_TIME_BASE_Q, pkt->time_base)))
diff --git a/fftools/ffmpeg_mux.h b/fftools/ffmpeg_mux.h
index 0938a261ee..81c4698161 100644
--- a/fftools/ffmpeg_mux.h
+++ b/fftools/ffmpeg_mux.h
@@ -75,6 +75,7 @@ typedef struct MuxStream {
     // combined size of all the packets sent to the muxer
     uint64_t data_size_mux;
 
+    int copy_prior_start;
     int streamcopy_started;
 } MuxStream;
 
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index 48efaef664..bfa84f5a28 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -908,7 +908,7 @@ static int streamcopy_init(const Muxer *mux, const OptionsContext *o,
     if (ost->st->duration <= 0 && ist->st->duration > 0)
         ost->st->duration = av_rescale_q(ist->st->duration, ist->st->time_base, ost->st->time_base);
 
-    if (!ost->copy_prior_start) {
+    if (!ms->copy_prior_start) {
         ms->ts_copy_start = (mux->of.start_time == AV_NOPTS_VALUE) ?
                             0 : mux->of.start_time;
         if (copy_ts && ifile->start_time != AV_NOPTS_VALUE) {
@@ -1156,8 +1156,8 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o,
         }
     }
 
-    ost->copy_prior_start = -1;
-    MATCH_PER_STREAM_OPT(copy_prior_start, i, ost->copy_prior_start, oc ,st);
+    ms->copy_prior_start = -1;
+    MATCH_PER_STREAM_OPT(copy_prior_start, i, ms->copy_prior_start, oc ,st);
 
     MATCH_PER_STREAM_OPT(bitstream_filters, str, bsfs, oc, st);
     if (bsfs && *bsfs) {
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 24/25] fftools/ffmpeg_mux: make copy_initial_nonkeyframes private to muxing code
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (21 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 23/25] fftools/ffmpeg_mux: make copy_prior_start " Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 25/25] fftools/ffmpeg_enc: make data_size_enc private to encoding code Anton Khirnov
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

It is no longer used outside of ffmpeg_mux*
---
 fftools/ffmpeg.h          | 1 -
 fftools/ffmpeg_mux.c      | 2 +-
 fftools/ffmpeg_mux.h      | 1 +
 fftools/ffmpeg_mux_init.c | 2 +-
 4 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 25c33cf207..494ea4abd3 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -658,7 +658,6 @@ typedef struct OutputStream {
     int inputs_done;
 
     const char *attachment_filename;
-    int copy_initial_nonkeyframes;
 
     int keep_pix_fmt;
 
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index f52205cf30..7778510d2c 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -394,7 +394,7 @@ void of_streamcopy(OutputStream *ost, const AVPacket *pkt, int64_t dts)
     }
 
     if (!ms->streamcopy_started && !(pkt->flags & AV_PKT_FLAG_KEY) &&
-        !ost->copy_initial_nonkeyframes)
+        !ms->copy_initial_nonkeyframes)
         return;
 
     if (!ms->streamcopy_started) {
diff --git a/fftools/ffmpeg_mux.h b/fftools/ffmpeg_mux.h
index 81c4698161..7e0454dfba 100644
--- a/fftools/ffmpeg_mux.h
+++ b/fftools/ffmpeg_mux.h
@@ -75,6 +75,7 @@ typedef struct MuxStream {
     // combined size of all the packets sent to the muxer
     uint64_t data_size_mux;
 
+    int copy_initial_nonkeyframes;
     int copy_prior_start;
     int streamcopy_started;
 } MuxStream;
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index bfa84f5a28..5b3606084e 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -1208,7 +1208,7 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o,
     ost->last_mux_dts = AV_NOPTS_VALUE;
 
     MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i,
-                         ost->copy_initial_nonkeyframes, oc, st);
+                         ms->copy_initial_nonkeyframes, oc, st);
 
     switch (type) {
     case AVMEDIA_TYPE_VIDEO:      new_stream_video     (mux, o, ost); break;
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [FFmpeg-devel] [PATCH 25/25] fftools/ffmpeg_enc: make data_size_enc private to encoding code
  2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
                   ` (22 preceding siblings ...)
  2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 24/25] fftools/ffmpeg_mux: make copy_initial_nonkeyframes " Anton Khirnov
@ 2023-04-13 14:12 ` Anton Khirnov
  23 siblings, 0 replies; 25+ messages in thread
From: Anton Khirnov @ 2023-04-13 14:12 UTC (permalink / raw)
  To: ffmpeg-devel

It is no longer used outside of ffmpeg_enc.c
---
 fftools/ffmpeg.h     |  2 --
 fftools/ffmpeg_enc.c | 14 ++++++++++----
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 494ea4abd3..a9302a95f0 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -662,8 +662,6 @@ typedef struct OutputStream {
     int keep_pix_fmt;
 
     /* stats */
-    // combined size of all the packets received from the encoder
-    uint64_t data_size_enc;
     // number of packets send to the muxer
     atomic_uint_least64_t packets_written;
     // number of frames/samples sent to the encoder
diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index 2462f53a82..45bf4b127f 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -56,6 +56,9 @@ struct Encoder {
     int64_t frames_prev_hist[3];
 
     AVFrame *sq_frame;
+
+    // combined size of all the packets received from the encoder
+    uint64_t data_size;
 };
 
 static uint64_t dup_warning = 1000;
@@ -513,6 +516,7 @@ void enc_stats_write(OutputStream *ost, EncStats *es,
                      const AVFrame *frame, const AVPacket *pkt,
                      uint64_t frame_num)
 {
+    Encoder      *e = ost->enc;
     AVIOContext *io = es->io;
     AVRational   tb = frame ? frame->time_base : pkt->time_base;
     int64_t     pts = frame ? frame->pts : pkt->pts;
@@ -564,7 +568,7 @@ void enc_stats_write(OutputStream *ost, EncStats *es,
             }
             case ENC_STATS_AVG_BITRATE: {
                 double duration = pkt->dts * av_q2d(tb);
-                avio_printf(io, "%g",  duration > 0 ? 8.0 * ost->data_size_enc / duration : -1.);
+                avio_printf(io, "%g",  duration > 0 ? 8.0 * e->data_size / duration : -1.);
                 continue;
             }
             default: av_assert0(0);
@@ -577,6 +581,7 @@ void enc_stats_write(OutputStream *ost, EncStats *es,
 
 static void update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
 {
+    Encoder        *e = ost->enc;
     const uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
                                                 NULL);
     AVCodecContext *enc = ost->enc_ctx;
@@ -624,14 +629,15 @@ static void update_video_stats(OutputStream *ost, const AVPacket *pkt, int write
         ti1 = 0.01;
 
     bitrate     = (pkt->size * 8) / av_q2d(enc->time_base) / 1000.0;
-    avg_bitrate = (double)(ost->data_size_enc * 8) / ti1 / 1000.0;
+    avg_bitrate = (double)(e->data_size * 8) / ti1 / 1000.0;
     fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
-           (double)ost->data_size_enc / 1024, ti1, bitrate, avg_bitrate);
+           (double)e->data_size / 1024, ti1, bitrate, avg_bitrate);
     fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(ost->pict_type));
 }
 
 static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
 {
+    Encoder            *e = ost->enc;
     AVCodecContext   *enc = ost->enc_ctx;
     AVPacket         *pkt = ost->pkt;
     const char *type_desc = av_get_media_type_string(enc->codec_type);
@@ -725,7 +731,7 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
             exit_program(1);
         }
 
-        ost->data_size_enc += pkt->size;
+        e->data_size += pkt->size;
 
         ost->packets_encoded++;
 
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2023-04-13 14:21 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-13 14:11 [FFmpeg-devel] [PATCH 01/25] fftools/ffmpeg_mux_init: move new_output_stream() lower in the file Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 02/25] fftools/ffmpeg_mux_init: restructure output stream creation Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 03/25] fftools/ffmpeg: move init_output_stream_streamcopy() to ffmpeg_mux_init Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 04/25] fftools/ffmpeg_mux_init: remove a redundant check Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 05/25] fftools/ffmpeg: move a miplaced assignment Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 06/25] fftools/ffmpeg: move a check to a more appropriate place Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 07/25] fftools/ffmpeg: drop unnecessary indirection Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 08/25] fftools/ffmpeg_mux_init: consolidate input stream flagging code Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 09/25] fftools/ffmpeg: add a function adding a destination filter for InputStream Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 10/25] fftools/ffmpeg: stop setting InputStream fields from muxing/filtering code Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 11/25] fftools/ffmpeg: move opening decoders to a new file Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 12/25] fftools/ffmpeg_dec: reindent after previous commit Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 13/25] fftools/ffmpeg_dec: drop useless abort_codec_experimental() Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 14/25] fftools/ffmpeg: open decoders right after we know they are needed Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 15/25] fftools/ffmpeg: initialize no-filter streams earlier Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 16/25] fftools/ffmpeg: store stream media type in OutputStream Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 17/25] fftools/ffmpeg: add muxer-input codec parameters to OutputStream Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 18/25] fftools/ffmpeg: move do_streamcopy() to ffmpeg_mux Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 19/25] fftools/ffmpeg_mux: use output stream parameters in of_streamcopy() Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 20/25] fftools/ffmpeg_mux: stop using filter_in_rescale_delta_last for streamcopy Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 21/25] fftools/ffmpeg_mux: make ts_copy_start private to muxing code Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 22/25] fftools/ffmpeg_mux: make streamcopy_started " Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 23/25] fftools/ffmpeg_mux: make copy_prior_start " Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 24/25] fftools/ffmpeg_mux: make copy_initial_nonkeyframes " Anton Khirnov
2023-04-13 14:12 ` [FFmpeg-devel] [PATCH 25/25] fftools/ffmpeg_enc: make data_size_enc private to encoding code Anton Khirnov

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git