From patchwork Thu Oct 13 13:48:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 38717 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:4a86:b0:9d:28a3:170e with SMTP id fn6csp348753pzb; Thu, 13 Oct 2022 06:49:39 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6lflXb/4P7vZ8WxpjKAVeh4rNi7B3hVcPXMugZYKT9yxDBxTixUo4k8fJlTxWMFIz8/F40 X-Received: by 2002:a17:906:9bc3:b0:78d:816f:3743 with SMTP id de3-20020a1709069bc300b0078d816f3743mr25073041ejc.380.1665668978783; Thu, 13 Oct 2022 06:49:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665668978; cv=none; d=google.com; s=arc-20160816; b=t0lcoIyzXuS+0fgraDfcBbEjVXgoWYUqrBhU0s879e8SDqzLO4f01uVU9CnHWaDzVe mvNO/dqjes5B/toXpW7sLjHCWYYQ7THwwoxOCOiEWhn4nXoHdVgXSdoHq9iB84hR4mh9 ggO5/J+nzaTrw5rmO0Kxgv+S25FRE6jWe/xhnW7wEOMKdfr+JbJgIApB7PWJ+K06jL7z 29xgfLbckNdQk1GKgdSbz2p8RzuOF0KHqUA6fssLfGLFeBioLvt57irP5ujmTn+ww+pi tb8XmL5h3vsxCHbNSZQauSTGRrTs3taYoEXCdA71gmKDfI7019V40qj5bTgOfZw2JtpZ bUsw== 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=g/g06Klsg7b+RLo/wb+FMcnSu34ABVrASf3iT0E9RqI=; b=dO6BGBspqYpyfRV0gfJ7VNnskdnTb4n8cjiA/iGbIJL4YkHwPuX24yNT0pzDE04DI+ BlsPlTRb4b+FdR0iz2XDRyz2qMj/AeQtwyZkYEArZz48fj/WXPZCH1DsOwJ9R/TQq33J Hw8Ab1e0kU9KNgPS/BZxkZAC3mnre22WRx7GdC3yKlvNvbGXulPkzdwM+UqrP4iYGtFM y+zjnsNxNLqKeF6kWpR8SVpMYxerF1MFOLsumduGa0Da2zWDXuSntdCJuoHI/TKzhNQS 63TFEktev0mkElqwoZqzarByqHfnKTS/H1cdimCMS99oCXZEB7CvvU0Rr7Avm2C7Kf4i 3/dA== 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 y14-20020a1709063a8e00b00781b67df7f6si17297327ejd.167.2022.10.13.06.49.38; Thu, 13 Oct 2022 06:49:38 -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 DE6D268BD54; Thu, 13 Oct 2022 16:49:22 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 50F7668B9A4 for ; Thu, 13 Oct 2022 16:49:14 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id DB6BC2400F4 for ; Thu, 13 Oct 2022 15:49:13 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id HlIK8O6E6DKt for ; Thu, 13 Oct 2022 15:49:13 +0200 (CEST) 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 mail0.khirnov.net (Postfix) with ESMTPS id 27AB42404E4 for ; Thu, 13 Oct 2022 15:49:13 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 056DC3A0922 for ; Thu, 13 Oct 2022 15:49:08 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 13 Oct 2022 15:48:53 +0200 Message-Id: <20221013134904.10104-2-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221013134904.10104-1-anton@khirnov.net> References: <20221013134904.10104-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 02/13] fftools/ffmpeg: move output_packet() to ffmpeg_mux 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: HJFtyIuXnEVn This function is common to both transcoding and streamcopy, so it properly belongs into the muxing code. --- fftools/ffmpeg.c | 78 ++++---------------------------------------- fftools/ffmpeg.h | 13 +++++++- fftools/ffmpeg_mux.c | 54 +++++++++++++++++++++++++++++- 3 files changed, 72 insertions(+), 73 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 754172e568..015ff762c9 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -694,70 +694,6 @@ static void close_output_stream(OutputStream *ost) sq_send(of->sq_encode, ost->sq_idx_encode, SQFRAME(NULL)); } -/* - * Send a single packet to the output, applying any bitstream filters - * associated with the output stream. This may result in any number - * of packets actually being written, depending on what bitstream - * filters are applied. The supplied packet is consumed and will be - * blank (as if newly-allocated) when this function returns. - * - * If eof is set, instead indicate EOF to all bitstream filters and - * therefore flush any delayed packets to the output. A blank packet - * must be supplied in this case. - */ -static void output_packet(OutputFile *of, AVPacket *pkt, - OutputStream *ost, int eof) -{ - const char *err_msg; - int ret = 0; - - if (!eof && pkt->dts != AV_NOPTS_VALUE) - ost->last_mux_dts = av_rescale_q(pkt->dts, ost->mux_timebase, AV_TIME_BASE_Q); - - /* apply the output bitstream filters */ - if (ost->bsf_ctx) { - int bsf_eof = 0; - - ret = av_bsf_send_packet(ost->bsf_ctx, eof ? NULL : pkt); - if (ret < 0) { - err_msg = "submitting a packet for bitstream filtering"; - goto fail; - } - - while (!bsf_eof) { - ret = av_bsf_receive_packet(ost->bsf_ctx, pkt); - if (ret == AVERROR(EAGAIN)) - return; - else if (ret == AVERROR_EOF) - bsf_eof = 1; - else if (ret < 0) { - err_msg = "applying bitstream filters to a packet"; - goto fail; - } - - ret = of_submit_packet(of, bsf_eof ? NULL : pkt, ost); - if (ret < 0) - goto mux_fail; - } - } else { - ret = of_submit_packet(of, eof ? NULL : pkt, ost); - if (ret < 0) - goto mux_fail; - } - - return; - -mux_fail: - err_msg = "submitting a packet to the muxer"; - -fail: - av_log(NULL, AV_LOG_ERROR, "Error %s for output stream #%d:%d.\n", - err_msg, ost->file_index, ost->index); - if (exit_on_error) - exit_program(1); - -} - static int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb) { OutputFile *of = output_files[ost->file_index]; @@ -949,7 +885,7 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame) av_assert0(frame); // should never happen during flushing return 0; } else if (ret == AVERROR_EOF) { - output_packet(of, pkt, ost, 1); + of_output_packet(of, pkt, ost, 1); return ret; } else if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "%s encoding failed\n", type_desc); @@ -985,7 +921,7 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame) ost->packets_encoded++; - output_packet(of, pkt, ost, 0); + of_output_packet(of, pkt, ost, 0); } av_assert0(0); @@ -1127,7 +1063,7 @@ static void do_subtitle_out(OutputFile *of, pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase); } pkt->dts = pkt->pts; - output_packet(of, pkt, ost, 0); + of_output_packet(of, pkt, ost, 0); } } @@ -1832,7 +1768,7 @@ static void flush_encoders(void) exit_program(1); } - output_packet(of, ost->pkt, ost, 1); + of_output_packet(of, ost->pkt, ost, 1); } init_output_stream_wrapper(ost, NULL, 1); @@ -1878,7 +1814,7 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p av_packet_unref(opkt); // EOF: flush output bitstream filters. if (!pkt) { - output_packet(of, opkt, ost, 1); + of_output_packet(of, opkt, ost, 1); return; } @@ -1934,7 +1870,7 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p opkt->duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->mux_timebase); - output_packet(of, opkt, ost, 0); + of_output_packet(of, opkt, ost, 0); ost->streamcopy_started = 1; } @@ -3833,7 +3769,7 @@ static int process_input(int file_index) if (ost->source_index == ifile->ist_index + i && (!ost->enc_ctx || ost->enc_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE)) { OutputFile *of = output_files[ost->file_index]; - output_packet(of, ost->pkt, ost, 1); + of_output_packet(of, ost->pkt, ost, 1); } } } diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 9c908e7465..632b7c0e86 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -725,7 +725,18 @@ int of_check_init(OutputFile *of); int of_write_trailer(OutputFile *of); void of_close(OutputFile **pof); -int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost); +/* + * Send a single packet to the output, applying any bitstream filters + * associated with the output stream. This may result in any number + * of packets actually being written, depending on what bitstream + * filters are applied. The supplied packet is consumed and will be + * blank (as if newly-allocated) when this function returns. + * + * If eof is set, instead indicate EOF to all bitstream filters and + * therefore flush any delayed packets to the output. A blank packet + * must be supplied in this case. + */ +void of_output_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof); int64_t of_filesize(OutputFile *of); AVChapter * const * of_get_chapters(OutputFile *of, unsigned int *nb_chapters); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index a6877ae5ec..6e9cbb4bab 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -319,7 +319,7 @@ static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) return 0; } -int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) +static int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) { int ret; @@ -338,6 +338,58 @@ int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) return 0; } +void of_output_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof) +{ + const char *err_msg; + int ret = 0; + + if (!eof && pkt->dts != AV_NOPTS_VALUE) + ost->last_mux_dts = av_rescale_q(pkt->dts, ost->mux_timebase, AV_TIME_BASE_Q); + + /* apply the output bitstream filters */ + if (ost->bsf_ctx) { + int bsf_eof = 0; + + ret = av_bsf_send_packet(ost->bsf_ctx, eof ? NULL : pkt); + if (ret < 0) { + err_msg = "submitting a packet for bitstream filtering"; + goto fail; + } + + while (!bsf_eof) { + ret = av_bsf_receive_packet(ost->bsf_ctx, pkt); + if (ret == AVERROR(EAGAIN)) + return; + else if (ret == AVERROR_EOF) + bsf_eof = 1; + else if (ret < 0) { + err_msg = "applying bitstream filters to a packet"; + goto fail; + } + + ret = of_submit_packet(of, bsf_eof ? NULL : pkt, ost); + if (ret < 0) + goto mux_fail; + } + } else { + ret = of_submit_packet(of, eof ? NULL : pkt, ost); + if (ret < 0) + goto mux_fail; + } + + return; + +mux_fail: + err_msg = "submitting a packet to the muxer"; + +fail: + av_log(NULL, AV_LOG_ERROR, "Error %s for output stream #%d:%d.\n", + err_msg, ost->file_index, ost->index); + if (exit_on_error) + exit_program(1); + +} + static int thread_stop(OutputFile *of) { Muxer *mux = of->mux;