From patchwork Mon Apr 15 09:04:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Araz Iusubov X-Patchwork-Id: 48065 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:670b:b0:1a9:af23:56c1 with SMTP id wh11csp1713469pzb; Mon, 15 Apr 2024 02:05:06 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXKcpF3YJ9nqSN4G2dE8W4s3fThFSrWZgllRYbuPxTP2Wr9aynFv0sWFwY4Yg6+dvq6V38xve4OP1NXF6vTT0ogN4qdXePlA+dC0Q== X-Google-Smtp-Source: AGHT+IFZagYh041mJkDEfJ6VgCRYx/0jrf44UEdG98HsyNZu/E/mlR4eNYyxWWfpYiPNquXgGlzA X-Received: by 2002:a17:906:5fc2:b0:a51:ae51:5206 with SMTP id k2-20020a1709065fc200b00a51ae515206mr4937866ejv.42.1713171906712; Mon, 15 Apr 2024 02:05:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1713171906; cv=none; d=google.com; s=arc-20160816; b=gb8SlV4bDyTGttVg9peRdJgOD50nZhSFuV3ny/mPdU/auRt+H28gNRYvV16fdmTXJ3 MvOBfuEFGC+HMQKCD6JDy+ZFa9SYxVqH9oUB520wfaxeznmKtscnD1FAYNIo2mSzRs0J WjglQEB4sAGOaelnVHxi17+y5oA8nvbk7BwkqC3OuSuePvkbkaM3u/mTONQkxgDAO77Y MBPrZ8qQD+OPVFLvmIO1tYpMrbclapF9LhrC7nZ3vOIpIsDp/ZfOIUd7rE0hjHzThBjm 8ILHEl7uyDB/A/s9mh33uBEzDu/5fwXoIPE5POe78MdM7/WxFK94GlyG5XjGgYEaM/Wh HxCg== 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=1DFZdSaIXljhFjfBLKI2GwbitBoQC/791Ky1EvxsguU=; fh=s4NEqlgyeQvuJ9M2FER/qIrd96lZ1v/frktaqQpFGmg=; b=b+Fk/uAP/A4xfume2XmxjSPrazfAyC5iQP4iebzN02a1vwXakYNr6pd/LmF0+JoZ9n tQsIZKsbp4zPV+3Ow0jb9U0QU+67ISPWvPd363tx+YJkOmk0rwDV2RjexoRabtE2l6fZ 5jWY9dDFZ0PnMRGu4gjBg+BGYfZmHhXmfFFDxnK+cfiwArfni1WkyecWCMsqmGbHgkF/ X9L0ah18FUsy61M005e1+/Yv9oW/kgmy9BTtATsA/tDE30Dup7OQ+7YNkpDIJ4w6rhdv hPr50w669tXRIh3aBLqpDL3ooPed6WANz6ujfoPwpWkuCn0mXRgbByK7b6qetfM/SdQq AVXA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=IaFNhH9H; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id qk30-20020a1709077f9e00b00a4e256a82ccsi4587372ejc.232.2024.04.15.02.05.06; Mon, 15 Apr 2024 02:05:06 -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; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=IaFNhH9H; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D260468D3DE; Mon, 15 Apr 2024 12:05:02 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f43.google.com (mail-ej1-f43.google.com [209.85.218.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A512468D3BE for ; Mon, 15 Apr 2024 12:04:55 +0300 (EEST) Received: by mail-ej1-f43.google.com with SMTP id a640c23a62f3a-a450bedffdfso354992066b.3 for ; Mon, 15 Apr 2024 02:04:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713171895; x=1713776695; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HcNco338gbhERxqFBR0RbryTDtAseuSjaMuiWA73MjI=; b=IaFNhH9HOdkvJKDmQwev709THYEsMS4CY8q/+SichnxTUw1qKjc4h5TbThc0xSIdqC 0nDyTqwoR3FF+SJA5poXu4Ss0P75p8as2gAcV7POMwwUH8zLZ1eNFhP4RmL4gPX4qvLP gqaWFbRks44P+9auyu0AT92xUn4xW8ve1aMrnVc+DeGxd+TXuCwCzYvsUAxZytXSacu2 FBEz0a0zgB9D6Y6+nKf2FBHKZL+isdbGB+ug/0quuvHPt5146CcyQn6JvLSULivMNJQB Fjk9LmwGOmMJnNYKDUNiEZEsCADdZBRzyBafAqc+62bnyLGRq4XG+d56CcsRB3E+ZHV7 oN4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713171895; x=1713776695; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HcNco338gbhERxqFBR0RbryTDtAseuSjaMuiWA73MjI=; b=I4HG+6ZmopdZVRpxLZ5Lf1Z+txbREH4q6Kz5267Q4eJ9Gh22RaB9uagslO64JYSu7x LwVHnGQCCEdS1jGg5r0NVbhLriTcc2d2bgegd1+0h+KRG9po4AWTlr497DB0uTmJU4d3 qq2b0WD/SHSdZbSaLsPoY0RF8vgM0aEkBPv1lHojSkFD+eCVDC8PUnihiZqzXaXR1iD7 tIX9bSiNW320JJdTGZHVemoCdgtr4X0NZND76oDHhOHAFZJKck74UFE63F26+mw38VK2 3KMvzkXi1oQyKCJol3DWwk8q+RyGNLrGs4yaWEYIk+ZWcnIyUgRtZ9kkCI7i+kDsHRUM ztOA== X-Gm-Message-State: AOJu0YxDBg9O4gmz9nlEG7e/dAsXq2HUHZ559M22tfpeLf7jqEndB56C ncFsC8mBknFNmqxJBrL7YmYKLYJcHq4pJX4jt9rEKNcO9V+VCqEF5sDxYA== X-Received: by 2002:a17:906:3b11:b0:a52:4fc6:2d6a with SMTP id g17-20020a1709063b1100b00a524fc62d6amr3096509ejf.32.1713171894474; Mon, 15 Apr 2024 02:04:54 -0700 (PDT) Received: from aiusubov.amd.com ([87.116.133.126]) by smtp.gmail.com with ESMTPSA id ay8-20020a05600c1e0800b004181c91d1dcsm9283139wmb.18.2024.04.15.02.04.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Apr 2024 02:04:54 -0700 (PDT) From: Araz Iusubov X-Google-Original-From: Araz Iusubov To: ffmpeg-devel@ffmpeg.org Date: Mon, 15 Apr 2024 11:04:08 +0200 Message-ID: <20240415090410.1138-2-Primeadvice@gmail.com> X-Mailer: git-send-email 2.43.0.windows.1 In-Reply-To: <20240415090410.1138-1-Primeadvice@gmail.com> References: <20240415090410.1138-1-Primeadvice@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/4] avcodec/amfenc: HDR metadata. 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: nyanmisaka Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: aLL03k3mNP7Z From: nyanmisaka v2: fixes for indentation --- libavcodec/amfenc.c | 83 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c index 0bd15dd812..068bb53002 100644 --- a/libavcodec/amfenc.c +++ b/libavcodec/amfenc.c @@ -36,6 +36,57 @@ #include "amfenc.h" #include "encode.h" #include "internal.h" +#include "libavutil/mastering_display_metadata.h" + +static int amf_save_hdr_metadata(AVCodecContext *avctx, const AVFrame *frame, AMFHDRMetadata *hdrmeta) +{ + AVFrameSideData *sd_display; + AVFrameSideData *sd_light; + AVMasteringDisplayMetadata *display_meta; + AVContentLightMetadata *light_meta; + + sd_display = av_frame_get_side_data(frame, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA); + if (sd_display) { + display_meta = (AVMasteringDisplayMetadata *)sd_display->data; + if (display_meta->has_luminance) { + const unsigned int luma_den = 10000; + hdrmeta->maxMasteringLuminance = + (amf_uint32)(luma_den * av_q2d(display_meta->max_luminance)); + hdrmeta->minMasteringLuminance = + FFMIN((amf_uint32)(luma_den * av_q2d(display_meta->min_luminance)), hdrmeta->maxMasteringLuminance); + } + if (display_meta->has_primaries) { + const unsigned int chroma_den = 50000; + hdrmeta->redPrimary[0] = + FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->display_primaries[0][0])), chroma_den); + hdrmeta->redPrimary[1] = + FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->display_primaries[0][1])), chroma_den); + hdrmeta->greenPrimary[0] = + FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->display_primaries[1][0])), chroma_den); + hdrmeta->greenPrimary[1] = + FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->display_primaries[1][1])), chroma_den); + hdrmeta->bluePrimary[0] = + FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->display_primaries[2][0])), chroma_den); + hdrmeta->bluePrimary[1] = + FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->display_primaries[2][1])), chroma_den); + hdrmeta->whitePoint[0] = + FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->white_point[0])), chroma_den); + hdrmeta->whitePoint[1] = + FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->white_point[1])), chroma_den); + } + + sd_light = av_frame_get_side_data(frame, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL); + if (sd_light) { + light_meta = (AVContentLightMetadata *)sd_light->data; + if (light_meta) { + hdrmeta->maxContentLightLevel = (amf_uint16)light_meta->MaxCLL; + hdrmeta->maxFrameAverageLightLevel = (amf_uint16)light_meta->MaxFALL; + } + } + return 0; + } + return 1; +} #if CONFIG_D3D11VA #include @@ -683,6 +734,26 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt) frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer); } + // HDR10 metadata + if (frame->color_trc == AVCOL_TRC_SMPTE2084) { + AMFBuffer * hdrmeta_buffer = NULL; + res = ctx->context->pVtbl->AllocBuffer(ctx->context, AMF_MEMORY_HOST, sizeof(AMFHDRMetadata), &hdrmeta_buffer); + if (res == AMF_OK) { + AMFHDRMetadata * hdrmeta = (AMFHDRMetadata*)hdrmeta_buffer->pVtbl->GetNative(hdrmeta_buffer); + if (amf_save_hdr_metadata(avctx, frame, hdrmeta) == 0) { + switch (avctx->codec->id) { + case AV_CODEC_ID_H264: + AMF_ASSIGN_PROPERTY_INTERFACE(res, ctx->encoder, AMF_VIDEO_ENCODER_INPUT_HDR_METADATA, hdrmeta_buffer); break; + case AV_CODEC_ID_HEVC: + AMF_ASSIGN_PROPERTY_INTERFACE(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_INPUT_HDR_METADATA, hdrmeta_buffer); break; + } + res = amf_set_property_buffer(surface, L"av_frame_hdrmeta", hdrmeta_buffer); + AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, "SetProperty failed for \"av_frame_hdrmeta\" with error %d\n", res); + } + hdrmeta_buffer->pVtbl->Release(hdrmeta_buffer); + } + } + surface->pVtbl->SetPts(surface, frame->pts); AMF_ASSIGN_PROPERTY_INT64(res, surface, PTS_PROP, frame->pts); @@ -746,6 +817,18 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt) } res_resubmit = AMF_OK; if (ctx->delayed_surface != NULL) { // try to resubmit frame + if (ctx->delayed_surface->pVtbl->HasProperty(ctx->delayed_surface, L"av_frame_hdrmeta")) { + AMFBuffer * hdrmeta_buffer = NULL; + res = amf_get_property_buffer((AMFData *)ctx->delayed_surface, L"av_frame_hdrmeta", &hdrmeta_buffer); + AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, "GetProperty failed for \"av_frame_hdrmeta\" with error %d\n", res); + switch (avctx->codec->id) { + case AV_CODEC_ID_H264: + AMF_ASSIGN_PROPERTY_INTERFACE(res, ctx->encoder, AMF_VIDEO_ENCODER_INPUT_HDR_METADATA, hdrmeta_buffer); break; + case AV_CODEC_ID_HEVC: + AMF_ASSIGN_PROPERTY_INTERFACE(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_INPUT_HDR_METADATA, hdrmeta_buffer); break; + } + hdrmeta_buffer->pVtbl->Release(hdrmeta_buffer); + } res_resubmit = ctx->encoder->pVtbl->SubmitInput(ctx->encoder, (AMFData*)ctx->delayed_surface); if (res_resubmit != AMF_INPUT_FULL) { int64_t pts = ctx->delayed_surface->pVtbl->GetPts(ctx->delayed_surface);