[FFmpeg-devel] [PATCH 02/10] fftools/ffmpeg_filter: compute input trim start/end in demuxer

Anton Khirnov anton at khirnov.net
Wed Feb 14 20:24:27 EET 2024


The computation is based on demuxer properties, so that is the more
appropriate place for it. Filter code just receives the desired
start time/duration.
---
 fftools/ffmpeg.h        | 11 +++++++----
 fftools/ffmpeg_demux.c  | 24 +++++++++++++++++++-----
 fftools/ffmpeg_filter.c | 23 ++++++-----------------
 3 files changed, 32 insertions(+), 26 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 33750e0bb3..cbc5413238 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -250,6 +250,11 @@ typedef struct OptionsContext {
     SpecifierOptList mux_stats_fmt;
 } OptionsContext;
 
+typedef struct InputFilterOptions {
+    int64_t             trim_start_us;
+    int64_t             trim_end_us;
+} InputFilterOptions;
+
 typedef struct InputFilter {
     struct FilterGraph *graph;
     uint8_t            *name;
@@ -394,15 +399,12 @@ typedef struct InputFile {
     int64_t          ts_offset;
     /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
     int64_t          start_time;
-    int64_t          recording_time;
 
     /* streams that ffmpeg is aware of;
      * there may be extra streams in ctx that are not mapped to an InputStream
      * if new streams appear dynamically during demuxing */
     InputStream    **streams;
     int           nb_streams;
-
-    int              accurate_seek;
 } InputFile;
 
 enum forced_keyframes_const {
@@ -790,7 +792,8 @@ int ifile_open(const OptionsContext *o, const char *filename, Scheduler *sch);
 void ifile_close(InputFile **f);
 
 int ist_output_add(InputStream *ist, OutputStream *ost);
-int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple);
+int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple,
+                   InputFilterOptions *opts);
 
 /**
  * Find an unused input stream of given type.
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index 3bf95e2c3f..abda4ad0d9 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -103,6 +103,9 @@ typedef struct Demuxer {
     int64_t ts_offset_discont;
     int64_t last_ts;
 
+    int64_t recording_time;
+    int accurate_seek;
+
     /* number of times input stream should be looped */
     int loop;
     int have_audio_dec;
@@ -450,13 +453,13 @@ static int input_packet_process(Demuxer *d, AVPacket *pkt, unsigned *send_flags)
     if (ret < 0)
         return ret;
 
-    if (f->recording_time != INT64_MAX) {
+    if (d->recording_time != INT64_MAX) {
         int64_t start_time = 0;
         if (copy_ts) {
             start_time += f->start_time != AV_NOPTS_VALUE ? f->start_time : 0;
             start_time += start_at_zero ? 0 : f->start_time_effective;
         }
-        if (ds->dts >= f->recording_time + start_time)
+        if (ds->dts >= d->recording_time + start_time)
             *send_flags |= DEMUX_SEND_STREAMCOPY_EOF;
     }
 
@@ -966,10 +969,12 @@ int ist_output_add(InputStream *ist, OutputStream *ost)
     return ost->enc ? ds->sch_idx_dec : ds->sch_idx_stream;
 }
 
-int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple)
+int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple,
+                   InputFilterOptions *opts)
 {
     Demuxer      *d = demuxer_from_ifile(ist->file);
     DemuxStream *ds = ds_from_ist(ist);
+    int64_t tsoffset = 0;
     int ret;
 
     ret = ist_use(ist, is_simple ? DECODING_FOR_OST : DECODING_FOR_FILTER);
@@ -995,6 +1000,15 @@ int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple)
         ds->have_sub2video = 1;
     }
 
+    if (copy_ts) {
+        tsoffset = d->f.start_time == AV_NOPTS_VALUE ? 0 : d->f.start_time;
+        if (!start_at_zero && d->f.ctx->start_time != AV_NOPTS_VALUE)
+            tsoffset += d->f.ctx->start_time;
+    }
+    opts->trim_start_us = ((d->f.start_time == AV_NOPTS_VALUE) || !d->accurate_seek) ?
+                          AV_NOPTS_VALUE : tsoffset;
+    opts->trim_end_us   = d->recording_time;
+
     return ds->sch_idx_dec;
 }
 
@@ -1722,11 +1736,11 @@ int ifile_open(const OptionsContext *o, const char *filename, Scheduler *sch)
     }
 
     f->start_time = start_time;
-    f->recording_time = recording_time;
+    d->recording_time = recording_time;
     f->input_sync_ref = o->input_sync_ref;
     f->input_ts_offset = o->input_ts_offset;
     f->ts_offset  = o->input_ts_offset - (copy_ts ? (start_at_zero && ic->start_time != AV_NOPTS_VALUE ? ic->start_time : 0) : timestamp);
-    f->accurate_seek = o->accurate_seek;
+    d->accurate_seek   = o->accurate_seek;
     d->loop = o->loop;
     d->nb_streams_warn = ic->nb_streams;
 
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index ed62e1d8ec..c411b882de 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -103,6 +103,8 @@ typedef struct FilterGraphThread {
 typedef struct InputFilterPriv {
     InputFilter ifilter;
 
+    InputFilterOptions opts;
+
     int              index;
 
     AVFilterContext *filter;
@@ -673,7 +675,8 @@ static int ifilter_bind_ist(InputFilter *ifilter, InputStream *ist)
     ifp->ist             = ist;
     ifp->type_src        = ist->st->codecpar->codec_type;
 
-    dec_idx = ist_filter_add(ist, ifilter, filtergraph_is_simple(ifilter->graph));
+    dec_idx = ist_filter_add(ist, ifilter, filtergraph_is_simple(ifilter->graph),
+                             &ifp->opts);
     if (dec_idx < 0)
         return dec_idx;
 
@@ -1478,7 +1481,6 @@ static int configure_input_video_filter(FilterGraph *fg, AVFilterGraph *graph,
     AVBPrint args;
     char name[255];
     int ret, pad_idx = 0;
-    int64_t tsoffset = 0;
     AVBufferSrcParameters *par = av_buffersrc_parameters_alloc();
     if (!par)
         return AVERROR(ENOMEM);
@@ -1558,13 +1560,7 @@ static int configure_input_video_filter(FilterGraph *fg, AVFilterGraph *graph,
 
     snprintf(name, sizeof(name), "trim_in_%d_%d",
              f->index, ist->index);
-    if (copy_ts) {
-        tsoffset = f->start_time == AV_NOPTS_VALUE ? 0 : f->start_time;
-        if (!start_at_zero && f->ctx->start_time != AV_NOPTS_VALUE)
-            tsoffset += f->ctx->start_time;
-    }
-    ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
-                      AV_NOPTS_VALUE : tsoffset, f->recording_time,
+    ret = insert_trim(ifp->opts.trim_start_us, ifp->opts.trim_end_us,
                       &last_filter, &pad_idx, name);
     if (ret < 0)
         return ret;
@@ -1589,7 +1585,6 @@ static int configure_input_audio_filter(FilterGraph *fg, AVFilterGraph *graph,
     AVBPrint args;
     char name[255];
     int ret, pad_idx = 0;
-    int64_t tsoffset = 0;
 
     ifp->time_base = (AVRational){ 1, ifp->sample_rate };
 
@@ -1615,13 +1610,7 @@ static int configure_input_audio_filter(FilterGraph *fg, AVFilterGraph *graph,
 
     snprintf(name, sizeof(name), "trim for input stream %d:%d",
              f->index, ist->index);
-    if (copy_ts) {
-        tsoffset = f->start_time == AV_NOPTS_VALUE ? 0 : f->start_time;
-        if (!start_at_zero && f->ctx->start_time != AV_NOPTS_VALUE)
-            tsoffset += f->ctx->start_time;
-    }
-    ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
-                      AV_NOPTS_VALUE : tsoffset, f->recording_time,
+    ret = insert_trim(ifp->opts.trim_start_us, ifp->opts.trim_end_us,
                       &last_filter, &pad_idx, name);
     if (ret < 0)
         return ret;
-- 
2.42.0



More information about the ffmpeg-devel mailing list