Commit b29b934e authored by Paul B Mahol's avatar Paul B Mahol

avfilter/f_interleave: add duration option

parent 6cfb33f9
...@@ -23316,6 +23316,21 @@ These filters accept the following options: ...@@ -23316,6 +23316,21 @@ These filters accept the following options:
@table @option @table @option
@item nb_inputs, n @item nb_inputs, n
Set the number of different inputs, it is 2 by default. Set the number of different inputs, it is 2 by default.
@item duration
How to determine the end-of-stream.
@table @option
@item longest
The duration of the longest input. (default)
@item shortest
The duration of the shortest input.
@item first
The duration of the first input.
@end table
@end table @end table
@subsection Examples @subsection Examples
......
...@@ -37,15 +37,25 @@ ...@@ -37,15 +37,25 @@
typedef struct InterleaveContext { typedef struct InterleaveContext {
const AVClass *class; const AVClass *class;
int nb_inputs; int nb_inputs;
int duration_mode;
int64_t pts; int64_t pts;
} InterleaveContext; } InterleaveContext;
#define DURATION_LONGEST 0
#define DURATION_SHORTEST 1
#define DURATION_FIRST 2
#define OFFSET(x) offsetof(InterleaveContext, x) #define OFFSET(x) offsetof(InterleaveContext, x)
#define DEFINE_OPTIONS(filt_name, flags_) \ #define DEFINE_OPTIONS(filt_name, flags_) \
static const AVOption filt_name##_options[] = { \ static const AVOption filt_name##_options[] = { \
{ "nb_inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \ { "nb_inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
{ "n", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \ { "n", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
{ "duration", "how to determine the end-of-stream", \
OFFSET(duration_mode), AV_OPT_TYPE_INT, { .i64 = DURATION_LONGEST }, 0, 2, flags_, "duration" }, \
{ "longest", "Duration of longest input", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_LONGEST }, 0, 0, flags_, "duration" }, \
{ "shortest", "Duration of shortest input", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_SHORTEST }, 0, 0, flags_, "duration" }, \
{ "first", "Duration of first input", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_FIRST }, 0, 0, flags_, "duration" }, \
{ NULL } \ { NULL } \
} }
...@@ -54,21 +64,26 @@ static int activate(AVFilterContext *ctx) ...@@ -54,21 +64,26 @@ static int activate(AVFilterContext *ctx)
AVFilterLink *outlink = ctx->outputs[0]; AVFilterLink *outlink = ctx->outputs[0];
InterleaveContext *s = ctx->priv; InterleaveContext *s = ctx->priv;
int64_t q_pts, pts = INT64_MAX; int64_t q_pts, pts = INT64_MAX;
int i, nb_eofs = 0, input_idx = -1; int i, nb_eofs = 0, input_idx = -1, nb_active_inputs = 0;
int nb_inputs_with_frames = 0;
FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx); FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
for (i = 0; i < ctx->nb_inputs; i++) { for (i = 0; i < ctx->nb_inputs; i++) {
if (!ff_outlink_get_status(ctx->inputs[i]) && if (!ff_outlink_get_status(ctx->inputs[i])) {
!ff_inlink_queued_frames(ctx->inputs[i])) nb_active_inputs++;
break; if (!ff_inlink_queued_frames(ctx->inputs[i]))
break;
nb_inputs_with_frames++;
}
} }
if (i == ctx->nb_inputs) { if (nb_active_inputs > 0 && nb_active_inputs == nb_inputs_with_frames) {
for (i = 0; i < ctx->nb_inputs; i++) { for (i = 0; i < ctx->nb_inputs; i++) {
AVFrame *frame; AVFrame *frame;
if (ff_outlink_get_status(ctx->inputs[i])) if (ff_outlink_get_status(ctx->inputs[i]) ||
ff_inlink_queued_frames(ctx->inputs[i]) == 0)
continue; continue;
frame = ff_inlink_peek_frame(ctx->inputs[i], 0); frame = ff_inlink_peek_frame(ctx->inputs[i], 0);
...@@ -104,6 +119,16 @@ static int activate(AVFilterContext *ctx) ...@@ -104,6 +119,16 @@ static int activate(AVFilterContext *ctx)
} }
} }
for (i = 0; i < ctx->nb_inputs; i++)
nb_eofs += !!ff_outlink_get_status(ctx->inputs[i]);
if ((nb_eofs > 0 && s->duration_mode == DURATION_SHORTEST) ||
(nb_eofs == ctx->nb_inputs && s->duration_mode == DURATION_LONGEST) ||
(ff_outlink_get_status(ctx->inputs[0]) && s->duration_mode == DURATION_FIRST)) {
ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
return 0;
}
for (i = 0; i < ctx->nb_inputs; i++) { for (i = 0; i < ctx->nb_inputs; i++) {
if (ff_inlink_queued_frames(ctx->inputs[i])) if (ff_inlink_queued_frames(ctx->inputs[i]))
continue; continue;
...@@ -112,12 +137,6 @@ static int activate(AVFilterContext *ctx) ...@@ -112,12 +137,6 @@ static int activate(AVFilterContext *ctx)
ff_inlink_request_frame(ctx->inputs[i]); ff_inlink_request_frame(ctx->inputs[i]);
return 0; return 0;
} }
nb_eofs++;
}
if (nb_eofs == ctx->nb_inputs) {
ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
return 0;
} }
return FFERROR_NOT_READY; return FFERROR_NOT_READY;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment