From: Anton Khirnov <anton@khirnov.net> To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH 27/35] fftools/ffmpeg: make the muxer AVFormatContext private to ffmpeg_mux.c Date: Thu, 16 Jun 2022 21:55:26 +0200 Message-ID: <20220616195534.5278-27-anton@khirnov.net> (raw) In-Reply-To: <20220616195534.5278-1-anton@khirnov.net> 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 ab3665e53c..4321652848 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1496,9 +1496,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 3d55370d3a..c757f72c0e 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -589,11 +589,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 @@ -697,7 +698,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; i<of->ctx->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 (i<of->ctx->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 87c7bdc4e3..c44ff0f1df 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; @@ -117,7 +119,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; @@ -263,8 +265,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++; } } @@ -302,15 +304,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 " @@ -321,7 +324,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) { @@ -333,7 +336,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; @@ -356,29 +359,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; } } @@ -386,14 +390,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; @@ -409,13 +427,14 @@ static void mux_free(Muxer **pmux, int nb_streams) av_packet_free(&mux->sq_pkt); + fc_close(&mux->fc); + av_freep(pmux); } void of_close(OutputFile **pof) { OutputFile *of = *pof; - AVFormatContext *s; if (!of) return; @@ -423,34 +442,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) { @@ -475,7 +493,7 @@ int of_muxer_init(OutputFile *of, AVDictionary *opts, int64_t limit_filesize) } /* 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; @@ -483,7 +501,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; } @@ -495,7 +513,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) @@ -512,6 +530,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 22eb558ff3..5ef4115cdb 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2455,7 +2455,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; @@ -3055,7 +3054,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); -- 2.34.1 _______________________________________________ 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".
next prev parent reply other threads:[~2022-06-16 20:10 UTC|newest] Thread overview: 55+ messages / expand[flat|nested] mbox.gz Atom feed top 2022-06-16 19:55 [FFmpeg-devel] [PATCH 01/35] fftools/ffmpeg_mux: add private muxer context Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 02/35] fftools/ffmpeg: add a helper function to access output file size Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 03/35] fftools/ffmpeg: fix the type of limit_filesize Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 04/35] fftools/ffmpeg: refactor limiting output file size with -fs Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 05/35] fftools/ffmpeg: set want_sdp when initializing the muxer Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 06/35] fftools/ffmpeg: write the header for stream-less outputs " Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 07/35] fftools/ffmpeg: move closing the file into of_write_trailer() Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 08/35] fftools/ffmpeg: refactor the code checking for bitexact output Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 09/35] fftools/ffmpeg: access output file chapters through a wrapper Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 10/35] fftools/ffmpeg: do not log to the muxer context Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 11/35] fftools/ffmpeg: move the mux queue into muxer private data Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 12/35] fftools/ffmpeg_mux: split queuing packets into a separate function Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 13/35] fftools/ffmpeg_mux: split of_write_packet() Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 14/35] fftools/ffmpeg: move output file opts into private context Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 15/35] fftools/ffmpeg: move freeing 2pass input stats to a better place Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 16/35] fftools/ffmpeg: use refcounted packets for encoded subtitles Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 17/35] fftools/ffmpeg: do not send spurious EOF for streamcopy when looping Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 18/35] fate/ffmpeg: add a test for interleaving video+subs Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 19/35] fftools/ffmpeg: use last filter output pts to choose next output stream Anton Khirnov 2022-06-17 18:45 ` Michael Niedermayer 2022-06-23 15:32 ` [FFmpeg-devel] [PATCH] " Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 20/35] fftools/ffmpeg: use pre-BSF DTS for choosing next output Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 21/35] fftools: add an object pool Anton Khirnov 2022-06-16 21:41 ` Andreas Rheinhardt 2022-07-22 15:39 ` [FFmpeg-devel] [PATCH 22/35] " Anton Khirnov 2022-07-22 17:58 ` Andreas Rheinhardt 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 22/35] fftools/ffmpeg: rework -shortest implementation Anton Khirnov 2022-06-16 21:05 ` Andreas Rheinhardt 2022-06-17 10:25 ` Anton Khirnov 2022-06-23 10:04 ` [FFmpeg-devel] [PATCH] " Anton Khirnov 2022-07-13 10:50 ` Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 23/35] fftools/ffmpeg_mux: reindent Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 24/35] fftools/ffmpeg: use the sync queues to handle -frames Anton Khirnov 2022-06-16 20:33 ` Andreas Rheinhardt 2022-06-17 10:46 ` Anton Khirnov 2022-06-22 8:27 ` Andreas Rheinhardt 2022-06-22 17:26 ` Anton Khirnov 2022-06-23 22:12 ` Michael Niedermayer 2022-07-04 16:11 ` [FFmpeg-devel] [PATCH] " Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 25/35] fftools/ffmpeg: stop using OutputStream.frame_number in print_report() Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 26/35] fftools/ffmpeg: only set OutputStream.frame_number for video encoding Anton Khirnov 2022-06-16 19:55 ` Anton Khirnov [this message] 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 28/35] fftools/ffmpeg_mux: return errors from of_submit_packet() Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 29/35] fftools/ffmpeg_mux: return errors from submit_packet() Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 30/35] fftools/ffmpeg_mux: return errors from write_packet() Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 31/35] fftools/ffmpeg_mux: do not call exit_program() in print_sdp() Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 32/35] fftools/ffmpeg: stop using av_stream_get_end_pts() Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 33/35] fftools/ffmpeg: depend on threads Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 34/35] fftools: add a multistream thread-safe queue Anton Khirnov 2022-06-16 19:55 ` [FFmpeg-devel] [PATCH 35/35] fftools/ffmpeg: move each muxer to a separate thread Anton Khirnov 2022-06-17 10:27 ` [FFmpeg-devel] [PATCH 01/35] fftools/ffmpeg_mux: add private muxer context Anton Khirnov 2022-07-08 16:58 ` Michael Niedermayer 2022-07-13 10:58 ` Anton Khirnov 2022-07-13 22:12 ` Michael Niedermayer 2022-07-14 5:37 ` 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=20220616195534.5278-27-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