From patchwork Wed Jun 14 16:49:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 42065 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c526:b0:117:ac03:c9de with SMTP id gm38csp191738pzb; Wed, 14 Jun 2023 09:52:50 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7MiRQmhFfS1Hg93wGHrmb9Fv+llh7mL8P6sKfqSuHr4t7nFezdsaz8Zf+Dl6nCvRZClpgy X-Received: by 2002:aa7:d04f:0:b0:51a:1d9c:196b with SMTP id n15-20020aa7d04f000000b0051a1d9c196bmr863430edo.17.1686761570539; Wed, 14 Jun 2023 09:52:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686761570; cv=none; d=google.com; s=arc-20160816; b=bEalGvMyiVOdVfd4PRc0+f166tn8eQFQoyCMZp8MTfQcv2NyNyXJSCzD3YzzmkHUqD h/mv47QtOqVt/5lfbIKHJ7ylQaEChoKat3rf88LkemPLlEfLsJV+aTSAZjozKhtCenJ8 ky3SxTggi/N2+0LGgDrGrOwjsSYQ2T43C9MIBOWDFJ5umIwwbzYYJ9WZhIBUmmYD49UN NIX+VsR6C5tamC3drSRVlsKXGTOhJRnRx7ZgtK+k6TKJnX3SkPtWkksvZfFykajzbhIA jgTHeWDLJNWpQ1KbY2/KY3agFrFsC0KZqwlGf9jGWl1Q+xe9x7q0usgDdG/pUuM2Vy/9 5oLQ== 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=xJZBRFyHlvmbg3HV4SWjo2912MHsBETLpYu9jV6Y+Z0=; b=RB+t/c22YQzTzKYg5SI+UoLm+p5z/PvQD/kaFkNjzlsXvgZoU7AhdM34CcAjnI6d3i t7GzGjIor3/vPcB6AyrhNnMUuW84XyCHq+JSaX/IU2CAaI629tCJiEGyht0oLpn6kN6d 2NbcZ0mBy9cJrqDgnP3X++Zrx3oQXh38RkIV7MwJJhT+ngBrY8fYObCa+QpaHUhSEPfl w6+HAdGjULhTRYQx6mFdxKhal9ww2jueTrouwle7rcFyoNzlRofig8yuZY13QaSMThYH RKprT1wy1UBHIDNebOE54bG0ogbAcYYZqmA+4ckKZJsdBytDkvkubR+LrgIe8eMKwx9X IDHg== 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 w5-20020aa7dcc5000000b00514a4541760si8714845edu.524.2023.06.14.09.52.50; Wed, 14 Jun 2023 09:52:50 -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 57B3C68C500; Wed, 14 Jun 2023 19:50:17 +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 6A13E68C4B3 for ; Wed, 14 Jun 2023 19:50:05 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 2839D2404F5 for ; Wed, 14 Jun 2023 18:50:05 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id x7bWB0wv9Sie for ; Wed, 14 Jun 2023 18:50:03 +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 20E37240D0E for ; Wed, 14 Jun 2023 18:49:54 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id CA72D3A0BDC for ; Wed, 14 Jun 2023 18:49:47 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Wed, 14 Jun 2023 18:49:02 +0200 Message-Id: <20230614164908.28712-15-anton@khirnov.net> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230614164908.28712-1-anton@khirnov.net> References: <20230614164908.28712-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 15/21] fftools/ffmpeg: attach bits_per_raw_sample information to frames 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: nX+gbHQPriox This way avoids encoders reaching into filters or decoders for this information. --- fftools/ffmpeg.h | 7 ++----- fftools/ffmpeg_dec.c | 21 ++++++++++----------- fftools/ffmpeg_enc.c | 13 ++++++------- fftools/ffmpeg_filter.c | 27 ++++++++++++++++++--------- fftools/ffmpeg_mux_init.c | 14 +++++--------- 5 files changed, 41 insertions(+), 41 deletions(-) diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 80d9ab52bc..abc1a21d73 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -315,9 +315,6 @@ typedef struct FilterGraph { int index; AVFilterGraph *graph; - // true when the filtergraph contains only meta filters - // that do not modify the frame data - int is_meta; InputFilter **inputs; int nb_inputs; @@ -339,8 +336,6 @@ typedef struct InputStream { int decoding_needed; /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */ #define DECODING_FOR_OST 1 #define DECODING_FOR_FILTER 2 - // should attach FrameData as opaque_ref after decoding - int want_frame_data; /** * Codec parameters - to be used by the decoding/streamcopy code. @@ -653,6 +648,8 @@ typedef struct FrameData { AVRational tb; AVRational frame_rate_filter; + + int bits_per_raw_sample; } FrameData; extern InputFile **input_files; diff --git a/fftools/ffmpeg_dec.c b/fftools/ffmpeg_dec.c index f9053c424b..e6c6e22b04 100644 --- a/fftools/ffmpeg_dec.c +++ b/fftools/ffmpeg_dec.c @@ -472,6 +472,7 @@ int dec_packet(InputStream *ist, const AVPacket *pkt, int no_eof) while (1) { AVFrame *frame = d->frame; + FrameData *fd; update_benchmark(NULL); ret = avcodec_receive_frame(dec, frame); @@ -508,19 +509,17 @@ int dec_packet(InputStream *ist, const AVPacket *pkt, int no_eof) exit_program(1); } - if (ist->want_frame_data) { - FrameData *fd; - av_assert0(!frame->opaque_ref); - fd = frame_data(frame); - if (!fd) { - av_frame_unref(frame); - report_and_exit(AVERROR(ENOMEM)); - } - fd->pts = frame->pts; - fd->tb = dec->pkt_timebase; - fd->idx = dec->frame_num - 1; + av_assert0(!frame->opaque_ref); + fd = frame_data(frame); + if (!fd) { + av_frame_unref(frame); + report_and_exit(AVERROR(ENOMEM)); } + fd->pts = frame->pts; + fd->tb = dec->pkt_timebase; + fd->idx = dec->frame_num - 1; + fd->bits_per_raw_sample = dec->bits_per_raw_sample; frame->time_base = dec->pkt_timebase; diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c index 0fdf7e7c4d..f42f4055d6 100644 --- a/fftools/ffmpeg_enc.c +++ b/fftools/ffmpeg_enc.c @@ -198,6 +198,7 @@ int enc_open(OutputStream *ost, AVFrame *frame) AVCodecContext *dec_ctx = NULL; const AVCodec *enc = enc_ctx->codec; OutputFile *of = output_files[ost->file_index]; + FrameData *fd = frame ? frame_data(frame) : NULL; int ret; if (e->opened) @@ -219,8 +220,8 @@ int enc_open(OutputStream *ost, AVFrame *frame) if (ost->bits_per_raw_sample) enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample; - else if (dec_ctx && ost->filter->graph->is_meta) - enc_ctx->bits_per_raw_sample = FFMIN(dec_ctx->bits_per_raw_sample, + else if (fd) + enc_ctx->bits_per_raw_sample = FFMIN(fd->bits_per_raw_sample, av_get_bytes_per_sample(enc_ctx->sample_fmt) << 3); enc_ctx->time_base = ost->enc_timebase.num > 0 ? ost->enc_timebase : @@ -230,10 +231,8 @@ int enc_open(OutputStream *ost, AVFrame *frame) case AVMEDIA_TYPE_VIDEO: { AVRational fr = ost->frame_rate; - if (!fr.num && frame) { - FrameData *fd = frame_data(frame); + if (!fr.num && fd) fr = fd->frame_rate_filter; - } if (!fr.num && !ost->max_frame_rate.num) { fr = (AVRational){25, 1}; av_log(ost, AV_LOG_WARNING, @@ -282,8 +281,8 @@ int enc_open(OutputStream *ost, AVFrame *frame) if (ost->bits_per_raw_sample) enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample; - else if (dec_ctx && ost->filter->graph->is_meta) - enc_ctx->bits_per_raw_sample = FFMIN(dec_ctx->bits_per_raw_sample, + else if (fd) + enc_ctx->bits_per_raw_sample = FFMIN(fd->bits_per_raw_sample, av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth); if (frame) { diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c index ccde5b26ec..9e6883ccdd 100644 --- a/fftools/ffmpeg_filter.c +++ b/fftools/ffmpeg_filter.c @@ -45,6 +45,9 @@ typedef struct FilterGraphPriv { char log_name[32]; int is_simple; + // true when the filtergraph contains only meta filters + // that do not modify the frame data + int is_meta; const char *graph_desc; @@ -1566,7 +1569,7 @@ static int configure_filtergraph(FilterGraph *fg) if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0) goto fail; - fg->is_meta = graph_is_meta(fg->graph); + fgp->is_meta = graph_is_meta(fg->graph); /* limit the lists of allowed formats to the ones selected, to * make sure they stay the same if the filtergraph is reconfigured later */ @@ -1714,6 +1717,8 @@ int reap_filters(int flush) filtered_frame = fgp->frame; while (1) { + FrameData *fd; + ret = av_buffersink_get_frame_flags(filter, filtered_frame, AV_BUFFERSINK_FLAG_NO_REQUEST); if (ret < 0) { @@ -1744,16 +1749,20 @@ int reap_filters(int flush) tb.num, tb.den); } - if (ost->type == AVMEDIA_TYPE_VIDEO) { - FrameData *fd = frame_data(filtered_frame); - if (!fd) { - av_frame_unref(filtered_frame); - report_and_exit(AVERROR(ENOMEM)); - } - - fd->frame_rate_filter = av_buffersink_get_frame_rate(filter); + fd = frame_data(filtered_frame); + if (!fd) { + av_frame_unref(filtered_frame); + report_and_exit(AVERROR(ENOMEM)); } + // only use bits_per_raw_sample passed through from the decoder + // if the filtergraph did not touch the frame data + if (!fgp->is_meta) + fd->bits_per_raw_sample = 0; + + if (ost->type == AVMEDIA_TYPE_VIDEO) + fd->frame_rate_filter = av_buffersink_get_frame_rate(filter); + enc_frame(ost, filtered_frame); av_frame_unref(filtered_frame); } diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index a18320fa9b..6ab541d7c5 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -365,15 +365,11 @@ static int enc_stats_init(OutputStream *ost, EncStats *es, int pre, c->type = fmt_specs[i].type; - if (fmt_specs[i].need_input_data) { - if (ost->ist) - ost->ist->want_frame_data = 1; - else { - av_log(ost, AV_LOG_WARNING, - "Format directive '%s' is unavailable, because " - "this output stream has no associated input stream\n", - val); - } + if (fmt_specs[i].need_input_data && !ost->ist) { + av_log(ost, AV_LOG_WARNING, + "Format directive '%s' is unavailable, because " + "this output stream has no associated input stream\n", + val); } break;