From b71069879010426ef8d7dce614086f8fb90eb0f3 Mon Sep 17 00:00:00 2001 From: Timo Rothenpieler Date: Sat, 6 Aug 2022 20:58:48 +0200 Subject: avcodec/nvenc: hardcode color characteristics for internal RGB2YUV conversion nvenc does not appear to use these values as inputs for its built in RGB to YUV conversion, but instead sets them on the output as-is. Testing indicates the input is expected to be sRGB, with the output ending up as limited range bt.470. --- libavcodec/nvenc.c | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c index e19378736f..11bd21f365 100644 --- a/libavcodec/nvenc.c +++ b/libavcodec/nvenc.c @@ -1096,11 +1096,20 @@ static av_cold int nvenc_setup_h264_config(AVCodecContext *avctx) NV_ENC_CONFIG_H264 *h264 = &cc->encodeCodecConfig.h264Config; NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters; - vui->colourMatrix = IS_GBRP(ctx->data_pix_fmt) ? AVCOL_SPC_RGB : avctx->colorspace; - vui->colourPrimaries = avctx->color_primaries; - vui->transferCharacteristics = avctx->color_trc; - vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG - || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ420P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ422P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ444P); + const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(ctx->data_pix_fmt); + + if ((pixdesc->flags & AV_PIX_FMT_FLAG_RGB) && !IS_GBRP(ctx->data_pix_fmt)) { + vui->colourMatrix = AVCOL_SPC_BT470BG; + vui->colourPrimaries = avctx->color_primaries; + vui->transferCharacteristics = avctx->color_trc; + vui->videoFullRangeFlag = 0; + } else { + vui->colourMatrix = IS_GBRP(ctx->data_pix_fmt) ? AVCOL_SPC_RGB : avctx->colorspace; + vui->colourPrimaries = avctx->color_primaries; + vui->transferCharacteristics = avctx->color_trc; + vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG + || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ420P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ422P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ444P); + } vui->colourDescriptionPresentFlag = (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2); @@ -1208,11 +1217,20 @@ static av_cold int nvenc_setup_hevc_config(AVCodecContext *avctx) NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig; NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters; - vui->colourMatrix = IS_GBRP(ctx->data_pix_fmt) ? AVCOL_SPC_RGB : avctx->colorspace; - vui->colourPrimaries = avctx->color_primaries; - vui->transferCharacteristics = avctx->color_trc; - vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG - || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ420P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ422P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ444P); + const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(ctx->data_pix_fmt); + + if ((pixdesc->flags & AV_PIX_FMT_FLAG_RGB) && !IS_GBRP(ctx->data_pix_fmt)) { + vui->colourMatrix = AVCOL_SPC_BT470BG; + vui->colourPrimaries = avctx->color_primaries; + vui->transferCharacteristics = avctx->color_trc; + vui->videoFullRangeFlag = 0; + } else { + vui->colourMatrix = IS_GBRP(ctx->data_pix_fmt) ? AVCOL_SPC_RGB : avctx->colorspace; + vui->colourPrimaries = avctx->color_primaries; + vui->transferCharacteristics = avctx->color_trc; + vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG + || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ420P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ422P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ444P); + } vui->colourDescriptionPresentFlag = (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2); -- cgit v1.2.3