From patchwork Sat Jul 15 10:45:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 42693 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6da1:b0:131:a7d0:bc6d with SMTP id gl33csp4239168pzb; Sat, 15 Jul 2023 03:47:06 -0700 (PDT) X-Google-Smtp-Source: APBJJlFw8J6V0sZV45MAmEQ3c6re+qOsKSQ/rqSRcKqv5zKVFbP3j/PHL8BwSjpAgi0RHe+GYxk/ X-Received: by 2002:a17:906:3f1c:b0:98d:f953:f473 with SMTP id c28-20020a1709063f1c00b0098df953f473mr6592530ejj.50.1689418025939; Sat, 15 Jul 2023 03:47:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689418025; cv=none; d=google.com; s=arc-20160816; b=z5qAEqZ5ZvJPNQh7tVnpvDs7RT9ajAC1e9PZdf7sdmlpyLvLLA2MVxKRQYiRCNx9aK xJ2lsCeuvHOuyGBF9auS9HGAfRQtV4OrHNBVk4GDctoRBpaV5KSCN1QdDe9FzxZvMydg zey9kNdQK0OCLRy2B/4IzG9loAzFiT9/WJ0RXXlZib56WE1whTil4oSGsJOxqGGs7C/r yfBkMhWmHEU6Kx7UYDK2xyv+uebvRE5qEyURKnFbsPnlrP4Wkm7PAA8Wu5es9R+C3Bhp S/ClMpEmhg5ZhZDleur+V2Ch65Q1tY7gwnQfQ2fj28baWP3qn1sGvczygZv/jQj+8BP9 PDbQ== 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=T44YInkRsV3rXnT+S1gJs8gYNXnQeYWUaphaQdjMak4=; fh=8ZF2QtiKOjBtyn9wuO2cMeZaLsE4aq02Bq77gGK61E8=; b=mieSANoR5Dhi/EmBzbjmngT7aPaED4KeNEpHXThWAFfkjnWeO1YtA3/QTLAtHVVp3X nwRfUKXi4hy71YdZiHUFCW5SHgeeky09VxONBKl5TV+xszFPIByfDraZzFqnWpAgl2JG DXxxtSF2B0GJd6apx+kHsGlQUe0Fm9m/7WS5yFC0IHUP5PGDgvA7AtztO/f/Fc/aGzGM bPmhEr7aUVNgAs4+nNL0fudIvw7auWp0nwmmCrIhzkugpK7IS+TS77/k6ex6K2o/AP+t Fp6riFcpSYlDPLs/WEE5X7XgpSajSDVm/7Sqw3Tx4BcDiIxxKh72/w8Q8P0fVycVraBH 0irg== 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 v7-20020a170906564700b0098e3890e257si10211531ejr.131.2023.07.15.03.47.05; Sat, 15 Jul 2023 03:47:05 -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 0C46568C62C; Sat, 15 Jul 2023 13:46:37 +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 7979968C550 for ; Sat, 15 Jul 2023 13:46:29 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 3D9D02404EC for ; Sat, 15 Jul 2023 12:46:29 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id h0JFiUHpuAHK for ; Sat, 15 Jul 2023 12:46:27 +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 7B6652404F8 for ; Sat, 15 Jul 2023 12:46:26 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 2DEF33A1251 for ; Sat, 15 Jul 2023 12:46:20 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Sat, 15 Jul 2023 12:45:29 +0200 Message-Id: <20230715104611.17902-5-anton@khirnov.net> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230715104611.17902-1-anton@khirnov.net> References: <20230715104611.17902-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 05/47] fftools/ffmpeg_filter: stop accessing encoder from pixfmt selection 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: LLh3YqIi30pg ffmpeg CLI pixel format selection for filtering currently special-cases MJPEG encoding, where it will restrict the supported list of pixel formats depending on the value of the -strict option. In order to get that value it will apply it from the options dict into the encoder context, which is a highly invasive action even now, and would become a race once encoding is moved to its own thread. The ugliness of this code can be much reduced by moving the special handling of MJPEG into ofilter_bind_ost(), which is called from encoder init and is thus synchronized with it. There is also no need to write anything to the encoder context, we can evaluate the option into our stack variable. There is also no need to access AVCodec at all during pixel format selection, as the pixel formats array is already stored in OutputFilterPriv. --- fftools/ffmpeg_filter.c | 64 ++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c index 71595513f3..e14b8f0f3c 100644 --- a/fftools/ffmpeg_filter.c +++ b/fftools/ffmpeg_filter.c @@ -270,46 +270,22 @@ static void sub2video_update(InputFilterPriv *ifp, int64_t heartbeat_pts, ifp->sub2video.initialize = 0; } -// FIXME: YUV420P etc. are actually supported with full color range, -// yet the latter information isn't available here. -static const enum AVPixelFormat *get_compliance_normal_pix_fmts(const AVCodec *codec, const enum AVPixelFormat default_formats[]) -{ - static const enum AVPixelFormat mjpeg_formats[] = - { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, - AV_PIX_FMT_NONE }; - - if (!strcmp(codec->name, "mjpeg")) { - return mjpeg_formats; - } else { - return default_formats; - } -} - /* May return NULL (no pixel format found), a static string or a string * backed by the bprint. Nothing has been written to the AVBPrint in case * NULL is returned. The AVBPrint provided should be clean. */ static const char *choose_pix_fmts(OutputFilter *ofilter, AVBPrint *bprint) { + OutputFilterPriv *ofp = ofp_from_ofilter(ofilter); OutputStream *ost = ofilter->ost; - AVCodecContext *enc = ost->enc_ctx; - const AVDictionaryEntry *strict_dict = av_dict_get(ost->encoder_opts, "strict", NULL, 0); - if (strict_dict) - av_opt_set(ost->enc_ctx, "strict", strict_dict->value, 0); - if (ost->keep_pix_fmt) { - if (ost->enc_ctx->pix_fmt == AV_PIX_FMT_NONE) - return NULL; - return av_get_pix_fmt_name(ost->enc_ctx->pix_fmt); + if (ost->keep_pix_fmt) { + return ofp->format == AV_PIX_FMT_NONE ? NULL : + av_get_pix_fmt_name(ofp->format); } - if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) { - return av_get_pix_fmt_name(enc->pix_fmt); - } else if (enc->codec->pix_fmts) { - const enum AVPixelFormat *p; - - p = enc->codec->pix_fmts; - if (ost->enc_ctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) { - p = get_compliance_normal_pix_fmts(enc->codec, p); - } + if (ofp->format != AV_PIX_FMT_NONE) { + return av_get_pix_fmt_name(ofp->format); + } else if (ofp->formats) { + const enum AVPixelFormat *p = ofp->formats; for (; *p != AV_PIX_FMT_NONE; p++) { const char *name = av_get_pix_fmt_name(*p); @@ -668,6 +644,30 @@ void ofilter_bind_ost(OutputFilter *ofilter, OutputStream *ost) ofp->format = ost->enc_ctx->pix_fmt; } else { ofp->formats = c->pix_fmts; + + // MJPEG encoder exports a full list of supported pixel formats, + // but the full-range ones are experimental-only. + // Restrict the auto-conversion list unless -strict experimental + // has been specified. + if (!strcmp(c->name, "mjpeg")) { + // FIXME: YUV420P etc. are actually supported with full color range, + // yet the latter information isn't available here. + static const enum AVPixelFormat mjpeg_formats[] = + { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, + AV_PIX_FMT_NONE }; + + const AVDictionaryEntry *strict = av_dict_get(ost->encoder_opts, "strict", NULL, 0); + int strict_val = ost->enc_ctx->strict_std_compliance; + + if (strict) { + const AVOption *o = av_opt_find(ost->enc_ctx, strict->key, NULL, 0, 0); + av_assert0(o); + av_opt_eval_int(ost->enc_ctx, o, strict->value, &strict_val); + } + + if (strict_val > FF_COMPLIANCE_UNOFFICIAL) + ofp->formats = mjpeg_formats; + } } fgp->disable_conversions |= ost->keep_pix_fmt;