From patchwork Fri Oct 14 10:15:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 38738 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:4a86:b0:9d:28a3:170e with SMTP id fn6csp244608pzb; Fri, 14 Oct 2022 03:19:38 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7A6/PB0OMfpjDz2KfAWHTGgAoRwOlx1Jv5CCcIOWGU3argTQkGsNld2Spvr4oCQLi2V0Hr X-Received: by 2002:a05:6402:4312:b0:45c:c1e9:9dc8 with SMTP id m18-20020a056402431200b0045cc1e99dc8mr3639790edc.154.1665742778669; Fri, 14 Oct 2022 03:19:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665742778; cv=none; d=google.com; s=arc-20160816; b=pwcHtep2L5TRiiJNi83r2BXwIKfhDf6uT8IyFwClgct3BN0jvd0IsSHI9EcP3TT3h4 KR26zs8RBD/lY15iTr7EWKGhY9I9PO/GUpCVJ9p2Jy03AXWMQDuQNGvoVw6a6leMd6Fx SuPt/jmHq38igxpys9pXOqekDm+uUvFv+UVyUfvGTEebpbEKRfZio9xmeTOifG9hu3dd d/p42MaHWas0aGWAvJkyEAOgn5NZPdr2jR2z59P2M++nYKLKtPVCs797qgNf8G65UexX +q4bL7owvfAZxSfT4rh0VWb0G8xe1uKycHWpSRFEKscsHY1xPdynpp1/RpNBmWw7SDKB AY0A== 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=wDOF3hSxhkPJcC7fXXjC2fuV9B+5xBgK552Kd6H5JVs=; b=ThMG0htk1mDbkPxTAJyeiBhX5dy+HoX4T6HKG1NRmxM0MgVzETaxGCzYjX6+hq8a5I Us907cQWV7fYHTOZiexGX7u1nqoLGtvNAKE04qdZuAWcJ6qqzm5Qbp3WWoULqCwM89q5 hoIBKavC2o0b+gHvtwKILasobyyXBttpHpbD32LM1VTn8M7opQ3YKVvAnRyYx/B976zO EhSltTR/5aOFqOF9OYQ8w3+Tt38fC38F6DdoNNtKBFqDUZTEajKwRb0KIkLnbvN9YEKb ilxH1IDC+b68N7YIVv1FQly2Lc46eJPQlvEAeV2xEF/YavkZTOFbngrZkIGOwaiIykpz crdg== 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 y21-20020aa7d515000000b004548a74553esi1546122edq.445.2022.10.14.03.19.38; Fri, 14 Oct 2022 03:19: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 386E668BDD0; Fri, 14 Oct 2022 13:18:40 +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 6C7E368BDA7 for ; Fri, 14 Oct 2022 13:18:31 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 123252404F5 for ; Fri, 14 Oct 2022 12:18:30 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id GiTy3SFQo-DD for ; Fri, 14 Oct 2022 12:18:29 +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 152DF2406C7 for ; Fri, 14 Oct 2022 12:18:25 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 2CCC13A1767 for ; Fri, 14 Oct 2022 12:18:24 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Fri, 14 Oct 2022 12:15:46 +0200 Message-Id: <20221014101548.3486-7-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221014101548.3486-1-anton@khirnov.net> References: <20221013134904.10104-1-anton@khirnov.net> <20221014101548.3486-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 20/22] fftools/ffmpeg_mux: embed OutputStream in a MuxStream 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: FtCGSJmAaznQ This is now possible since OutputStream is a child of OutputFile and the code allocating it can access MuxStream. Avoids the overhead and extra complexity of allocating two objects instead of one. Similar to what was previously done for OutputFile/Muxer. --- fftools/ffmpeg_mux.c | 33 +++++++++++++++++---------------- fftools/ffmpeg_mux.h | 5 ++--- fftools/ffmpeg_mux_init.c | 23 +++++++++-------------- 3 files changed, 28 insertions(+), 33 deletions(-) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 032b2ac34c..4ad69a149a 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -45,6 +45,11 @@ static Muxer *mux_from_of(OutputFile *of) return (Muxer*)of; } +static MuxStream *ms_from_ost(OutputStream *ost) +{ + return (MuxStream*)ost; +} + static int64_t filesize(AVIOContext *pb) { int64_t ret = -1; @@ -60,7 +65,7 @@ static int64_t filesize(AVIOContext *pb) static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt) { - MuxStream *ms = &mux->streams[ost->index]; + MuxStream *ms = ms_from_ost(ost); AVFormatContext *s = mux->fc; AVStream *st = ost->st; int64_t fs; @@ -251,7 +256,7 @@ finish: static int queue_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt) { - MuxStream *ms = &mux->streams[ost->index]; + MuxStream *ms = ms_from_ost(ost); AVPacket *tmp_pkt = NULL; int ret; @@ -409,8 +414,8 @@ static int thread_start(Muxer *mux) /* flush the muxing queues */ for (int i = 0; i < fc->nb_streams; i++) { - MuxStream *ms = &mux->streams[i]; OutputStream *ost = mux->of.streams[i]; + MuxStream *ms = ms_from_ost(ost); AVPacket *pkt; /* try to improve muxing time_base (only possible if nothing has been written yet) */ @@ -626,9 +631,11 @@ int of_write_trailer(OutputFile *of) static void ost_free(OutputStream **post) { OutputStream *ost = *post; + MuxStream *ms; if (!ost) return; + ms = ms_from_ost(ost); if (ost->logfile) { if (fclose(ost->logfile)) @@ -638,6 +645,13 @@ static void ost_free(OutputStream **post) ost->logfile = NULL; } + if (ms->muxing_queue) { + AVPacket *pkt; + while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0) + av_packet_free(&pkt); + av_fifo_freep2(&ms->muxing_queue); + } + av_bsf_free(&ost->bsf_ctx); av_frame_free(&ost->filtered_frame); @@ -697,19 +711,6 @@ void of_close(OutputFile **pof) sq_free(&of->sq_encode); sq_free(&mux->sq_mux); - for (int i = 0; i < mux->nb_streams; i++) { - MuxStream *ms = &mux->streams[i]; - AVPacket *pkt; - - if (!ms->muxing_queue) - continue; - - while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0) - av_packet_free(&pkt); - av_fifo_freep2(&ms->muxing_queue); - } - av_freep(&mux->streams); - for (int i = 0; i < of->nb_streams; i++) ost_free(&of->streams[i]); av_freep(&of->streams); diff --git a/fftools/ffmpeg_mux.h b/fftools/ffmpeg_mux.h index b73b6c628b..45daeb3591 100644 --- a/fftools/ffmpeg_mux.h +++ b/fftools/ffmpeg_mux.h @@ -35,6 +35,8 @@ #include "libavutil/thread.h" typedef struct MuxStream { + OutputStream ost; + /* the packets are buffered here until the muxer is ready to be initialized */ AVFifo *muxing_queue; @@ -57,9 +59,6 @@ typedef struct Muxer { pthread_t thread; ThreadQueue *tq; - MuxStream *streams; - int nb_streams; - AVDictionary *opts; int thread_queue_size; diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index 6909a017d8..d6bae92513 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -168,6 +168,7 @@ static OutputStream *new_output_stream(Muxer *mux, OptionsContext *o, enum AVMediaType type, int source_index) { AVFormatContext *oc = mux->fc; + MuxStream *ms; OutputStream *ost; const AVCodec *enc; AVStream *st = avformat_new_stream(oc, NULL); @@ -183,7 +184,14 @@ static OutputStream *new_output_stream(Muxer *mux, OptionsContext *o, if (oc->nb_streams - 1 < o->nb_streamid_map) st->id = o->streamid_map[oc->nb_streams - 1]; - ost = ALLOC_ARRAY_ELEM(mux->of.streams, mux->of.nb_streams); + ms = allocate_array_elem(&mux->of.streams, sizeof(MuxStream), + &mux->of.nb_streams); + ost = &ms->ost; + + ms->muxing_queue = av_fifo_alloc2(8, sizeof(AVPacket*), 0); + if (!ms->muxing_queue) + report_and_exit(AVERROR(ENOMEM)); + ms->last_mux_dts = AV_NOPTS_VALUE; ost->file_index = nb_output_files - 1; ost->index = idx; @@ -1900,19 +1908,6 @@ int of_open(OptionsContext *o, const char *filename) of->url = filename; - mux->streams = av_calloc(oc->nb_streams, sizeof(*mux->streams)); - if (!mux->streams) - return AVERROR(ENOMEM); - mux->nb_streams = oc->nb_streams; - - for (int i = 0; i < mux->nb_streams; i++) { - MuxStream *ms = &mux->streams[i]; - ms->muxing_queue = av_fifo_alloc2(8, sizeof(AVPacket*), 0); - if (!ms->muxing_queue) - return AVERROR(ENOMEM); - ms->last_mux_dts = AV_NOPTS_VALUE; - } - /* write the header for files with no streams */ if (of->format->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) { int ret = mux_check_init(mux);