From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 2A26A481B8 for ; Fri, 12 Jan 2024 08:30:54 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BCB7F68D09E; Fri, 12 Jan 2024 10:30:08 +0200 (EET) Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id ABAB868D068 for ; Fri, 12 Jan 2024 10:30:04 +0200 (EET) Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id 5C57D4BA15; Fri, 12 Jan 2024 09:30:04 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1705048204; bh=7O/z/0sG355q7oHxepGR/BtQogCliVZj6WjOTqS28Wg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oeqDGXUl+Cbws5zkN/p3P0HnHfQ2Fcv7SN7pG3MjvU9OitVk8hC9sY16zVKNSxHPI TxbLOZWVdru+OjrumSbpQURlM8SGC8qLZaBAnE8ESO2gLcaCclkvAJliSm1V1Jk9XS eOPvsM+xtW/PbjJEj2dGS1ipJYcZd7TntiDjPGqY= From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Fri, 12 Jan 2024 09:26:08 +0100 Message-ID: <20240112082950.41637-12-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240112082950.41637-1-ffmpeg@haasn.xyz> References: <20240112082950.41637-1-ffmpeg@haasn.xyz> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 11/11] avcodec/encode: enforce AVCodec capabilities at encode time 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 Cc: Niklas Haas Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: From: Niklas Haas In general, the logic is always the same: if the codec supports only a single format, enforce it if possible. Otherwise, throw an error when an incompatible format is passed. To preserve backwards compatibility and make this check less pedantic than it needs to be, always consider 'AVCOL_*_UNSPECIFIED' a valid value. That way, we will only throw an error if the user explicitly set a color value that we explicitly know is unsupported. --- libavcodec/encode.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/libavcodec/encode.c b/libavcodec/encode.c index 31565638fa..32eb7dd9eb 100644 --- a/libavcodec/encode.c +++ b/libavcodec/encode.c @@ -574,10 +574,23 @@ int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket * return 0; } +static int fmt_is_regular_yuv(enum AVPixelFormat fmt) +{ + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt); + if (!desc || desc->nb_components < 3) + return 0; + if (desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_PAL | + AV_PIX_FMT_FLAG_XYZ | AV_PIX_FMT_FLAG_FLOAT)) + return 0; + return 1; +} + + static int encode_preinit_video(AVCodecContext *avctx) { const AVCodec *c = avctx->codec; const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt); + enum AVPixelFormat swfmt = avctx->pix_fmt; int i; if (!av_get_pix_fmt_name(avctx->pix_fmt)) { @@ -605,6 +618,52 @@ static int encode_preinit_video(AVCodecContext *avctx) } } + if (pixdesc->flags & AV_PIX_FMT_FLAG_HWACCEL) { + const AVHWFramesContext *hwfc = (void *) avctx->hw_frames_ctx->data; + swfmt = hwfc->sw_format; + } + + if (fmt_is_regular_yuv(swfmt)) { + if (avctx->colorspace != AVCOL_SPC_UNSPECIFIED && c->color_spaces) { + for (i = 0; c->color_spaces[i] != AVCOL_SPC_UNSPECIFIED; i++) + if (avctx->colorspace == c->color_spaces[i]) + break; + if (c->color_spaces[i] == AVCOL_SPC_UNSPECIFIED) { + av_log(avctx, AV_LOG_ERROR, + "Specified color space %s is not supported by the %s encoder.\n", + av_color_space_name(avctx->colorspace), c->name); + av_log(avctx, AV_LOG_ERROR, "Supported color spaces:\n"); + for (int p = 0; c->color_spaces[p] != AVCOL_SPC_UNSPECIFIED; p++) { + av_log(avctx, AV_LOG_ERROR, " %s\n", + av_color_space_name(c->color_spaces[p])); + } + return AVERROR(EINVAL); + } + } + + if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED && c->color_ranges) { + for (i = 0; c->color_ranges[i] != AVCOL_RANGE_UNSPECIFIED; i++) { + if (avctx->color_range == c->color_ranges[i]) + break; + } + if (c->color_ranges[i] == AVCOL_RANGE_UNSPECIFIED) { + if (i == 1 && !avctx->color_range) { + avctx->color_range = c->color_ranges[0]; + } else if (avctx->color_range) { + av_log(avctx, AV_LOG_ERROR, + "Specified color range %s is not supported by the %s encoder.\n", + av_color_range_name(avctx->color_range), c->name); + av_log(avctx, AV_LOG_ERROR, "Supported color ranges:\n"); + for (int p = 0; c->color_ranges[p] != AVCOL_RANGE_UNSPECIFIED; p++) { + av_log(avctx, AV_LOG_ERROR, " %s\n", + av_color_range_name(c->color_ranges[p])); + } + return AVERROR(EINVAL); + } + } + } + } + if ( avctx->bits_per_raw_sample < 0 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) { av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n", -- 2.43.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".