From patchwork Mon Apr 4 11:30:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35187 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1914600pzb; Mon, 4 Apr 2022 04:38:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz7IpmBMfKrP58UeYVu0UMx3G9MRbDPdIJ19Z4WPHM3FwNqFLrxa+WIQgFD6bxtsLKXamEi X-Received: by 2002:a05:6402:202a:b0:41c:d9b0:e519 with SMTP id ay10-20020a056402202a00b0041cd9b0e519mr2147408edb.361.1649072294083; Mon, 04 Apr 2022 04:38:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072294; cv=none; d=google.com; s=arc-20160816; b=Z1CfNgNuXRHS73+5v0J8lQStDELv/nsDkzyQlUayOXdn79v6J+trBG/ewokT1sK73j zqEgUvnsIA3fC5t/H0McZy9rf9pkoPLLIlZPrsrtofbl5mF/SbMiVbyIeW8Dk9kxqQYg FZ9ZNw72Dw3GLLNgb06uoWvHJcXm/Rsvbz6mJGmV9LqawoOmD61kYuZp0LEoAb0QyO7T TIweQmmC8tHRLLrFHO8wQOdj9ccJfHTKEOlvlyN/OBvt2vCwkpY6USwb0OnPjPGIKAkD QoFF6jqTuGbJ75s3pbbfJRo2t18HjQYmQMaOTQxidb5afVUDjmchKyFPNl7T0LzpX0Mt 7YCg== 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=ZxXA84/qllbHs2XuVc8cCWAsb8XZZWxx1zK02AD827w=; b=EbrLkXixOeYyoTwoHJyqcz/AcWHVixdYTTmYiOsO7gKk2u4QWll4JkA6DItp9m4Jyr dw+RvkxpBRsG3orUMf8qxPb369ZOYUujWG7hFRQsJxTpG6o7roO5K4Fe/tjytFwM0qmp b0+lyMJ9yFOJm0qQdm52HRGDH+z3Y3x8SvTHNejEtrZUmUiFv5AS5pzIrZhGFmzPzShT Sb6frbMMJVTYBrbut/O2fZkRqMqicPzan8AdqnVA4B8PMU62HCXy2SEWDKDQCYCPKLVM wmzbhoVByRm1M9w1EE9pnU+Ria9hJX61GpaJvFj+RDCGi3eAe/jOf4zdAfP+kJOucLK8 K1uA== 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 o19-20020a1709062e9300b006e7ee995c94si1625467eji.924.2022.04.04.04.38.13; Mon, 04 Apr 2022 04:38: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 988A768B2CE; Mon, 4 Apr 2022 14:37:45 +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 D2AA168B299 for ; Mon, 4 Apr 2022 14:37:42 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 7290A240590 for ; Mon, 4 Apr 2022 13:37:42 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id ZCwABNhSxWF0 for ; Mon, 4 Apr 2022 13:37:41 +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 3DD1C24017C for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 2F2193A0DF7; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:26 +0200 Message-Id: <20220404113037.13070-39-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 38/49] fftools/ffmpeg: make the muxer AVFormatContext private to ffmpeg_mux.c 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: cGnGFWEiRJGc Since the muxer will operate in a separate thread in the future, the muxer context should not be accessed from the outside. --- fftools/ffmpeg.c | 4 +-- fftools/ffmpeg.h | 6 ++-- fftools/ffmpeg_filter.c | 6 ++-- fftools/ffmpeg_mux.c | 80 +++++++++++++++++++++++++---------------- fftools/ffmpeg_opt.c | 6 ++-- 5 files changed, 62 insertions(+), 40 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 701baa3a28..7e9ac943de 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1442,9 +1442,9 @@ static void print_final_stats(int64_t total_size) uint64_t total_packets = 0, total_size = 0; av_log(NULL, AV_LOG_VERBOSE, "Output file #%d (%s):\n", - i, of->ctx->url); + i, of->url); - for (j = 0; j < of->ctx->nb_streams; j++) { + for (j = 0; j < of->nb_streams; j++) { OutputStream *ost = output_streams[of->ost_index + j]; enum AVMediaType type = ost->enc_ctx->codec_type; diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 962867b783..2ba58b492f 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -583,11 +583,12 @@ typedef struct OutputFile { Muxer *mux; const AVOutputFormat *format; + const char *url; SyncQueue *sq_encode; SyncQueue *sq_mux; - AVFormatContext *ctx; + int nb_streams; int ost_index; /* index of the first stream in output_streams */ int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units @@ -696,7 +697,8 @@ int hw_device_setup_for_filter(FilterGraph *fg); int hwaccel_decode_init(AVCodecContext *avctx); -int of_muxer_init(OutputFile *of, AVDictionary *opts, int64_t limit_filesize); +int of_muxer_init(OutputFile *of, AVFormatContext *fc, + AVDictionary *opts, int64_t limit_filesize); /* open the muxer when all the streams are initialized */ int of_check_init(OutputFile *of); int of_write_trailer(OutputFile *of); diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c index 0845c631a5..122b17686a 100644 --- a/fftools/ffmpeg_filter.c +++ b/fftools/ffmpeg_filter.c @@ -603,11 +603,11 @@ static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, if (ost->apad && of->shortest) { int i; - for (i=0; ictx->nb_streams; i++) - if (of->ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) + for (i = 0; i < of->nb_streams; i++) + if (output_streams[of->ost_index + i]->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) break; - if (ictx->nb_streams) { + if (i < of->nb_streams) { AUTO_INSERT_FILTER("-apad", "apad", ost->apad); } } diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 1d8827277d..0a8bdc36d9 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -49,6 +49,8 @@ typedef struct MuxStream { } MuxStream; struct Muxer { + AVFormatContext *fc; + MuxStream *streams; AVDictionary *opts; @@ -167,7 +169,7 @@ static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) { MuxStream *ms = &of->mux->streams[ost->index]; - AVFormatContext *s = of->ctx; + AVFormatContext *s = of->mux->fc; AVStream *st = ost->st; int ret; @@ -311,8 +313,8 @@ static int print_sdp(void) if (!avc) exit_program(1); for (i = 0, j = 0; i < nb_output_files; i++) { - if (!strcmp(output_files[i]->ctx->oformat->name, "rtp")) { - avc[j] = output_files[i]->ctx; + if (!strcmp(output_files[i]->format->name, "rtp")) { + avc[j] = output_files[i]->mux->fc; j++; } } @@ -350,15 +352,16 @@ fail: /* open the muxer when all the streams are initialized */ int of_check_init(OutputFile *of) { + AVFormatContext *fc = of->mux->fc; int ret, i; - for (i = 0; i < of->ctx->nb_streams; i++) { + for (i = 0; i < fc->nb_streams; i++) { OutputStream *ost = output_streams[of->ost_index + i]; if (!ost->initialized) return 0; } - ret = avformat_write_header(of->ctx, &of->mux->opts); + ret = avformat_write_header(fc, &of->mux->opts); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Could not write header for output file #%d " @@ -369,7 +372,7 @@ int of_check_init(OutputFile *of) //assert_avoptions(of->opts); of->mux->header_written = 1; - av_dump_format(of->ctx, of->index, of->ctx->url, 1); + av_dump_format(fc, of->index, fc->url, 1); nb_output_dumped++; if (sdp_filename || want_sdp) { @@ -381,7 +384,7 @@ int of_check_init(OutputFile *of) } /* flush the muxing queues */ - for (i = 0; i < of->ctx->nb_streams; i++) { + for (i = 0; i < fc->nb_streams; i++) { MuxStream *ms = &of->mux->streams[i]; OutputStream *ost = output_streams[of->ost_index + i]; AVPacket *pkt; @@ -402,29 +405,30 @@ int of_check_init(OutputFile *of) int of_write_trailer(OutputFile *of) { + AVFormatContext *fc = of->mux->fc; int ret; if (!of->mux->header_written) { av_log(NULL, AV_LOG_ERROR, "Nothing was written into output file %d (%s), because " "at least one of its streams received no packets.\n", - of->index, of->ctx->url); + of->index, fc->url); return AVERROR(EINVAL); } - ret = av_write_trailer(of->ctx); + ret = av_write_trailer(fc); if (ret < 0) { - av_log(NULL, AV_LOG_ERROR, "Error writing trailer of %s: %s\n", of->ctx->url, av_err2str(ret)); + av_log(NULL, AV_LOG_ERROR, "Error writing trailer of %s: %s\n", fc->url, av_err2str(ret)); return ret; } of->mux->final_filesize = of_filesize(of); if (!(of->format->flags & AVFMT_NOFILE)) { - ret = avio_closep(&of->ctx->pb); + ret = avio_closep(&fc->pb); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Error closing file %s: %s\n", - of->ctx->url, av_err2str(ret)); + fc->url, av_err2str(ret)); return ret; } } @@ -432,14 +436,28 @@ int of_write_trailer(OutputFile *of) return 0; } -static void mux_free(Muxer **pmux, int nb_streams) +static void fc_close(AVFormatContext **pfc) +{ + AVFormatContext *fc = *pfc; + + if (!fc) + return; + + if (!(fc->oformat->flags & AVFMT_NOFILE)) + avio_closep(&fc->pb); + avformat_free_context(fc); + + *pfc = NULL; +} + +static void mux_free(Muxer **pmux) { Muxer *mux = *pmux; if (!mux) return; - for (int i = 0; i < nb_streams; i++) { + for (int i = 0; i < mux->fc->nb_streams; i++) { MuxStream *ms = &mux->streams[i]; AVPacket *pkt; @@ -453,13 +471,14 @@ static void mux_free(Muxer **pmux, int nb_streams) av_freep(&mux->streams); av_dict_free(&mux->opts); + fc_close(&mux->fc); + av_freep(pmux); } void of_close(OutputFile **pof) { OutputFile *of = *pof; - AVFormatContext *s; if (!of) return; @@ -467,34 +486,33 @@ void of_close(OutputFile **pof) sq_free(&of->sq_encode); sq_free(&of->sq_mux); - s = of->ctx; - - mux_free(&of->mux, s ? s->nb_streams : 0); - - if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE)) - avio_closep(&s->pb); - avformat_free_context(s); + mux_free(&of->mux); av_freep(pof); } -int of_muxer_init(OutputFile *of, AVDictionary *opts, int64_t limit_filesize) +int of_muxer_init(OutputFile *of, AVFormatContext *fc, + AVDictionary *opts, int64_t limit_filesize) { Muxer *mux = av_mallocz(sizeof(*mux)); int ret = 0; - if (!mux) + if (!mux) { + fc_close(&fc); return AVERROR(ENOMEM); + } - mux->streams = av_calloc(of->ctx->nb_streams, sizeof(*mux->streams)); + mux->streams = av_calloc(fc->nb_streams, sizeof(*mux->streams)); if (!mux->streams) { + fc_close(&fc); av_freep(&mux); return AVERROR(ENOMEM); } of->mux = mux; + mux->fc = fc; - for (int i = 0; i < of->ctx->nb_streams; i++) { + for (int i = 0; i < fc->nb_streams; i++) { MuxStream *ms = &mux->streams[i]; ms->muxing_queue = av_fifo_alloc2(8, sizeof(AVPacket*), 0); if (!ms->muxing_queue) { @@ -511,7 +529,7 @@ int of_muxer_init(OutputFile *of, AVDictionary *opts, int64_t limit_filesize) want_sdp = 0; /* write the header for files with no streams */ - if (of->format->flags & AVFMT_NOSTREAMS && of->ctx->nb_streams == 0) { + if (of->format->flags & AVFMT_NOSTREAMS && fc->nb_streams == 0) { ret = of_check_init(of); if (ret < 0) goto fail; @@ -519,7 +537,7 @@ int of_muxer_init(OutputFile *of, AVDictionary *opts, int64_t limit_filesize) fail: if (ret < 0) - mux_free(&of->mux, of->ctx->nb_streams); + mux_free(&of->mux); return ret; } @@ -531,7 +549,7 @@ int of_finished(OutputFile *of) int64_t of_filesize(OutputFile *of) { - AVIOContext *pb = of->ctx->pb; + AVIOContext *pb = of->mux->fc->pb; int64_t ret = -1; if (of->mux->final_filesize) @@ -548,6 +566,6 @@ int64_t of_filesize(OutputFile *of) AVChapter * const * of_get_chapters(OutputFile *of, unsigned int *nb_chapters) { - *nb_chapters = of->ctx->nb_chapters; - return of->ctx->chapters; + *nb_chapters = of->mux->fc->nb_chapters; + return of->mux->fc->chapters; } diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index ac0b4fc7d8..a86255e409 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2428,7 +2428,6 @@ static int open_output_file(OptionsContext *o, const char *filename) exit_program(1); } - of->ctx = oc; of->format = oc->oformat; if (o->recording_time != INT64_MAX) oc->duration = o->recording_time; @@ -3028,7 +3027,10 @@ loop_end: exit_program(1); } - err = of_muxer_init(of, format_opts, o->limit_filesize); + of->nb_streams = oc->nb_streams; + of->url = filename; + + err = of_muxer_init(of, oc, format_opts, o->limit_filesize); if (err < 0) { av_log(NULL, AV_LOG_FATAL, "Error initializing internal muxing state\n"); exit_program(1);