From patchwork Tue Sep 24 07:09:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 51787 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:ad12:0:b0:48e:c0f8:d0de with SMTP id w18csp173965vqy; Tue, 24 Sep 2024 00:19:21 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXfR3pPQB/2cv0D+20Lvz1N7VrjifXZxF6lgYS8uBdSQBPs0JsL9cG+dhTyrK77L7eudyvHzGHLy9o2qu6YGRL8@gmail.com X-Google-Smtp-Source: AGHT+IHocsL/FKW366745x7WWKuIdFy5ZqS4tSuS9VhhshIRLlvgX2lXz+ujaD4L6zjqV0NLF/Y6 X-Received: by 2002:a05:6512:ea0:b0:530:ea60:7e07 with SMTP id 2adb3069b0e04-536ac33b0e0mr6894246e87.58.1727162360767; Tue, 24 Sep 2024 00:19:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1727162360; cv=none; d=google.com; s=arc-20240605; b=c+8bjpSDhGKF6nGdMMMU9N+YDN0XSO67fHIRQi4w5UJE4xyt+/XPHK2G4dvgBNtEsu PtJztGiDdCHts4AUCdb4XtsgXl52Vf6IQezEwBKkjyHoNN0ycc2ZA7jChUQQIu63L3U3 ClLB/yNPlG8c+tsSHkG2dNUaD/VMvrzVpk0PP/rcF4srcegyG0WjNu8V/bRaX7P6eHYB FFza+6KXMnR/WOOlW3QJCNUk5eJOOhf+6S108+q05GxpY9x0i+9gUF8omeRo2dsYrprY pri8gWMCiKo1nXdSVvwGXhYRKsSnxl8KuXH6f3l5p2WP4NSElwPcGPehuaEl5fhprmKi 9RHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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:dkim-signature:delivered-to; bh=5w9XMcK2dLYaUknUKUdpnb6sT5DTy7Gm5B05/Ny0s6A=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=gz99a8665v5o4fwfZ36h8xw6RLKQybUa1oi8Ssbl0K0iTYw2MtLB3qbI6karnnpfBV ZNcpt7K6foSwHeUzkpGi3i118JiqaE8jBTduLWHuCZjrhd3E3pf4y2Mq5lEqx/vGFER/ ylrOVtZIGETWLETkzobAyKtN39kbFZ876Su486mNXJwwj03P+0KeeYkqFO9CXCkWNPZo Wg1UE4xZwbhCVfUMQaz9y7yDW294RouX+zDTvdLxxsMvvtoC03ApR0prmuES4cZv7DiB ku1PJzi8XCYnUAZZvzm4hnR4qVgK27rKA9a6sCuUwRdJ5Ls6rUnLCDz31Z//gWpwINL3 x+eA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@khirnov.net header.s=mail header.b=Do1SFQWc; 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 2adb3069b0e04-537a8646623si293998e87.346.2024.09.24.00.19.19; Tue, 24 Sep 2024 00:19:20 -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; dkim=neutral (body hash did not verify) header.i=@khirnov.net header.s=mail header.b=Do1SFQWc; 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 7BB4A68DB63; Tue, 24 Sep 2024 10:10:32 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail1.khirnov.net (quelana.khirnov.net [94.230.150.81]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1CB7968DAC2 for ; Tue, 24 Sep 2024 10:10:23 +0300 (EEST) Authentication-Results: mail1.khirnov.net; dkim=pass (2048-bit key; unprotected) header.d=khirnov.net header.i=@khirnov.net header.a=rsa-sha256 header.s=mail header.b=Do1SFQWc; dkim-atps=neutral Received: from localhost (mail1.khirnov.net [IPv6:::1]) by mail1.khirnov.net (Postfix) with ESMTP id 04EC64E11 for ; Tue, 24 Sep 2024 09:10:19 +0200 (CEST) Received: from mail1.khirnov.net ([IPv6:::1]) by localhost (mail1.khirnov.net [IPv6:::1]) (amavis, port 10024) with ESMTP id d6C7t2ygwnAA for ; Tue, 24 Sep 2024 09:10:18 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=khirnov.net; s=mail; t=1727161816; bh=zLqI5gcojMd0o5SzftSlPcNH/j5VRlaEE7hgGNyoc3U=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Do1SFQWcvLX8/B3JkXpMzr5HXnLQZUWJ1ZvV5dPHPP+Gqgnjx3pF/FDVUIK711JnY 57IHNLg1uIgXjeGz/RXDcd0j061inQT2eG7rgACMssCzcIWxDWkl3u72jmxEtB24lZ 4+Qu8f9DwQflpF0bwKVE/GhGr7lq1ZkXJDINi2WXqXKof9zG7HpFEMjXbzaNjzKf1q XO4cYMgYTfDGnaccYNqgeTFTcW4mP/Lt0CNvLX2+Wn9osd1LMpenwfXWvBd/oT/C7/ 6Rcipi65hMfM4YI4kjTHNivcdVBbxd946a8tlY/1bVNKKY0DMDyj0ZPb1ET5rqKAvO frwq3PjU2+6FA== 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 mail1.khirnov.net (Postfix) with ESMTPS id 9CDB64E18 for ; Tue, 24 Sep 2024 09:10:16 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 1241C3A0F41 for ; Tue, 24 Sep 2024 09:10:10 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 24 Sep 2024 09:09:46 +0200 Message-ID: <20240924071000.11428-5-anton@khirnov.net> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240924071000.11428-1-anton@khirnov.net> References: <20240924071000.11428-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 05/13] fftools/ffmpeg_enc: do not set output stream codec parameters directly 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: D9Xg+fKRq1vR Have the muxer code read them out of the encoder context in of_stream_init() instead. OutputStream.par_in no longer needs to be public, so can be moved to MuxStream. This is a step towards decoupling encoders from muxers. --- fftools/ffmpeg.h | 6 ------ fftools/ffmpeg_enc.c | 7 ------- fftools/ffmpeg_mux.c | 13 ++++++++++--- fftools/ffmpeg_mux.h | 6 ++++++ fftools/ffmpeg_mux_init.c | 30 +++++++++++++++++------------- 5 files changed, 33 insertions(+), 29 deletions(-) diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 98080213fd..13be7c4872 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -594,12 +594,6 @@ typedef struct OutputStream { 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 ba79f6a3fc..796849fdb9 100644 --- a/fftools/ffmpeg_enc.c +++ b/fftools/ffmpeg_enc.c @@ -360,13 +360,6 @@ int enc_open(void *opaque, const AVFrame *frame) av_log(e, 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->par_in, ost->enc_ctx); - if (ret < 0) { - av_log(e, AV_LOG_FATAL, - "Error initializing the output stream codec context.\n"); - return ret; - } - ret = of_stream_init(of, ost, enc_ctx); if (ret < 0) return ret; diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 71a771052f..ea0bbeed32 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -581,9 +581,9 @@ static int bsf_init(MuxStream *ms) int ret; if (!ctx) - return avcodec_parameters_copy(ost->st->codecpar, ost->par_in); + return avcodec_parameters_copy(ost->st->codecpar, ms->par_in); - ret = avcodec_parameters_copy(ctx->par_in, ost->par_in); + ret = avcodec_parameters_copy(ctx->par_in, ms->par_in); if (ret < 0) return ret; @@ -619,6 +619,13 @@ int of_stream_init(OutputFile *of, OutputStream *ost, // use upstream time base unless it has been overridden previously if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0) ost->st->time_base = av_add_q(enc_ctx->time_base, (AVRational){0, 1}); + + ret = avcodec_parameters_from_context(ms->par_in, enc_ctx); + if (ret < 0) { + av_log(ost, AV_LOG_FATAL, + "Error initializing the output stream codec parameters.\n"); + return ret; + } } /* initialize bitstream filters for the output stream @@ -813,7 +820,7 @@ static void ost_free(OutputStream **post) ost->logfile = NULL; } - avcodec_parameters_free(&ost->par_in); + avcodec_parameters_free(&ms->par_in); av_bsf_free(&ms->bsf_ctx); av_packet_free(&ms->bsf_pkt); diff --git a/fftools/ffmpeg_mux.h b/fftools/ffmpeg_mux.h index 22d728a919..5df718faf0 100644 --- a/fftools/ffmpeg_mux.h +++ b/fftools/ffmpeg_mux.h @@ -36,6 +36,12 @@ typedef struct MuxStream { OutputStream ost; + /** + * Codec parameters for packets submitted to the muxer (i.e. before + * bitstream filtering, if any). + */ + AVCodecParameters *par_in; + // name used for logging char log_name[32]; diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index b2351de177..47d745aa65 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -67,8 +67,9 @@ static int check_opt_bitexact(void *ctx, const AVDictionary *opts, } static int choose_encoder(const OptionsContext *o, AVFormatContext *s, - OutputStream *ost, const AVCodec **enc) + MuxStream *ms, const AVCodec **enc) { + OutputStream *ost = &ms->ost; enum AVMediaType type = ost->type; const char *codec_name = NULL; @@ -90,20 +91,20 @@ static int choose_encoder(const OptionsContext *o, AVFormatContext *s, } if (!codec_name) { - ost->par_in->codec_id = av_guess_codec(s->oformat, NULL, s->url, NULL, ost->type); - *enc = avcodec_find_encoder(ost->par_in->codec_id); + ms->par_in->codec_id = av_guess_codec(s->oformat, NULL, s->url, NULL, ost->type); + *enc = avcodec_find_encoder(ms->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->par_in->codec_id)); + s->oformat->name, avcodec_get_name(ms->par_in->codec_id)); return AVERROR_ENCODER_NOT_FOUND; } } else if (strcmp(codec_name, "copy")) { int ret = find_codec(ost, codec_name, ost->type, 1, enc); if (ret < 0) return ret; - ost->par_in->codec_id = (*enc)->id; + ms->par_in->codec_id = (*enc)->id; } return 0; @@ -1029,7 +1030,7 @@ static int streamcopy_init(const Muxer *mux, OutputStream *ost, AVDictionary **e const InputStream *ist = ost->ist; const InputFile *ifile = ist->file; - AVCodecParameters *par = ost->par_in; + AVCodecParameters *par = ms->par_in; uint32_t codec_tag = par->codec_tag; AVCodecContext *codec_ctx = NULL; @@ -1198,8 +1199,8 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, } } - ost->par_in = avcodec_parameters_alloc(); - if (!ost->par_in) + ms->par_in = avcodec_parameters_alloc(); + if (!ms->par_in) return AVERROR(ENOMEM); ms->last_mux_dts = AV_NOPTS_VALUE; @@ -1207,10 +1208,10 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, ost->st = st; ost->ist = ist; ost->kf.ref_pts = AV_NOPTS_VALUE; - ost->par_in->codec_type = type; + ms->par_in->codec_type = type; st->codecpar->codec_type = type; - ret = choose_encoder(o, oc, ost, &enc); + ret = choose_encoder(o, oc, ms, &enc); if (ret < 0) { av_log(ost, AV_LOG_FATAL, "Error selecting an encoder\n"); return ret; @@ -1447,7 +1448,7 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, tag = AV_RL32(buf); } ost->st->codecpar->codec_tag = tag; - ost->par_in->codec_tag = tag; + ms->par_in->codec_tag = tag; if (ost->enc_ctx) ost->enc_ctx->codec_tag = tag; } @@ -1799,6 +1800,7 @@ loop_end: static int of_add_attachments(Muxer *mux, const OptionsContext *o) { + MuxStream *ms; OutputStream *ost; int err; @@ -1866,9 +1868,11 @@ read_fail: return err; } + ms = ms_from_ost(ost); + ost->attachment_filename = attachment_filename; - ost->par_in->extradata = attachment; - ost->par_in->extradata_size = len; + ms->par_in->extradata = attachment; + ms->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);