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 13/18] fftools/ffmpeg_sched: allow encoders to send to multiple destinations
Date: Wed,  6 Mar 2024 12:03:14 +0100
Message-ID: <20240306110319.17339-13-anton@khirnov.net> (raw)
In-Reply-To: <20240306110319.17339-1-anton@khirnov.net>

Will become useful in following commits.
---
 fftools/ffmpeg_sched.c | 117 ++++++++++++++++++++++++++++++++++-------
 1 file changed, 98 insertions(+), 19 deletions(-)

diff --git a/fftools/ffmpeg_sched.c b/fftools/ffmpeg_sched.c
index cf9b0c836e..e5435dd866 100644
--- a/fftools/ffmpeg_sched.c
+++ b/fftools/ffmpeg_sched.c
@@ -104,7 +104,9 @@ typedef struct SchEnc {
     const AVClass      *class;
 
     SchedulerNode       src;
-    SchedulerNode       dst;
+    SchedulerNode      *dst;
+    uint8_t            *dst_finished;
+    unsigned         nb_dst;
 
     // [0] - index of the sync queue in Scheduler.sq_enc,
     // [1] - index of this encoder in the sq
@@ -134,7 +136,9 @@ typedef struct SchEnc {
     ThreadQueue        *queue;
     // tq_send() to queue returned EOF
     int                 in_finished;
-    int                 out_finished;
+
+    // temporary storage used by sch_enc_send()
+    AVPacket           *send_pkt;
 } SchEnc;
 
 typedef struct SchDemuxStream {
@@ -569,6 +573,11 @@ void sch_free(Scheduler **psch)
         SchEnc *enc = &sch->enc[i];
 
         tq_free(&enc->queue);
+
+        av_packet_free(&enc->send_pkt);
+
+        av_freep(&enc->dst);
+        av_freep(&enc->dst_finished);
     }
     av_freep(&sch->enc);
 
@@ -819,6 +828,10 @@ int sch_add_enc(Scheduler *sch, SchThreadFunc func, void *ctx,
 
     task_init(sch, &enc->task, SCH_NODE_TYPE_ENC, idx, func, ctx);
 
+    enc->send_pkt = av_packet_alloc();
+    if (!enc->send_pkt)
+        return AVERROR(ENOMEM);
+
     ret = queue_alloc(&enc->queue, 1, 0, QUEUE_FRAMES);
     if (ret < 0)
         return ret;
@@ -1048,9 +1061,14 @@ int sch_connect(Scheduler *sch, SchedulerNode src, SchedulerNode dst)
         enc = &sch->enc[src.idx];
         ms  = &sch->mux[dst.idx].streams[dst.idx_stream];
 
-        av_assert0(!enc->dst.type && !ms->src.type);
-        enc->dst = dst;
-        ms->src  = src;
+        av_assert0(!ms->src.type);
+
+        ret = GROW_ARRAY(enc->dst, enc->nb_dst);
+        if (ret < 0)
+            return ret;
+
+        enc->dst[enc->nb_dst - 1] = dst;
+        ms->src                   = src;
 
         break;
         }
@@ -1339,12 +1357,16 @@ int sch_start(Scheduler *sch)
                    "Encoder not connected to a source\n");
             return AVERROR(EINVAL);
         }
-        if (!enc->dst.type) {
+        if (!enc->nb_dst) {
             av_log(enc, AV_LOG_ERROR,
-                   "Encoder not connected to a sink\n");
+                   "Encoder not connected to any sink\n");
             return AVERROR(EINVAL);
         }
 
+        enc->dst_finished = av_calloc(enc->nb_dst, sizeof(*enc->dst_finished));
+        if (!enc->dst_finished)
+            return AVERROR(ENOMEM);
+
         ret = task_start(&enc->task);
         if (ret < 0)
             return ret;
@@ -1518,15 +1540,21 @@ static int send_to_enc_sq(Scheduler *sch, SchEnc *enc, AVFrame *frame)
     // TODO: consider a cleaner way of passing this information through
     //       the pipeline
     if (!frame) {
-        SchMux      *mux = &sch->mux[enc->dst.idx];
-        SchMuxStream *ms = &mux->streams[enc->dst.idx_stream];
+        for (unsigned i = 0; i < enc->nb_dst; i++) {
+            SchMux      *mux;
+            SchMuxStream *ms;
 
-        pthread_mutex_lock(&sch->schedule_lock);
 
-        ms->source_finished = 1;
-        schedule_update_locked(sch);
+            mux = &sch->mux[enc->dst[i].idx];
+            ms = &mux->streams[enc->dst[i].idx_stream];
 
-        pthread_mutex_unlock(&sch->schedule_lock);
+            pthread_mutex_lock(&sch->schedule_lock);
+
+            ms->source_finished = 1;
+            schedule_update_locked(sch);
+
+            pthread_mutex_unlock(&sch->schedule_lock);
+        }
     }
 
     pthread_mutex_lock(&sq->lock);
@@ -2080,20 +2108,64 @@ int sch_enc_receive(Scheduler *sch, unsigned enc_idx, AVFrame *frame)
     return ret;
 }
 
+static int enc_send_to_dst(Scheduler *sch, const SchedulerNode dst,
+                           uint8_t *dst_finished, AVPacket *pkt)
+{
+    int ret;
+
+    if (*dst_finished)
+        return AVERROR_EOF;
+
+    if (!pkt)
+        goto finish;
+
+    ret = send_to_mux(sch, &sch->mux[dst.idx], dst.idx_stream, pkt);
+    if (ret == AVERROR_EOF)
+        goto finish;
+
+    return ret;
+
+finish:
+    send_to_mux(sch, &sch->mux[dst.idx], dst.idx_stream, NULL);
+
+    *dst_finished = 1;
+
+    return AVERROR_EOF;
+}
+
 int sch_enc_send(Scheduler *sch, unsigned enc_idx, AVPacket *pkt)
 {
     SchEnc *enc;
     int ret;
+    unsigned nb_done = 0;
 
     av_assert0(enc_idx < sch->nb_enc);
     enc = &sch->enc[enc_idx];
 
-    if (enc->out_finished)
-        return pkt ? AVERROR_EOF : 0;
+    for (unsigned i = 0; i < enc->nb_dst; i++) {
+        uint8_t *finished = &enc->dst_finished[i];
+        AVPacket *to_send = pkt;
 
-    ret = send_to_mux(sch, &sch->mux[enc->dst.idx], enc->dst.idx_stream, pkt);
-    if (ret < 0)
-        enc->out_finished = 1;
+        // sending a packet consumes it, so make a temporary reference if needed
+        if (i < enc->nb_dst - 1) {
+            to_send = enc->send_pkt;
+
+            ret = av_packet_ref(to_send, pkt);
+            if (ret < 0)
+                return ret;
+        }
+
+        ret = enc_send_to_dst(sch, enc->dst[i], finished, to_send);
+        if (ret < 0) {
+            av_packet_unref(to_send);
+            if (ret == AVERROR_EOF) {
+                nb_done++;
+                ret = 0;
+                continue;
+            }
+            return ret;
+        }
+    }
 
     return ret;
 }
@@ -2101,10 +2173,17 @@ int sch_enc_send(Scheduler *sch, unsigned enc_idx, AVPacket *pkt)
 static int enc_done(Scheduler *sch, unsigned enc_idx)
 {
     SchEnc *enc = &sch->enc[enc_idx];
+    int ret = 0;
 
     tq_receive_finish(enc->queue, 0);
 
-    return send_to_mux(sch, &sch->mux[enc->dst.idx], enc->dst.idx_stream, NULL);
+    for (unsigned i = 0; i < enc->nb_dst; i++) {
+        int err = enc_send_to_dst(sch, enc->dst[i], &enc->dst_finished[i], NULL);
+        if (err < 0 && err != AVERROR_EOF)
+            ret = err_merge(ret, err);
+    }
+
+    return ret;
 }
 
 int sch_filter_receive(Scheduler *sch, unsigned fg_idx,
-- 
2.43.0

_______________________________________________
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:[~2024-03-06 11:06 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-06 11:03 [FFmpeg-devel] [PATCH 01/18] fftools/cmdutils: fix printing group name in split_commandline() Anton Khirnov
2024-03-06 11:03 ` [FFmpeg-devel] [PATCH 02/18] fftools/ffmpeg_filter: refactor setting input timebase Anton Khirnov
2024-03-07 20:37   ` Michael Niedermayer
2024-03-08  5:34     ` Anton Khirnov
2024-03-10  3:36       ` Michael Niedermayer
2024-03-10  6:13         ` Anton Khirnov
2024-03-10 19:21           ` Michael Niedermayer
2024-03-10 22:24             ` Anton Khirnov
2024-03-10 22:29               ` James Almer
2024-03-10 22:49                 ` Anton Khirnov
2024-03-10 23:37                   ` Michael Niedermayer
2024-03-11  6:03                     ` Anton Khirnov
2024-03-11 10:12                   ` Tobias Rapp
2024-03-11 12:23                     ` Anton Khirnov
2024-03-11 12:29                       ` Martin Storsjö
2024-03-11 13:28                         ` Anton Khirnov
2024-03-11 14:03                           ` Martin Storsjö via ffmpeg-devel
2024-03-11 14:32                             ` Anton Khirnov
2024-03-11 14:37                               ` Martin Storsjö
2024-03-11 14:31                           ` Tobias Rapp
2024-03-06 11:03 ` [FFmpeg-devel] [PATCH 03/18] fftools/ffmpeg_filter: drop unused InputFilterPriv.ist Anton Khirnov
2024-03-06 11:03 ` [FFmpeg-devel] [PATCH 04/18] fftools/ffmpeg_dec: move scheduler registration from dec_open() to dec_alloc() Anton Khirnov
2024-03-06 11:03 ` [FFmpeg-devel] [PATCH 05/18] fftools/ffmpeg_dec: factor opening the decoder out of dec_open() Anton Khirnov
2024-03-06 11:03 ` [FFmpeg-devel] [PATCH 06/18] fftools/ffmpeg_opt: merge init_complex_filters() and check_filter_outputs() Anton Khirnov
2024-03-06 11:03 ` [FFmpeg-devel] [PATCH 07/18] fftools/ffmpeg_filter: move filtergraph input type check to a better place Anton Khirnov
2024-03-06 11:03 ` [FFmpeg-devel] [PATCH 08/18] fftools/ffmpeg_filter: add logging for binding inputs to demuxer streams Anton Khirnov
2024-03-06 11:03 ` [FFmpeg-devel] [PATCH 09/18] fftools/ffmpeg: simplify propagating fallback parameters from decoders to filters Anton Khirnov
2024-03-06 11:03 ` [FFmpeg-devel] [PATCH 10/18] fftools/ffmpeg: remove unncessary casts for *_thread() return values Anton Khirnov
2024-03-06 11:03 ` [FFmpeg-devel] [PATCH 11/18] fftools/ffmpeg_enc: drop unnecessary parameter from forced_kf_apply() Anton Khirnov
2024-03-06 11:03 ` [FFmpeg-devel] [PATCH 12/18] fftools/ffmpeg_enc: merge do_{audio, video}_out into frame_encode() Anton Khirnov
2024-03-06 11:03 ` Anton Khirnov [this message]
2024-03-06 11:03 ` [FFmpeg-devel] [PATCH 14/18] fftools/ffmpeg_sched: factor initializing nodes into separate function Anton Khirnov
2024-03-06 11:03 ` [FFmpeg-devel] [PATCH 15/18] fftools/ffmpeg_sched: allow connecting encoder output to decoders Anton Khirnov
2024-03-06 11:03 ` [FFmpeg-devel] [PATCH 16/18] fftools/ffmpeg: prepare FrameData for having allocated fields Anton Khirnov
2024-03-06 11:03 ` [FFmpeg-devel] [PATCH 17/18] fftools/ffmpeg: add loopback decoding Anton Khirnov
2024-03-07 14:42   ` [FFmpeg-devel] [PATCH v2 " Anton Khirnov
2024-03-06 11:03 ` [FFmpeg-devel] [PATCH 18/18] fftools/ffmpeg_enc: set AV_PKT_FLAG_TRUSTED on encoded packets Anton Khirnov
2024-03-09 19:08 ` [FFmpeg-devel] [PATCH 01/18] fftools/cmdutils: fix printing group name in split_commandline() 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=20240306110319.17339-13-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