Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Timo Rothenpieler <timo@rothenpieler.org>
To: ffmpeg-devel@ffmpeg.org
Subject: Re: [FFmpeg-devel] [PATCH] avcodec/nvenc: add support for writing mastering metadata SEI messages
Date: Tue, 4 Feb 2025 18:16:26 +0100
Message-ID: <f1ff5e0a-9984-401a-a027-350ad4988761@rothenpieler.org> (raw)
In-Reply-To: <20250204163033.3767-1-jamrial@gmail.com>

On 04.02.2025 17:30, James Almer wrote:
> Including Mastering Display and Content Light Level.
> Requires SDK 13.0, and only supports HEVC and AV1.
> 
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>   libavcodec/nvenc.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++
>   libavcodec/nvenc.h |  2 ++
>   2 files changed, 84 insertions(+)
> 
> diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c
> index 1f6b670c1b..9780fc712c 100644
> --- a/libavcodec/nvenc.c
> +++ b/libavcodec/nvenc.c
> @@ -35,6 +35,7 @@
>   #include "libavutil/mem.h"
>   #include "libavutil/pixdesc.h"
>   #include "libavutil/mathematics.h"
> +#include "libavutil/mastering_display_metadata.h"
>   #include "atsc_a53.h"
>   #include "codec_desc.h"
>   #include "encode.h"
> @@ -1470,6 +1471,15 @@ static av_cold int nvenc_setup_hevc_config(AVCodecContext *avctx)
>   #endif
>       }
>   
> +#ifdef NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
> +    ctx->mdm = hevc->outputMasteringDisplay = !!av_frame_side_data_get(avctx->decoded_side_data,
> +                                                                       avctx->nb_decoded_side_data,
> +                                                                       AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
> +    ctx->cll = hevc->outputMaxCll           = !!av_frame_side_data_get(avctx->decoded_side_data,
> +                                                                       avctx->nb_decoded_side_data,
> +                                                                       AV_FRAME_DATA_CONTENT_LIGHT_LEVEL);
> +#endif
> +
>   #ifdef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
>       if (ctx->constrained_encoding)
>           hevc->enableConstrainedEncoding = 1;
> @@ -1638,6 +1648,15 @@ static av_cold int nvenc_setup_av1_config(AVCodecContext *avctx)
>       av1->pixelBitDepthMinus8 = (IS_10BIT(ctx->data_pix_fmt) || ctx->highbitdepth) ? 2 : 0;
>   #endif
>   
> +#ifdef NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
> +    ctx->mdm = av1->outputMasteringDisplay = !!av_frame_side_data_get(avctx->decoded_side_data,
> +                                                                      avctx->nb_decoded_side_data,
> +                                                                      AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
> +    ctx->cll = av1->outputMaxCll           = !!av_frame_side_data_get(avctx->decoded_side_data,
> +                                                                      avctx->nb_decoded_side_data,
> +                                                                      AV_FRAME_DATA_CONTENT_LIGHT_LEVEL);
> +#endif
> +
>       if (ctx->b_ref_mode >= 0)
>           av1->useBFramesAsRef = ctx->b_ref_mode;
>   
> @@ -2892,6 +2911,10 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame)
>       int res, res2;
>       int sei_count = 0;
>       int i;
> +#ifdef NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
> +    MASTERING_DISPLAY_INFO mastering_disp_info = { 0 };
> +    CONTENT_LIGHT_LEVEL content_light_level = { 0 };
> +#endif
>   
>       NvencContext *ctx = avctx->priv_data;
>       NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
> @@ -2956,6 +2979,65 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame)
>               sei_count = res;
>           }
>   
> +#ifdef NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
> +        if (ctx->mdm || ctx->cll) {
> +            const AVFrameSideData *sd_mdm = av_frame_get_side_data(frame, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
> +            const AVFrameSideData *sd_cll = av_frame_get_side_data(frame, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL);
> +            const int chroma_den =   (avctx->codec->id == AV_CODEC_ID_AV1) ? 1 << 16 : 50000;
> +            const int max_luma_den = (avctx->codec->id == AV_CODEC_ID_AV1) ? 1 << 8  : 10000;
> +            const int min_luma_den = (avctx->codec->id == AV_CODEC_ID_AV1) ? 1 << 14 : 10000;
> +
> +            if (!sd_mdm)
> +                sd_mdm = av_frame_side_data_get(avctx->decoded_side_data,
> +                                                avctx->nb_decoded_side_data,
> +                                                AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
> +            if (!sd_cll)
> +                sd_cll = av_frame_side_data_get(avctx->decoded_side_data,
> +                                                avctx->nb_decoded_side_data,
> +                                                AV_FRAME_DATA_CONTENT_LIGHT_LEVEL);
> +
> +            if (sd_mdm) {
> +                const AVMasteringDisplayMetadata *mdm = (AVMasteringDisplayMetadata *)sd_mdm->data;
> +
> +                mastering_disp_info.r.x = av_rescale(mdm->display_primaries[0][0].num, chroma_den,
> +                                                     mdm->display_primaries[0][0].den);
> +                mastering_disp_info.r.y = av_rescale(mdm->display_primaries[0][1].num, chroma_den,
> +                                                     mdm->display_primaries[0][1].den);
> +                mastering_disp_info.g.x = av_rescale(mdm->display_primaries[1][0].num, chroma_den,
> +                                                     mdm->display_primaries[1][0].den);
> +                mastering_disp_info.g.y = av_rescale(mdm->display_primaries[1][1].num, chroma_den,
> +                                                     mdm->display_primaries[1][1].den);
> +                mastering_disp_info.b.x = av_rescale(mdm->display_primaries[2][0].num, chroma_den,
> +                                                     mdm->display_primaries[2][0].den);
> +                mastering_disp_info.b.y = av_rescale(mdm->display_primaries[2][1].num, chroma_den,
> +                                                     mdm->display_primaries[2][1].den);
> +                mastering_disp_info.whitePoint.x = av_rescale(mdm->white_point[0].num, chroma_den,
> +                                                              mdm->white_point[0].den);
> +                mastering_disp_info.whitePoint.y = av_rescale(mdm->white_point[1].num, chroma_den,
> +                                                              mdm->white_point[1].den);
> +                mastering_disp_info.maxLuma = av_rescale(mdm->max_luminance.num, max_luma_den,
> +                                                         mdm->max_luminance.den);
> +                mastering_disp_info.minLuma = av_rescale(mdm->min_luminance.num, min_luma_den,
> +                                                         mdm->min_luminance.den);
> +
> +                if (avctx->codec->id == AV_CODEC_ID_HEVC)
> +                    pic_params.codecPicParams.hevcPicParams.pMasteringDisplay = &mastering_disp_info;
> +                else
> +                    pic_params.codecPicParams.av1PicParams.pMasteringDisplay = &mastering_disp_info;
> +            }
> +            if (sd_cll) {
> +                const AVContentLightMetadata *cll = (AVContentLightMetadata *)sd_cll->data;
> +
> +                content_light_level.maxContentLightLevel = cll->MaxCLL;
> +                content_light_level.maxPicAverageLightLevel = cll->MaxFALL;
> +
> +                if (avctx->codec->id == AV_CODEC_ID_HEVC)
> +                    pic_params.codecPicParams.hevcPicParams.pMaxCll = &content_light_level;
> +                else
> +                    pic_params.codecPicParams.av1PicParams.pMaxCll = &content_light_level;

I'd prefer if this and the same check above would also check for == AV1 
and bug out otherwise.
Otherwise this looks good to me.

> +            }
> +        }
> +#endif
>           res = nvenc_store_frame_data(avctx, &pic_params, frame);
>           if (res < 0)
>               return res;
> diff --git a/libavcodec/nvenc.h b/libavcodec/nvenc.h
> index 55a95f8399..dbd18cac12 100644
> --- a/libavcodec/nvenc.h
> +++ b/libavcodec/nvenc.h
> @@ -104,6 +104,7 @@ typedef void ID3D11Device;
>   #define NVENC_HAVE_422_SUPPORT
>   #define NVENC_HAVE_AV1_UHQ_TUNING
>   #define NVENC_HAVE_H264_AND_AV1_TEMPORAL_FILTER
> +#define NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
>   #endif
>   
>   typedef struct NvencSurface
> @@ -306,6 +307,7 @@ typedef struct NvencContext
>       int lookahead_level;
>       int unidir_b;
>       int split_encode_mode;
> +    int mdm, cll;
>   } NvencContext;
>   
>   int ff_nvenc_encode_init(AVCodecContext *avctx);

_______________________________________________
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".

      reply	other threads:[~2025-02-04 17:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-04 16:30 James Almer
2025-02-04 17:16 ` Timo Rothenpieler [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f1ff5e0a-9984-401a-a027-350ad4988761@rothenpieler.org \
    --to=timo@rothenpieler.org \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git