From patchwork Tue May 23 13:58: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: 41794 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c51c:b0:10c:5e6f:955f with SMTP id gm28csp600113pzb; Tue, 23 May 2023 06:59:09 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5MvoG8TOD5Nu86KUW/I1xw6jUP1SpAWB9fE+L5Q1Z/362BLSD4oDzi8O3ZPRG038clKYeS X-Received: by 2002:a17:906:b044:b0:96f:4927:7a96 with SMTP id bj4-20020a170906b04400b0096f49277a96mr12054853ejb.70.1684850349481; Tue, 23 May 2023 06:59:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684850349; cv=none; d=google.com; s=arc-20160816; b=VI7yNoXFcmlMGJg1WOLg1iTpgW6MIfRVH10EJC/jPEKQyXbNq0iH/fQNZ+dbmB6T9Z k3dSDtLYS8Qc4EPdM3J4Fl3xsojTfKwDgeCejr+aOXCXiSj4WFWJh1s7/toFTWUIiMz4 gOUzYa0DhQp+wtym+6b2J9NSe+norWlifiRaDbgSAwVdnEvpQWcUAQC+r3OL4h1XQx98 Pmoq/lit88beQIPvfpAjGDJ1D+PFHUS2s9n5xhPjnrDTsECdVEmbEDMbq4oXryzPUMHR bfIa4MDeK9j+l0S4LxBai7OtAIhYXSnZ2Gp+VejcITNJaKqtLwDQ5+gECk55gOYGQsYY 0wOg== 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=h7+4InNIcEdRUBMXId6xfER9pMTdFvLhkXLlEGMRHFw=; b=Psgdnu3FxQPFX8FJ14RaRnM0xs1he1kJzuR3YsM8saiaMo3AVU3i3tx84hZAdHOO7Z HQDD9E1JcKMak0+LnQLfH1zxIOh3S28TXkewVQinWHXHT15M8yvfwr6Ip+gT1vY9Tmdh VOrqhIYAZb9lUI1ps9m8k+k/FS49dHxKQ5UoOZEgdlR+TuHzEXV3nE1LB8OY5S3dygy7 bWIdUvYqVxwAQWZGo4O1qRVVa638F+MDjvzsUK1y+Bgy3MeRTqXFKiK0MIko8ZIKAfH8 hk9oJUdPAhofXWhPoECJbnkAe3BE/xc+nqGLhPRVG6JRObaOq1/WhP/vHwpQKATtRF04 SbjA== 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 n21-20020a170906841500b00965a718330esi5580405ejx.164.2023.05.23.06.59.08; Tue, 23 May 2023 06:59:09 -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 C326668C0A7; Tue, 23 May 2023 16:59:03 +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 DD0EA68BF99 for ; Tue, 23 May 2023 16:58:49 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 910572406D0 for ; Tue, 23 May 2023 15:58:49 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id YjbFHjiqKrVj for ; Tue, 23 May 2023 15:58:48 +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 6929E2404EC for ; Tue, 23 May 2023 15:58:48 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 2DAD23A048F for ; Tue, 23 May 2023 15:58:48 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 23 May 2023 15:58:29 +0200 Message-Id: <20230523135842.20388-2-anton@khirnov.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230523135842.20388-1-anton@khirnov.net> References: <20230523135842.20388-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 02/15] fftools/ffmpeg_hw: move hw_device_setup_for_encode() to ffmpeg_enc 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: +oIHRXdWZyBC This function is entangled with encoder setup, so it is more encoding code rather than ffmpeg_hw code. This will allow making more encoder state private in the future. --- fftools/ffmpeg.h | 1 - fftools/ffmpeg_enc.c | 55 ++++++++++++++++++++++++++++++++++++++++++++ fftools/ffmpeg_hw.c | 55 -------------------------------------------- 3 files changed, 55 insertions(+), 56 deletions(-) diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 895edbd6d6..927e402f7c 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -802,7 +802,6 @@ int hw_device_init_from_type(enum AVHWDeviceType type, HWDevice **dev_out); void hw_device_free_all(void); -int hw_device_setup_for_encode(OutputStream *ost); /** * Get a hardware device to be used with this filtergraph. * The returned reference is owned by the callee, the caller diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c index 9b81d14922..7d99d9270b 100644 --- a/fftools/ffmpeg_enc.c +++ b/fftools/ffmpeg_enc.c @@ -103,6 +103,61 @@ fail: return AVERROR(ENOMEM); } +static int hw_device_setup_for_encode(OutputStream *ost) +{ + const AVCodecHWConfig *config; + HWDevice *dev = NULL; + AVBufferRef *frames_ref = NULL; + int i; + + if (ost->filter) { + frames_ref = av_buffersink_get_hw_frames_ctx(ost->filter->filter); + if (frames_ref && + ((AVHWFramesContext*)frames_ref->data)->format == + ost->enc_ctx->pix_fmt) { + // Matching format, will try to use hw_frames_ctx. + } else { + frames_ref = NULL; + } + } + + for (i = 0;; i++) { + config = avcodec_get_hw_config(ost->enc_ctx->codec, i); + if (!config) + break; + + if (frames_ref && + config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX && + (config->pix_fmt == AV_PIX_FMT_NONE || + config->pix_fmt == ost->enc_ctx->pix_fmt)) { + av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using input " + "frames context (format %s) with %s encoder.\n", + av_get_pix_fmt_name(ost->enc_ctx->pix_fmt), + ost->enc_ctx->codec->name); + ost->enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref); + if (!ost->enc_ctx->hw_frames_ctx) + return AVERROR(ENOMEM); + return 0; + } + + if (!dev && + config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) + dev = hw_device_get_by_type(config->device_type); + } + + if (dev) { + av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using device %s " + "(type %s) with %s encoder.\n", dev->name, + av_hwdevice_get_type_name(dev->type), ost->enc_ctx->codec->name); + ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref); + if (!ost->enc_ctx->hw_device_ctx) + return AVERROR(ENOMEM); + } else { + // No device required, or no device available. + } + return 0; +} + static void set_encoder_id(OutputFile *of, OutputStream *ost) { const char *cname = ost->enc_ctx->codec->name; diff --git a/fftools/ffmpeg_hw.c b/fftools/ffmpeg_hw.c index d28257a1d6..4a0b346fe1 100644 --- a/fftools/ffmpeg_hw.c +++ b/fftools/ffmpeg_hw.c @@ -297,61 +297,6 @@ void hw_device_free_all(void) nb_hw_devices = 0; } -int hw_device_setup_for_encode(OutputStream *ost) -{ - const AVCodecHWConfig *config; - HWDevice *dev = NULL; - AVBufferRef *frames_ref = NULL; - int i; - - if (ost->filter) { - frames_ref = av_buffersink_get_hw_frames_ctx(ost->filter->filter); - if (frames_ref && - ((AVHWFramesContext*)frames_ref->data)->format == - ost->enc_ctx->pix_fmt) { - // Matching format, will try to use hw_frames_ctx. - } else { - frames_ref = NULL; - } - } - - for (i = 0;; i++) { - config = avcodec_get_hw_config(ost->enc_ctx->codec, i); - if (!config) - break; - - if (frames_ref && - config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX && - (config->pix_fmt == AV_PIX_FMT_NONE || - config->pix_fmt == ost->enc_ctx->pix_fmt)) { - av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using input " - "frames context (format %s) with %s encoder.\n", - av_get_pix_fmt_name(ost->enc_ctx->pix_fmt), - ost->enc_ctx->codec->name); - ost->enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref); - if (!ost->enc_ctx->hw_frames_ctx) - return AVERROR(ENOMEM); - return 0; - } - - if (!dev && - config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) - dev = hw_device_get_by_type(config->device_type); - } - - if (dev) { - av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using device %s " - "(type %s) with %s encoder.\n", dev->name, - av_hwdevice_get_type_name(dev->type), ost->enc_ctx->codec->name); - ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref); - if (!ost->enc_ctx->hw_device_ctx) - return AVERROR(ENOMEM); - } else { - // No device required, or no device available. - } - return 0; -} - static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input) { InputStream *ist = avctx->opaque;