From patchwork Sat Nov 4 07:56:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 44523 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3aa6:b0:181:818d:5e7f with SMTP id d38csp336997pzh; Sat, 4 Nov 2023 02:25:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IELPhKmbFv5BZBb87p/3T4GF7wsGhAK8z53qOMDEnh/Y9NT/JFFyALnoqBs2If8oifa+Sy0 X-Received: by 2002:a17:907:934c:b0:9bf:b6f5:3a08 with SMTP id bv12-20020a170907934c00b009bfb6f53a08mr9110914ejc.52.1699089914173; Sat, 04 Nov 2023 02:25:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1699089914; cv=none; d=google.com; s=arc-20160816; b=pHA4zW2EYNGntpSkiHTPhsUDtV7xWG4hij7J1yEqjjQuq8YRQfdavXp39HHqbe38vV nU9B1PmdEYZApOUTSU81rMOh6FJa7pXiBrGKE9Bbr1oBo0Bmp8HrcCmP+wrOKL4i03UK QjNDlW0zpJPonq1CeulRPQHu6myzMjLDjHr+E8CyFqpcYoYZuf+KOjQQrU4oO9kyWAnR Pd/tj/mW24BAjqXQlF0qN3rBBtH0DYaPbd5kefUozspuBLL3FBFZ/XnGtd9oct7nnHsF gBdKn594r+qOh6/MvSpejyaWnzNxRSgnmhBLzKbZGFq+z2/R4yTPCWBEFgvTXYIjWSv0 bc8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=J0GJsQs51LHWKWhsfQg0/KwRsOiKRttB/wlYm6iy7wI=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=brUG0UKopkA+AqpMfrH7HheT5a3UUxV5pmdhGF1V9GgEoDoHsgzhtH4RpdAHwj30b3 xLJB89brRFntQdx7/tzaaZ2UsxZQQ7XbVsV14sHtDC3e4PiJXl6wAS+NdARcXvHPQM4W nJodt71Q70gTjBAigw3vs7zEyLuUIhQSAC5GdL0YR20eo+5qBleO9w+7HsMhZzAe4KOd lPeWPYG80S+QhamR5n7C8MKW8bfwwo/ZBnYJNWbRuTNZQL0NU/RUfm2wbI5UmLithQ87 g+7v3nL8N169Z4Znrb51+KDaa9GR+Bk23VhdE2RA7xlKUIeNU1CeF7mwhKXeXuCR0HJn O6rQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id qw7-20020a1709066a0700b009bf6c3aeaebsi2121063ejc.164.2023.11.04.02.25.13; Sat, 04 Nov 2023 02:25:14 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6083D68CEC6; Sat, 4 Nov 2023 11:22:17 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail1.khirnov.net (quelana.khirnov.net [94.230.150.81]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8943368CE44 for ; Sat, 4 Nov 2023 11:21:55 +0200 (EET) Received: from localhost (mail1.khirnov.net [IPv6:::1]) by mail1.khirnov.net (Postfix) with ESMTP id 5DE7D1372 for ; Sat, 4 Nov 2023 10:21:53 +0100 (CET) Received: from mail1.khirnov.net ([IPv6:::1]) by localhost (mail1.khirnov.net [IPv6:::1]) (amavis, port 10024) with ESMTP id ZGvGCy4Bpqz5 for ; Sat, 4 Nov 2023 10:21:53 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail1.khirnov.net (Postfix) with ESMTPS id 0EB2A14E4 for ; Sat, 4 Nov 2023 10:21:47 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id B285B3A1249 for ; Sat, 4 Nov 2023 10:21:40 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Sat, 4 Nov 2023 08:56:23 +0100 Message-ID: <20231104092125.10213-15-anton@khirnov.net> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231104092125.10213-1-anton@khirnov.net> References: <20231104092125.10213-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 14/24] fftools/ffmpeg_mux: move bitstream filtering to the muxer thread X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: b85ruMrJFL4T This will be the appropriate place for it after the rest of transcoding is switched to a threaded architecture. --- fftools/ffmpeg_mux.c | 112 ++++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 45 deletions(-) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 82352b7981..57fb8a8413 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -207,6 +207,67 @@ static int sync_queue_process(Muxer *mux, OutputStream *ost, AVPacket *pkt, int return 0; } +/* apply the output bitstream filters */ +static int mux_packet_filter(Muxer *mux, OutputStream *ost, + AVPacket *pkt, int *stream_eof) +{ + MuxStream *ms = ms_from_ost(ost); + const char *err_msg; + int ret = 0; + + if (ms->bsf_ctx) { + int bsf_eof = 0; + + if (pkt) + av_packet_rescale_ts(pkt, pkt->time_base, ms->bsf_ctx->time_base_in); + + ret = av_bsf_send_packet(ms->bsf_ctx, pkt); + if (ret < 0) { + err_msg = "submitting a packet for bitstream filtering"; + goto fail; + } + + while (!bsf_eof) { + ret = av_bsf_receive_packet(ms->bsf_ctx, ms->bsf_pkt); + if (ret == AVERROR(EAGAIN)) + return 0; + else if (ret == AVERROR_EOF) + bsf_eof = 1; + else if (ret < 0) { + av_log(ost, AV_LOG_ERROR, + "Error applying bitstream filters to a packet: %s", + av_err2str(ret)); + if (exit_on_error) + return ret; + continue; + } + + if (!bsf_eof) + ms->bsf_pkt->time_base = ms->bsf_ctx->time_base_out; + + ret = sync_queue_process(mux, ost, bsf_eof ? NULL : ms->bsf_pkt, stream_eof); + if (ret < 0) + goto mux_fail; + } + *stream_eof = 1; + return AVERROR_EOF; + } else { + ret = sync_queue_process(mux, ost, pkt, stream_eof); + if (ret < 0) + goto mux_fail; + } + + return 0; + +mux_fail: + err_msg = "submitting a packet to the muxer"; + +fail: + if (ret != AVERROR_EOF) + av_log(ost, AV_LOG_ERROR, "Error %s: %s\n", err_msg, av_err2str(ret)); + return ret; +} + static void thread_set_name(OutputFile *of) { char name[16]; @@ -263,7 +324,7 @@ static void *muxer_thread(void *arg) } ost = of->streams[stream_idx]; - ret = sync_queue_process(mux, ost, ret < 0 ? NULL : mt.pkt, &stream_eof); + ret = mux_packet_filter(mux, ost, ret < 0 ? NULL : mt.pkt, &stream_eof); av_packet_unref(mt.pkt); if (ret == AVERROR_EOF) { if (stream_eof) { @@ -376,58 +437,19 @@ static int submit_packet(Muxer *mux, AVPacket *pkt, OutputStream *ost) int of_output_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) { Muxer *mux = mux_from_of(of); - MuxStream *ms = ms_from_ost(ost); - const char *err_msg; int ret = 0; if (pkt && pkt->dts != AV_NOPTS_VALUE) ost->last_mux_dts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q); - /* apply the output bitstream filters */ - if (ms->bsf_ctx) { - int bsf_eof = 0; - - if (pkt) - av_packet_rescale_ts(pkt, pkt->time_base, ms->bsf_ctx->time_base_in); - - ret = av_bsf_send_packet(ms->bsf_ctx, pkt); - if (ret < 0) { - err_msg = "submitting a packet for bitstream filtering"; - goto fail; - } - - while (!bsf_eof) { - ret = av_bsf_receive_packet(ms->bsf_ctx, ms->bsf_pkt); - if (ret == AVERROR(EAGAIN)) - return 0; - else if (ret == AVERROR_EOF) - bsf_eof = 1; - else if (ret < 0) { - err_msg = "applying bitstream filters to a packet"; - goto fail; - } - - if (!bsf_eof) - ms->bsf_pkt->time_base = ms->bsf_ctx->time_base_out; - - ret = submit_packet(mux, bsf_eof ? NULL : ms->bsf_pkt, ost); - if (ret < 0) - goto mux_fail; - } - } else { - ret = submit_packet(mux, pkt, ost); - if (ret < 0) - goto mux_fail; + ret = submit_packet(mux, pkt, ost); + if (ret < 0) { + av_log(ost, AV_LOG_ERROR, "Error submitting a packet to the muxer: %s", + av_err2str(ret)); + return ret; } return 0; - -mux_fail: - err_msg = "submitting a packet to the muxer"; - -fail: - av_log(ost, AV_LOG_ERROR, "Error %s\n", err_msg); - return exit_on_error ? ret : 0; } int of_streamcopy(OutputStream *ost, const AVPacket *pkt, int64_t dts)