Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Anton Khirnov <anton@khirnov.net>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH 08/24] fftools/ffmpeg_filter: create Input/OutputFilters together with FilterGraph
Date: Sun, 28 May 2023 11:14:00 +0200
Message-ID: <20230528091416.17927-8-anton@khirnov.net> (raw)
In-Reply-To: <20230528091416.17927-1-anton@khirnov.net>

This way the list of filtergraph inputs/outputs is always known after
FilterGraph creation. This will allow treating simple and complex
filtergraphs in a more uniform manner.
---
 fftools/ffmpeg_filter.c | 157 +++++++++++++++++++---------------------
 1 file changed, 74 insertions(+), 83 deletions(-)

diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 5169a3ca82..d74eeef52a 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -62,6 +62,10 @@ typedef struct InputFilterPriv {
     // used to hold submitted input
     AVFrame *frame;
 
+    /* for filters that are not yet bound to an input stream,
+     * this stores the input linklabel, if any */
+    uint8_t *linklabel;
+
     // filter data type
     enum AVMediaType type;
     // source data type: AVMEDIA_TYPE_SUBTITLE for sub2video,
@@ -456,8 +460,6 @@ static int ifilter_bind_ist(InputFilter *ifilter, InputStream *ist)
 
     ifp->ist             = ist;
     ifp->type_src        = ist->st->codecpar->codec_type;
-    ifp->type            = ifp->type_src == AVMEDIA_TYPE_SUBTITLE ?
-                           AVMEDIA_TYPE_VIDEO : ifp->type_src;
 
     return 0;
 }
@@ -505,7 +507,7 @@ void fg_free(FilterGraph **pfg)
                 av_frame_free(&frame);
             av_fifo_freep2(&ifp->frame_queue);
         }
-        if (ist->sub2video.sub_queue) {
+        if (ist && ist->sub2video.sub_queue) {
             AVSubtitle sub;
             while (av_fifo_read(ist->sub2video.sub_queue, &sub, 1) >= 0)
                 avsubtitle_free(&sub);
@@ -517,6 +519,7 @@ void fg_free(FilterGraph **pfg)
         av_frame_free(&ifp->frame);
 
         av_buffer_unref(&ifp->hw_frames_ctx);
+        av_freep(&ifp->linklabel);
         av_freep(&ifilter->name);
         av_freep(&fg->inputs[j]);
     }
@@ -542,6 +545,10 @@ FilterGraph *fg_create(char *graph_desc)
     FilterGraphPriv *fgp = allocate_array_elem(&filtergraphs, sizeof(*fgp), &nb_filtergraphs);
     FilterGraph      *fg = &fgp->fg;
 
+    AVFilterInOut *inputs, *outputs;
+    AVFilterGraph *graph;
+    int ret = 0;
+
     fg->index      = nb_filtergraphs - 1;
     fgp->graph_desc = graph_desc;
 
@@ -549,6 +556,48 @@ FilterGraph *fg_create(char *graph_desc)
     if (!fgp->frame)
         report_and_exit(AVERROR(ENOMEM));
 
+    /* this graph is only used for determining the kinds of inputs
+     * and outputs we have, and is discarded on exit from this function */
+    graph = avfilter_graph_alloc();
+    if (!graph)
+        report_and_exit(AVERROR(ENOMEM));
+    graph->nb_threads = 1;
+
+    ret = graph_parse(graph, fgp->graph_desc, &inputs, &outputs, NULL);
+    if (ret < 0)
+        goto fail;
+
+    for (AVFilterInOut *cur = inputs; cur; cur = cur->next) {
+        InputFilter *const ifilter = ifilter_alloc(fg);
+        InputFilterPriv       *ifp = ifp_from_ifilter(ifilter);
+
+        ifp->linklabel = cur->name;
+        cur->name      = NULL;
+
+        ifp->type      = avfilter_pad_get_type(cur->filter_ctx->input_pads,
+                                               cur->pad_idx);
+        ifilter->name  = describe_filter_link(fg, cur, 1);
+    }
+
+    for (AVFilterInOut *cur = outputs; cur; cur = cur->next) {
+        OutputFilter *const ofilter = ofilter_alloc(fg);
+
+        ofilter->linklabel = cur->name;
+        cur->name          = NULL;
+
+        ofilter->type      = avfilter_pad_get_type(cur->filter_ctx->output_pads,
+                                                   cur->pad_idx);
+        ofilter->name      = describe_filter_link(fg, cur, 0);
+    }
+
+fail:
+    avfilter_inout_free(&inputs);
+    avfilter_inout_free(&outputs);
+    avfilter_graph_free(&graph);
+
+    if (ret < 0)
+        report_and_exit(ret);
+
     return fg;
 }
 
@@ -557,8 +606,6 @@ int init_simple_filtergraph(InputStream *ist, OutputStream *ost,
 {
     FilterGraph *fg;
     FilterGraphPriv *fgp;
-    OutputFilter *ofilter;
-    InputFilter  *ifilter;
     int ret;
 
     fg = fg_create(graph_desc);
@@ -568,26 +615,32 @@ int init_simple_filtergraph(InputStream *ist, OutputStream *ost,
 
     fgp->is_simple = 1;
 
-    ofilter      = ofilter_alloc(fg);
-    ofilter->ost = ost;
+    if (fg->nb_inputs != 1 || fg->nb_outputs != 1) {
+        av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' was expected "
+               "to have exactly 1 input and 1 output. "
+               "However, it had %d input(s) and %d output(s). Please adjust, "
+               "or use a complex filtergraph (-filter_complex) instead.\n",
+               graph_desc, fg->nb_inputs, fg->nb_outputs);
+        return AVERROR(EINVAL);
+    }
 
-    ost->filter = ofilter;
+    fg->outputs[0]->ost = ost;
 
-    ifilter = ifilter_alloc(fg);
+    ost->filter = fg->outputs[0];
 
-    ret = ifilter_bind_ist(ifilter, ist);
+    ret = ifilter_bind_ist(fg->inputs[0], ist);
     if (ret < 0)
         return ret;
 
     return 0;
 }
 
-static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
+static void init_input_filter(FilterGraph *fg, InputFilter *ifilter)
 {
     FilterGraphPriv *fgp = fgp_from_fg(fg);
+    InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
     InputStream *ist = NULL;
-    enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
-    InputFilter *ifilter;
+    enum AVMediaType type = ifp->type;
     int i, ret;
 
     // TODO: support other filter types
@@ -597,11 +650,11 @@ static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
         exit_program(1);
     }
 
-    if (in->name) {
+    if (ifp->linklabel) {
         AVFormatContext *s;
         AVStream       *st = NULL;
         char *p;
-        int file_idx = strtol(in->name, &p, 0);
+        int file_idx = strtol(ifp->linklabel, &p, 0);
 
         if (file_idx < 0 || file_idx >= nb_input_files) {
             av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
@@ -631,63 +684,27 @@ static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
         ist = ist_find_unused(type);
         if (!ist) {
             av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
-                   "unlabeled input pad %d on filter %s\n", in->pad_idx,
-                   in->filter_ctx->name);
+                   "unlabeled input pad %s\n", ifilter->name);
             exit_program(1);
         }
     }
     av_assert0(ist);
 
-    ifilter         = ifilter_alloc(fg);
-    ifilter->name   = describe_filter_link(fg, in, 1);
-
     ret = ifilter_bind_ist(ifilter, ist);
     if (ret < 0) {
         av_log(NULL, AV_LOG_ERROR,
                "Error binding an input stream to complex filtergraph input %s.\n",
-               in->name ? in->name : "");
+               ifilter->name);
         exit_program(1);
     }
 }
 
 int init_complex_filtergraph(FilterGraph *fg)
 {
-    FilterGraphPriv *fgp = fgp_from_fg(fg);
-    AVFilterInOut *inputs, *outputs, *cur;
-    AVFilterGraph *graph;
-    int ret = 0;
-
-    /* this graph is only used for determining the kinds of inputs
-     * and outputs we have, and is discarded on exit from this function */
-    graph = avfilter_graph_alloc();
-    if (!graph)
-        return AVERROR(ENOMEM);
-    graph->nb_threads = 1;
-
-    ret = graph_parse(graph, fgp->graph_desc, &inputs, &outputs, NULL);
-    if (ret < 0)
-        goto fail;
-
-    for (cur = inputs; cur; cur = cur->next)
-        init_input_filter(fg, cur);
-
-    for (cur = outputs; cur;) {
-        OutputFilter *const ofilter = ofilter_alloc(fg);
-
-        ofilter->linklabel = cur->name;
-        cur->name          = NULL;
-
-        ofilter->type    = avfilter_pad_get_type(cur->filter_ctx->output_pads,
-                                                                         cur->pad_idx);
-        ofilter->name    = describe_filter_link(fg, cur, 0);
-        cur = cur->next;
-    }
-
-fail:
-    avfilter_inout_free(&inputs);
-    avfilter_inout_free(&outputs);
-    avfilter_graph_free(&graph);
-    return ret;
+    // bind filtergraph inputs to input streams
+    for (int i = 0; i < fg->nb_inputs; i++)
+        init_input_filter(fg, fg->inputs[i]);
+    return 0;
 }
 
 static int insert_trim(int64_t start_time, int64_t duration,
@@ -1324,32 +1341,6 @@ int configure_filtergraph(FilterGraph *fg)
     if ((ret = graph_parse(fg->graph, graph_desc, &inputs, &outputs, hw_device)) < 0)
         goto fail;
 
-    if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
-        const char *num_inputs;
-        const char *num_outputs;
-        if (!outputs) {
-            num_outputs = "0";
-        } else if (outputs->next) {
-            num_outputs = ">1";
-        } else {
-            num_outputs = "1";
-        }
-        if (!inputs) {
-            num_inputs = "0";
-        } else if (inputs->next) {
-            num_inputs = ">1";
-        } else {
-            num_inputs = "1";
-        }
-        av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' was expected "
-               "to have exactly 1 input and 1 output."
-               " However, it had %s input(s) and %s output(s)."
-               " Please adjust, or use a complex filtergraph (-filter_complex) instead.\n",
-               graph_desc, num_inputs, num_outputs);
-        ret = AVERROR(EINVAL);
-        goto fail;
-    }
-
     for (cur = inputs, i = 0; cur; cur = cur->next, i++)
         if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0) {
             avfilter_inout_free(&inputs);
-- 
2.40.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".

  parent reply	other threads:[~2023-05-28  9:18 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-28  9:13 [FFmpeg-devel] [PATCH 01/24] fftools/ffmpeg_mux_init: merge ost_add_from_filter() to ost_add() Anton Khirnov
2023-05-28  9:13 ` [FFmpeg-devel] [PATCH 02/24] fftools/ffmpeg: add logging for creating output streams Anton Khirnov
2023-05-28  9:13 ` [FFmpeg-devel] [PATCH 03/24] fftools/ffmpeg_filter: use a dedicated variable for marking simple filtergraphs Anton Khirnov
2023-05-28  9:13 ` [FFmpeg-devel] [PATCH 04/24] fftools/ffmpeg_filter: always pass graph description to fg_create() Anton Khirnov
2023-05-28  9:13 ` [FFmpeg-devel] [PATCH 05/24] fftools/ffmpeg_filter: store just the link label in OutputFilter Anton Khirnov
2023-05-28  9:13 ` [FFmpeg-devel] [PATCH 06/24] fftools/ffmpeg_filter: decouple allocating InputFilter and binding it to InputStream Anton Khirnov
2023-05-28  9:13 ` [FFmpeg-devel] [PATCH 07/24] fftools/ffmpeg_filter: move some functions higher up Anton Khirnov
2023-05-28  9:14 ` Anton Khirnov [this message]
2023-05-28  9:14 ` [FFmpeg-devel] [PATCH 09/24] fftools/ffmpeg_filter: factor out binding an output stream to OutputFilter Anton Khirnov
2023-05-28  9:14 ` [FFmpeg-devel] [PATCH 10/24] fftools/ffmpeg_mux_init: move OutputFilter setup code to ffmpeg_filter Anton Khirnov
2023-05-28  9:14 ` [FFmpeg-devel] [PATCH 11/24] fftools/ffmpeg_filter: try to configure filtergraphs earlier Anton Khirnov
2023-05-28  9:14 ` [FFmpeg-devel] [PATCH 12/24] fftools/ffmpeg: constify AVSubtitle parameters as appropriate Anton Khirnov
2023-05-28  9:14 ` [FFmpeg-devel] [PATCH 13/24] fftools/ffmpeg_dec: move sub2video submission to ffmpeg_filter Anton Khirnov
2023-05-28  9:14 ` [FFmpeg-devel] [PATCH 14/24] fftools/ffmpeg_filter: move sub2video subtitle queue to InputFilterPriv Anton Khirnov
2023-05-28  9:14 ` [FFmpeg-devel] [PATCH 15/24] fftools/ffmpeg: tweak sub2video_heartbeat() arguments Anton Khirnov
2023-05-28  9:14 ` [FFmpeg-devel] [PATCH 16/24] fftools/ffmpeg: rework setting sub2video parameters Anton Khirnov
2023-05-28 19:43   ` Michael Niedermayer
2023-05-29 12:49     ` [FFmpeg-devel] [PATCH] " Anton Khirnov
2023-05-31 17:02       ` Michael Niedermayer
2023-05-28  9:14 ` [FFmpeg-devel] [PATCH 17/24] fftools/ffmpeg: move sub2video handling to ffmpeg_filter Anton Khirnov
2023-05-28  9:14 ` [FFmpeg-devel] [PATCH 18/24] fftools/ffmpeg_enc: stop configuring filters from encoder flush Anton Khirnov
2023-05-28  9:14 ` [FFmpeg-devel] [PATCH 19/24] fftools/ffmpeg_filter: drop unreachable code Anton Khirnov
2023-05-28  9:14 ` [FFmpeg-devel] [PATCH 20/24] fftools/ffmpeg_filter: make ifilter_has_all_input_formats() static Anton Khirnov
2023-05-28  9:14 ` [FFmpeg-devel] [PATCH 21/24] fftools/ffmpeg_filter: make InputStream.filter private Anton Khirnov
2023-05-28  9:14 ` [FFmpeg-devel] [PATCH 22/24] fftools/ffmpeg_filter: constify the argument of filtergraph_is_simple() Anton Khirnov
2023-05-28  9:14 ` [FFmpeg-devel] [PATCH 23/24] fftools/ffmpeg_mux: flush bsfs immediately on exceeding recoding time Anton Khirnov
2023-05-28  9:14 ` [FFmpeg-devel] [PATCH 24/24] fftools/ffmpeg_filter: do not flush encoders on parameter change Anton Khirnov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230528091416.17927-8-anton@khirnov.net \
    --to=anton@khirnov.net \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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