From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 21A1345E8E for ; Thu, 13 Apr 2023 14:20:43 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0964C68BEFE; Thu, 13 Apr 2023 17:16:21 +0300 (EEST) Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id EE9CF68BEAB for ; Thu, 13 Apr 2023 17:16:02 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id BCB292406CC for ; Thu, 13 Apr 2023 16:16:02 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 9fVoOqOLYqIB for ; Thu, 13 Apr 2023 16:16:00 +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 9CA1C240706 for ; Thu, 13 Apr 2023 16:15:53 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 8CEA13A03E5 for ; Thu, 13 Apr 2023 16:15:53 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 13 Apr 2023 16:12:15 +0200 Message-Id: <20230413141223.17245-17-anton@khirnov.net> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230413141223.17245-1-anton@khirnov.net> References: <20230413141223.17245-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 17/25] fftools/ffmpeg: add muxer-input codec parameters to OutputStream 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: It stores codec parameters of the stream submitted to the muxer, which may be different from the codec parameters in AVStream due to bitstream filtering. This avoids the confusing back and forth synchronisation between the encoder, bitstream filters, and the muxer, now information flows only in one direction. It also reduces the need for non-muxing code to access AVStream. --- fftools/ffmpeg.h | 6 ++++++ fftools/ffmpeg_enc.c | 2 +- fftools/ffmpeg_mux.c | 6 ++++-- fftools/ffmpeg_mux_init.c | 21 +++++++++++++-------- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index f08f5db49e..8ec0f1b3f3 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -583,6 +583,12 @@ typedef struct OutputStream { int file_index; /* file index */ int index; /* stream index in the output file */ + /** + * Codec parameters for packets submitted to the muxer (i.e. before + * bitstream filtering, if any). + */ + AVCodecParameters *par_in; + /* input stream that is the source for this output stream; * may be NULL for streams with no well-defined source, e.g. * attachments or outputs from complex filtergraphs */ diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c index 063053623f..2462f53a82 100644 --- a/fftools/ffmpeg_enc.c +++ b/fftools/ffmpeg_enc.c @@ -354,7 +354,7 @@ int enc_open(OutputStream *ost, AVFrame *frame) av_log(ost, AV_LOG_WARNING, "The bitrate parameter is set too low." " It takes bits/s as argument, not kbits/s\n"); - ret = avcodec_parameters_from_context(ost->st->codecpar, ost->enc_ctx); + ret = avcodec_parameters_from_context(ost->par_in, ost->enc_ctx); if (ret < 0) { av_log(ost, AV_LOG_FATAL, "Error initializing the output stream codec context.\n"); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index eb64d8c3ff..40b439eea2 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -556,9 +556,9 @@ static int bsf_init(MuxStream *ms) int ret; if (!ctx) - return 0; + return avcodec_parameters_copy(ost->st->codecpar, ost->par_in); - ret = avcodec_parameters_copy(ctx->par_in, ost->st->codecpar); + ret = avcodec_parameters_copy(ctx->par_in, ost->par_in); if (ret < 0) return ret; @@ -768,6 +768,8 @@ static void ost_free(OutputStream **post) av_fifo_freep2(&ms->muxing_queue); } + avcodec_parameters_free(&ost->par_in); + av_bsf_free(&ms->bsf_ctx); av_frame_free(&ost->filtered_frame); diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index 129973d9f0..33d5268949 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -116,19 +116,19 @@ static int choose_encoder(const OptionsContext *o, AVFormatContext *s, if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO || type == AVMEDIA_TYPE_SUBTITLE) { MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, ost->st); if (!codec_name) { - ost->st->codecpar->codec_id = av_guess_codec(s->oformat, NULL, s->url, + ost->par_in->codec_id = av_guess_codec(s->oformat, NULL, s->url, NULL, ost->type); - *enc = avcodec_find_encoder(ost->st->codecpar->codec_id); + *enc = avcodec_find_encoder(ost->par_in->codec_id); if (!*enc) { av_log(ost, AV_LOG_FATAL, "Automatic encoder selection failed " "Default encoder for format %s (codec %s) is " "probably disabled. Please choose an encoder manually.\n", - s->oformat->name, avcodec_get_name(ost->st->codecpar->codec_id)); + s->oformat->name, avcodec_get_name(ost->par_in->codec_id)); return AVERROR_ENCODER_NOT_FOUND; } } else if (strcmp(codec_name, "copy")) { *enc = find_codec_or_die(ost, codec_name, ost->type, 1); - ost->st->codecpar->codec_id = (*enc)->id; + ost->par_in->codec_id = (*enc)->id; } } @@ -844,7 +844,7 @@ static int streamcopy_init(const Muxer *mux, const OptionsContext *o, const InputStream *ist = ost->ist; const InputFile *ifile = input_files[ist->file_index]; - AVCodecParameters *par = ost->st->codecpar; + AVCodecParameters *par = ost->par_in; uint32_t codec_tag = par->codec_tag; AVCodecContext *codec_ctx = NULL; @@ -995,6 +995,10 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, ms = mux_stream_alloc(mux, type); ost = &ms->ost; + ost->par_in = avcodec_parameters_alloc(); + if (!ost->par_in) + report_and_exit(AVERROR(ENOMEM)); + ms->muxing_queue = av_fifo_alloc2(8, sizeof(AVPacket*), 0); if (!ms->muxing_queue) report_and_exit(AVERROR(ENOMEM)); @@ -1003,6 +1007,7 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, ost->st = st; ost->ist = ist; ost->kf.ref_pts = AV_NOPTS_VALUE; + ost->par_in->codec_type = type; st->codecpar->codec_type = type; ret = choose_encoder(o, oc, ost, &enc); @@ -1166,7 +1171,7 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, uint32_t tag = strtol(codec_tag, &next, 0); if (*next) tag = AV_RL32(codec_tag); - ost->st->codecpar->codec_tag = tag; + ost->par_in->codec_tag = tag; if (ost->enc_ctx) ost->enc_ctx->codec_tag = tag; } @@ -1520,8 +1525,8 @@ static void of_add_attachments(Muxer *mux, const OptionsContext *o) ost = ost_add(mux, o, AVMEDIA_TYPE_ATTACHMENT, NULL); ost->attachment_filename = o->attachments[i]; - ost->st->codecpar->extradata = attachment; - ost->st->codecpar->extradata_size = len; + ost->par_in->extradata = attachment; + ost->par_in->extradata_size = len; p = strrchr(o->attachments[i], '/'); av_dict_set(&ost->st->metadata, "filename", (p && *p) ? p + 1 : o->attachments[i], AV_DICT_DONT_OVERWRITE); -- 2.39.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".