From patchwork Thu Nov 17 10:16:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 39316 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:999a:b0:a4:2148:650a with SMTP id ve26csp997529pzb; Thu, 17 Nov 2022 02:17:48 -0800 (PST) X-Google-Smtp-Source: AA0mqf7n7NtwSuLK/TO7iQP1immzKsSqk79PiITY35RMoJZfGzGHbaTJPn1WUMA79GV0zTo//RsH X-Received: by 2002:a17:907:8b13:b0:781:541:6599 with SMTP id sz19-20020a1709078b1300b0078105416599mr1574747ejc.45.1668680268697; Thu, 17 Nov 2022 02:17:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668680268; cv=none; d=google.com; s=arc-20160816; b=ERUOZP7DxSms6kn45WWH8HCaspsR6f4VwBY0lDY4GqVr0hg0EVDm9EzQbD4Tod+yGD OD6ZhxOpg97Uumz0RkZ2PbmgtNJBSiVR27SeezHKjI+zM7fqC9K4z89t4gPn8vUV0Eh5 8Zo+wSQDmrA82BneGZMLPUHcAwzXuFfHODMnx48ey84cyKPi0ovblx/k1ioLL9RjAkmb Io8uaw2z7Wgc9NU2d9A2NfAs4QR//i5wRcizI1RcLcNAVmRd08KMYI/gzq3UwWZoKdJy 4Pc8LBfVarMXU1913a/hL+qrUAdNEQ/K3vjhPDv3BUlpgar+aWwpwBIrInOfts/XJBFr gbvg== 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=TaJo+dmPOauQGSvjSyqACkZ6IjgHGz/+NpypSv9lZ6o=; b=UjDp3h8agMgSXkwoDfTWbqoQ4Q1dnULdXv2FlC4+ZtxGBGD0sG/lZHjZpuS704esbF 2T3/6PCI/gSwwfQLzcexXeLTEPriC2YX2jQykbUJtiMSmRl3H+ieX4LnUvIeWfp1FT+5 z8vNRn7VaXVt19latcVABBM0PrqoZwfllh4On+uAK+CFsC5f6amCaihTPKkYIKX4uxY1 wRtK7Nfeuq1vRsuAisjLqj9hkXtX+OrlDviEw4H4ovuZnXJiufN5A0thpcVmwxtufbQf uWKswLy86AsFzgHNNOePXU51x/RPujGB5cEIHjEkvQEvWfSVlRhLQC4Xr4yG/eu45zys xXng== 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 nb4-20020a1709071c8400b007ae2dfe020bsi250684ejc.783.2022.11.17.02.17.48; Thu, 17 Nov 2022 02:17:48 -0800 (PST) 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 8218168BDC9; Thu, 17 Nov 2022 12:16:57 +0200 (EET) 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 3D6B868BD36 for ; Thu, 17 Nov 2022 12:16:49 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 936642405F9 for ; Thu, 17 Nov 2022 11:16:46 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id QvaDIiljf51E for ; Thu, 17 Nov 2022 11:16:45 +0100 (CET) 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 5326C2405EC for ; Thu, 17 Nov 2022 11:16:43 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 189693A054D for ; Thu, 17 Nov 2022 11:16:43 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 17 Nov 2022 11:16:34 +0100 Message-Id: <20221117101640.6789-4-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221117101640.6789-1-anton@khirnov.net> References: <20221117101640.6789-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 04/10] fftools/ffmpeg: replace OutputStream.source_index with a pointer to InputStream 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: kZWWGcdUH8/k This is simpler. The indirection via an index exists for historical reasons that longer apply. --- fftools/ffmpeg.c | 42 ++++++++++++++------------------------- fftools/ffmpeg.h | 7 ++++++- fftools/ffmpeg_mux_init.c | 37 ++++++++++++++++------------------ 3 files changed, 38 insertions(+), 48 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 08fdd6e98f..0be2715687 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1122,15 +1122,12 @@ static void do_video_out(OutputFile *of, double delta, delta0; double duration = 0; double sync_ipts = AV_NOPTS_VALUE; - InputStream *ist = NULL; + InputStream *ist = ost->ist; AVFilterContext *filter = ost->filter->filter; init_output_stream_wrapper(ost, next_picture, 1); sync_ipts = adjust_frame_pts_to_encoder_tb(of, ost, next_picture); - if (ost->source_index >= 0) - ist = input_streams[ost->source_index]; - frame_rate = av_buffersink_get_frame_rate(filter); if (frame_rate.num > 0 && frame_rate.den > 0) duration = 1/(av_q2d(frame_rate) * av_q2d(enc->time_base)); @@ -1764,9 +1761,8 @@ static void flush_encoders(void) static int check_output_constraints(InputStream *ist, OutputStream *ost) { OutputFile *of = output_files[ost->file_index]; - int ist_index = input_files[ist->file_index]->ist_index + ist->st->index; - if (ost->source_index != ist_index) + if (ost->ist != ist) return 0; if (ost->finished & MUXER_FINISHED) @@ -2610,13 +2606,6 @@ static int init_input_stream(int ist_index, char *error, int error_len) return 0; } -static InputStream *get_input_stream(OutputStream *ost) -{ - if (ost->source_index >= 0) - return input_streams[ost->source_index]; - return NULL; -} - static int compare_int64(const void *a, const void *b) { return FFDIFFSIGN(*(const int64_t *)a, *(const int64_t *)b); @@ -2625,7 +2614,7 @@ static int compare_int64(const void *a, const void *b) static int init_output_stream_streamcopy(OutputStream *ost) { OutputFile *of = output_files[ost->file_index]; - InputStream *ist = get_input_stream(ost); + InputStream *ist = ost->ist; InputFile *ifile = input_files[ist->file_index]; AVCodecParameters *par = ost->st->codecpar; AVCodecContext *codec_ctx; @@ -2838,7 +2827,7 @@ static void parse_forced_key_frames(char *kf, OutputStream *ost, static void init_encoder_time_base(OutputStream *ost, AVRational default_time_base) { - InputStream *ist = get_input_stream(ost); + InputStream *ist = ost->ist; AVCodecContext *enc_ctx = ost->enc_ctx; if (ost->enc_timebase.num > 0) { @@ -2862,7 +2851,7 @@ static void init_encoder_time_base(OutputStream *ost, AVRational default_time_ba static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) { - InputStream *ist = get_input_stream(ost); + InputStream *ist = ost->ist; AVCodecContext *enc_ctx = ost->enc_ctx; AVCodecContext *dec_ctx = NULL; OutputFile *of = output_files[ost->file_index]; @@ -3006,8 +2995,8 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) case AVMEDIA_TYPE_SUBTITLE: enc_ctx->time_base = AV_TIME_BASE_Q; if (!enc_ctx->width) { - enc_ctx->width = input_streams[ost->source_index]->par->width; - enc_ctx->height = input_streams[ost->source_index]->par->height; + enc_ctx->width = ost->ist->par->width; + enc_ctx->height = ost->ist->par->height; } if (dec_ctx && dec_ctx->subtitle_header) { /* ASS code assumes this buffer is null terminated so add extra byte. */ @@ -3063,13 +3052,12 @@ static int init_output_stream(OutputStream *ost, AVFrame *frame, if (ost->enc_ctx) { const AVCodec *codec = ost->enc_ctx->codec; - InputStream *ist; + InputStream *ist = ost->ist; ret = init_output_stream_encode(ost, frame); if (ret < 0) return ret; - ist = get_input_stream(ost); if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0)) av_dict_set(&ost->encoder_opts, "threads", "auto", 0); @@ -3150,7 +3138,7 @@ static int init_output_stream(OutputStream *ost, AVFrame *frame, // copy estimated duration as a hint to the muxer if (ost->st->duration <= 0 && ist && ist->st->duration > 0) ost->st->duration = av_rescale_q(ist->st->duration, ist->st->time_base, ost->st->time_base); - } else if (ost->source_index >= 0) { + } else if (ost->ist) { ret = init_output_stream_streamcopy(ost); if (ret < 0) return ret; @@ -3257,12 +3245,12 @@ static int transcode_init(void) } av_log(NULL, AV_LOG_INFO, " Stream #%d:%d -> #%d:%d", - input_streams[ost->source_index]->file_index, - input_streams[ost->source_index]->st->index, + ost->ist->file_index, + ost->ist->st->index, ost->file_index, ost->index); if (ost->enc_ctx) { - const AVCodec *in_codec = input_streams[ost->source_index]->dec; + const AVCodec *in_codec = ost->ist->dec; const AVCodec *out_codec = ost->enc_ctx->codec; const char *decoder_name = "?"; const char *in_codec_name = "?"; @@ -3648,7 +3636,7 @@ static int process_input(int file_index) /* mark all outputs that don't go through lavfi as finished */ for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) { - if (ost->source_index == ifile->ist_index + i && + if (ost->ist == ist && (!ost->enc_ctx || ost->enc_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE)) { OutputFile *of = output_files[ost->file_index]; of_output_packet(of, ost->pkt, ost, 1); @@ -3837,8 +3825,8 @@ static int transcode_step(void) return 0; } } else { - av_assert0(ost->source_index >= 0); - ist = input_streams[ost->source_index]; + ist = ost->ist; + av_assert0(ist); } ret = process_input(ist->file_index); diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index a96ff0d723..b1e466406d 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -486,7 +486,12 @@ typedef enum { typedef struct OutputStream { int file_index; /* file index */ int index; /* stream index in the output file */ - int source_index; /* InputStream index */ + + /* 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 */ + InputStream *ist; + AVStream *st; /* stream in the output file */ /* number of frames emitted by the video-encoding sync code */ int64_t vsync_frame_number; diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index db45fa09fa..e460e7603f 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -354,10 +354,10 @@ static OutputStream *new_output_stream(Muxer *mux, const OptionsContext *o, if (ost->enc_ctx && av_get_exact_bits_per_sample(ost->enc_ctx->codec_id) == 24) av_dict_set(&ost->swr_opts, "output_sample_bits", "24", 0); - ost->source_index = source_index; if (source_index >= 0) { - input_streams[source_index]->discard = 0; - input_streams[source_index]->st->discard = input_streams[source_index]->user_set_discard; + ost->ist = input_streams[source_index]; + ost->ist->discard = 0; + ost->ist->st->discard = ost->ist->user_set_discard; } ost->last_mux_dts = AV_NOPTS_VALUE; ost->last_filter_pts = AV_NOPTS_VALUE; @@ -631,9 +631,8 @@ static OutputStream *new_video_stream(Muxer *mux, const OptionsContext *o, int s VSYNC_CFR; } - if (ost->source_index >= 0 && ost->vsync_method == VSYNC_CFR) { - const InputStream *ist = input_streams[ost->source_index]; - const InputFile *ifile = input_files[ist->file_index]; + if (ost->ist && ost->vsync_method == VSYNC_CFR) { + const InputFile *ifile = input_files[ost->ist->file_index]; if (ifile->nb_streams == 1 && ifile->input_ts_offset == 0) ost->vsync_method = VSYNC_VSCFR; @@ -730,12 +729,12 @@ static OutputStream *new_audio_stream(Muxer *mux, const OptionsContext *o, int s if (map->channel_idx == -1) { ist = NULL; - } else if (ost->source_index < 0) { + } else if (!ost->ist) { av_log(NULL, AV_LOG_FATAL, "Cannot determine input stream for channel mapping %d.%d\n", ost->file_index, ost->st->index); continue; } else { - ist = input_streams[ost->source_index]; + ist = ost->ist; } if (!ist || (ist->file_index == map->file_idx && ist->st->index == map->stream_idx)) { @@ -1648,11 +1647,10 @@ static void copy_meta(Muxer *mux, const OptionsContext *o) if (!metadata_streams_manual) for (int i = 0; i < of->nb_streams; i++) { OutputStream *ost = of->streams[i]; - InputStream *ist; - if (ost->source_index < 0) /* this is true e.g. for attached files */ + + if (!ost->ist) /* this is true e.g. for attached files */ continue; - ist = input_streams[ost->source_index]; - av_dict_copy(&ost->st->metadata, ist->st->metadata, AV_DICT_DONT_OVERWRITE); + av_dict_copy(&ost->st->metadata, ost->ist->st->metadata, AV_DICT_DONT_OVERWRITE); if (ost->enc_ctx) { av_dict_set(&ost->st->metadata, "encoder", NULL, 0); } @@ -1673,8 +1671,8 @@ static int set_dispositions(OutputFile *of, AVFormatContext *ctx) have_manual |= !!ost->disposition; - if (ost->source_index >= 0) { - ost->st->disposition = input_streams[ost->source_index]->st->disposition; + if (ost->ist) { + ost->st->disposition = ost->ist->st->disposition; if (ost->st->disposition & AV_DISPOSITION_DEFAULT) have_default[ost->st->codecpar->codec_type] = 1; @@ -1846,8 +1844,8 @@ int of_open(const OptionsContext *o, const char *filename) for (int i = 0; i < of->nb_streams; i++) { OutputStream *ost = of->streams[i]; - if (ost->enc_ctx && ost->source_index >= 0) { - InputStream *ist = input_streams[ost->source_index]; + if (ost->enc_ctx && ost->ist) { + InputStream *ist = ost->ist; ist->decoding_needed |= DECODING_FOR_OST; ist->processing_needed = 1; @@ -1857,14 +1855,13 @@ int of_open(const OptionsContext *o, const char *filename) if (err < 0) { av_log(NULL, AV_LOG_ERROR, "Error initializing a simple filtergraph between streams " - "%d:%d->%d:%d\n", ist->file_index, ost->source_index, + "%d:%d->%d:%d\n", ist->file_index, ist->st->index, nb_output_files - 1, ost->st->index); exit_program(1); } } - } else if (ost->source_index >= 0) { - InputStream *ist = input_streams[ost->source_index]; - ist->processing_needed = 1; + } else if (ost->ist) { + ost->ist->processing_needed = 1; } /* set the filter output constraints */