fftools/ffmpeg_filter: compute input trim start/end in demuxer

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.
This commit is contained in:
Anton Khirnov 2024-02-13 12:01:11 +01:00
parent 09438d6529
commit 8f592eb35f
3 changed files with 32 additions and 26 deletions

View File

@ -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.

View File

@ -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;

View File

@ -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;