From patchwork Wed Jan 12 22:49:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Fabian 'Xaymar' Dirks X-Patchwork-Id: 33281 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5047167iog; Wed, 12 Jan 2022 14:50:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJwT+/NotKaRebpicXJum7Cmg/w/R7y3OYQnpJgujzQ325Z95cdTHo41yzYqyepCzm3vbq/5 X-Received: by 2002:a05:6402:2813:: with SMTP id h19mr1657576ede.365.1642027806082; Wed, 12 Jan 2022 14:50:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642027806; cv=none; d=google.com; s=arc-20160816; b=IG4c612n+Lh8PiwGPDME8WAXu/EM4/frAzafuaqSd1tMJ4sj7D2akHfnN6+ESWfTNa /oFhgX4txEq2Yp/DRTfAXaOLb/EM95S1DrNg2rtKEdMpeExUqgvWSc3rSNAfUBjDwViy fUZ9qOXNfDYuiMTqNpSuIVn1Wc1AS1eaMpUrFH3/XfsXNOHFK06tbF0eqvHtwkPR54xY gjH6oBKewfti4/tRF0UMCa/jSNKtb+V64Y+izCkR7kptXfAnL7WpgvjJy40jv2/xczAM oTPI4OaxAg6sdMfnB4o95ig06vr1nWiabzXzfhCcGp1tmK6zOpTnZE9ixAm7jBmwpev8 jqLQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc: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:dkim-signature:delivered-to; bh=0tSYlgYLrop1BJrt4jM+dfdhEv/z9v0WEn8tLFDPHOs=; b=o75mk68XDwPENio3iIhbW0ouyB0LBRvRfiQ/BHcPNtk1jseC2n5YMnRzCpWF1HVQC2 LoqLBJjU+FNgi/YVgHDH7aw3kEscexsWbYEGlobli1k7aNO4RqXV4i3gUaSEX7awEhv1 PReuELYmFNgD5R4pMy+vHNzhqDrKmewGUJEWQOW2EIOEgBbUP5rZYEcdh4rGrziiI9/m 7pUhjAB2hj3AaBrvgW/4e1zdnvH7FcAy7HOlcFEf0ugJ9wbUmdWf7XvhFPUvswufOrHP 6b7UaP4o1YTCSwRTTgdgRJ0xrgMJ0Pe6snlvyJRq94rGpXMQ4C2PasQOaYnNskl2BKFV 92NA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@xaymar.com header.s=2021 header.b=EudoQXCp; 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; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=xaymar.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id f19si608618edd.39.2022.01.12.14.50.05; Wed, 12 Jan 2022 14:50:06 -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; dkim=neutral (body hash did not verify) header.i=@xaymar.com header.s=2021 header.b=EudoQXCp; 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; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=xaymar.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0D97368A7ED; Thu, 13 Jan 2022 00:49:52 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.xaymar.com (xaymar.com [95.217.118.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 801FD6899A7 for ; Thu, 13 Jan 2022 00:49:44 +0200 (EET) Received: from localhost.localdomain (unknown [IPv6:2a02:8106:c:6a00:ccbc:cf2c:5bb9:d3cf]) by mail.xaymar.com (Postfix) with ESMTPSA id AA39961341; Wed, 12 Jan 2022 23:49:43 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xaymar.com; s=2021; t=1642027783; bh=b/353aHdmaIO4F2oI1ltV0rxbVBJuKPzo2XyoI4pcUg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EudoQXCpsaIjoKd8F22KG4SifWbUtqNfwyMI1XMhsfD4x6+nKMxUTQ+opsKGrdE+Y ZU1PbzpcYJsmPBhubdoNhrm7NRMKb8vcUkn9ARMNzX/vG+/GWPHYoBzumg8Hta/Xk+ notLU2ELYJGrwHQXnvz5v9rTEMJEP7+wTc/brNfH7rBpNELalxQ5fekYebzJSqMhRX ik4vyJmcp0K+fKV0HL98rK6wsSKDitI7nQB5Zhd+D5LG4DggGAYOK7I1WP0tFNTCBw rB3nPB9ois1u+DukU48/xNyWdthHNnl0BCYCe135lG9VWrRM2ufOmOLA2+eURMqiFs dey2dixmjUmGhnbuf9gGG4DhzkLm//PljzAwQBy6SXQzrsypexKHM1NPpQKJXcuMyQ sVUkTNlx2hFq5g+MbOY7G9gB0xOPian+4aUQQIwIJez/+SURmlqQ+Mdbex1yLr72Af O6xcKOWXnnec9+DI2XiuUKUZn3a3IYdgB8FkO/Y3B8KJmbyxt73OeZbYVLnVnBGhml W3U4flWqjsxFornNzLWPZ+XtGMjYRcXQ2+xalxkGG9NbAFXS3B9SXDNxRSXjQlDrFW TlgM1stSdwZCfNu5fL5vKvL5aoz08IyY7FICtoGPoputtTSjJ5/YYmyacC2bLtNF92 OShPaRBcww137QRrbIEw= From: Michael Fabian 'Xaymar' Dirks To: ffmpeg-devel@ffmpeg.org Date: Wed, 12 Jan 2022 23:49:21 +0100 Message-Id: X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <96117e46dbb4032a02d92c178ea4cc979203a0d3.1642027745.git.michael.dirks@xaymar.com> References: <96117e46dbb4032a02d92c178ea4cc979203a0d3.1642027745.git.michael.dirks@xaymar.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/5] avcodec/amfenc: Set all color metadata for AMF 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: Michael Fabian 'Xaymar' Dirks Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: URJktCTjtfZZ Fixes the color information in the output from the AMD encoder being wrong, which led to weird transcoding results. This problem appeared out of thin air, and I've been unable to tie it to a specific driver that supports my hardware. Unfortunately this requires AMF SDK version 1.4.23 or later, but it should still work fine on older drivers. Theoretical support for HDR encoding is also now possible, although the implementation is not complete. I have no clear idea on how to generate AMFs HDR metadata structure, or where to even take this information from. --- libavcodec/amfenc.h | 1 + libavcodec/amfenc_h264.c | 48 +++++++++++++++++++++++++++++++++++++- libavcodec/amfenc_hevc.c | 50 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/libavcodec/amfenc.h b/libavcodec/amfenc.h index 358b2ef778..951e529362 100644 --- a/libavcodec/amfenc.h +++ b/libavcodec/amfenc.h @@ -21,6 +21,7 @@ #include +#include #include #include diff --git a/libavcodec/amfenc_h264.c b/libavcodec/amfenc_h264.c index aeca99f7c6..009378e9f1 100644 --- a/libavcodec/amfenc_h264.c +++ b/libavcodec/amfenc_h264.c @@ -137,6 +137,8 @@ static av_cold int amf_encode_init_h264(AVCodecContext *avctx) AMFRate framerate; AMFSize framesize = AMFConstructSize(avctx->width, avctx->height); int deblocking_filter = (avctx->flags & AV_CODEC_FLAG_LOOP_FILTER) ? 1 : 0; + amf_int64 color_depth; + amf_int64 color_profile; if (avctx->framerate.num > 0 && avctx->framerate.den > 0) { framerate = AMFConstructRate(avctx->framerate.num, avctx->framerate.den); @@ -194,10 +196,54 @@ static av_cold int amf_encode_init_h264(AVCodecContext *avctx) AMF_ASSIGN_PROPERTY_RATIO(res, ctx->encoder, AMF_VIDEO_ENCODER_ASPECT_RATIO, ratio); } - /// Color Range (Partial/TV/MPEG or Full/PC/JPEG) + // Color Metadata + /// Color Range (Support for older Drivers) if (avctx->color_range == AVCOL_RANGE_JPEG) { AMF_ASSIGN_PROPERTY_BOOL(res, ctx->encoder, AMF_VIDEO_ENCODER_FULL_RANGE_COLOR, 1); + } else { + AMF_ASSIGN_PROPERTY_BOOL(res, ctx->encoder, AMF_VIDEO_ENCODER_FULL_RANGE_COLOR, 0); + } + /// Color Space & Depth + color_depth = AMF_COLOR_BIT_DEPTH_8; + color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_UNKNOWN; + switch (avctx->colorspace) { + case AVCOL_SPC_SMPTE170M: + if (avctx->color_range == AVCOL_RANGE_JPEG) { + color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_601; + } else { + color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_601; + } + break; + case AVCOL_SPC_BT709: + if (avctx->color_range == AVCOL_RANGE_JPEG) { + color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_709; + } else { + color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_709; + } + break; + case AVCOL_SPC_BT2020_NCL: + case AVCOL_SPC_BT2020_CL: + // !FIXME: Verify that this is correct on Hardware supporting it. + // !FIXME: Figure out how to decide on bit depth. + if (avctx->color_range == AVCOL_RANGE_JPEG) { + color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_2020; + color_depth = AMF_COLOR_BIT_DEPTH_10; + } else { + color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_2020; + color_depth = AMF_COLOR_BIT_DEPTH_10; + } + break; } + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_COLOR_BIT_DEPTH, color_depth); + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_INPUT_COLOR_PROFILE, color_profile); + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_OUTPUT_COLOR_PROFILE, color_profile); + /// Color Transfer Characteristics (AMF matches ISO/IEC) + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_INPUT_TRANSFER_CHARACTERISTIC, (amf_int64)avctx->color_trc); + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_OUTPUT_TRANSFER_CHARACTERISTIC, (amf_int64)avctx->color_trc); + /// Color Primaries (AMF matches ISO/IEC) + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_INPUT_COLOR_PRIMARIES, (amf_int64)avctx->color_primaries); + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_OUTPUT_COLOR_PRIMARIES, (amf_int64)avctx->color_primaries); + /// !TODO: AMF HDR Metadata generation // autodetect rate control method if (ctx->rate_control_mode == AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_UNKNOWN) { diff --git a/libavcodec/amfenc_hevc.c b/libavcodec/amfenc_hevc.c index 79541b9b2f..900e1482b4 100644 --- a/libavcodec/amfenc_hevc.c +++ b/libavcodec/amfenc_hevc.c @@ -104,6 +104,8 @@ static av_cold int amf_encode_init_hevc(AVCodecContext *avctx) AMFRate framerate; AMFSize framesize = AMFConstructSize(avctx->width, avctx->height); int deblocking_filter = (avctx->flags & AV_CODEC_FLAG_LOOP_FILTER) ? 1 : 0; + amf_int64 color_depth; + amf_int64 color_profile; if (avctx->framerate.num > 0 && avctx->framerate.den > 0) { framerate = AMFConstructRate(avctx->framerate.num, avctx->framerate.den); @@ -152,6 +154,54 @@ static av_cold int amf_encode_init_hevc(AVCodecContext *avctx) AMFRatio ratio = AMFConstructRatio(avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den); AMF_ASSIGN_PROPERTY_RATIO(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_ASPECT_RATIO, ratio); } + + // Color Metadata + /// Color Range (Support for older Drivers) + if (avctx->color_range == AVCOL_RANGE_JPEG) { + AMF_ASSIGN_PROPERTY_BOOL(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_NOMINAL_RANGE, 1); + } else { + AMF_ASSIGN_PROPERTY_BOOL(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_NOMINAL_RANGE, 0); + } + /// Color Space & Depth + color_depth = AMF_COLOR_BIT_DEPTH_8; + color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_UNKNOWN; + switch (avctx->colorspace) { + case AVCOL_SPC_SMPTE170M: + if (avctx->color_range == AVCOL_RANGE_JPEG) { + color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_601; + } else { + color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_601; + } + break; + case AVCOL_SPC_BT709: + if (avctx->color_range == AVCOL_RANGE_JPEG) { + color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_709; + } else { + color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_709; + } + break; + case AVCOL_SPC_BT2020_NCL: + case AVCOL_SPC_BT2020_CL: + // !FIXME: Verify that this is correct on Hardware supporting it. + // !FIXME: Figure out how to decide on bit depth. + if (avctx->color_range == AVCOL_RANGE_JPEG) { + color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_2020; + color_depth = AMF_COLOR_BIT_DEPTH_10; + } else { + color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_2020; + color_depth = AMF_COLOR_BIT_DEPTH_10; + } + break; + } + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_COLOR_BIT_DEPTH, color_depth); + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_INPUT_COLOR_PROFILE, color_profile); + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_OUTPUT_COLOR_PROFILE, color_profile); + /// Color Transfer Characteristics (AMF matches ISO/IEC) + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_INPUT_TRANSFER_CHARACTERISTIC, (amf_int64)avctx->color_trc); + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_OUTPUT_TRANSFER_CHARACTERISTIC, (amf_int64)avctx->color_trc); + /// Color Primaries (AMF matches ISO/IEC) + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_INPUT_COLOR_PRIMARIES, (amf_int64)avctx->color_primaries); + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_OUTPUT_COLOR_PRIMARIES, (amf_int64)avctx->color_primaries); // Picture control properties AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_NUM_GOPS_PER_IDR, ctx->gops_per_idr);