From patchwork Thu Aug 1 11:13:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Araz X-Patchwork-Id: 50850 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:12d6:0:b0:489:2eb3:e4c4 with SMTP id 205csp1065617vqs; Thu, 1 Aug 2024 04:14:08 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVtSoApDUNmYATUDURCFoKMDUjZLn2RHOwxkl7DXYTjuZGjTDyHEZhX6yevMPcDglODG6BZ2W/7527BTMK/VUOqcvnHSa2mhCUMDA== X-Google-Smtp-Source: AGHT+IHUsiiLYeLuK4oCy7RHu+YrqJ7iqqgMXNksmPCHBSjCFvPQmVeYXR5EyRWrCKE9Ao/2HhX6 X-Received: by 2002:ac2:5e3c:0:b0:52c:cca8:a9fb with SMTP id 2adb3069b0e04-530b61e7f0cmr1346685e87.42.1722510848651; Thu, 01 Aug 2024 04:14:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1722510848; cv=none; d=google.com; s=arc-20160816; b=bvLsDG9s7ETH0px1p1lyj+hE09WlsGFalD4Ajm5+Va6AB9tNMq+ncio41TEmyN+vtI s0eQGOxmAtlTHURNMuYlzDWQH0pAQbgDNiuQV4auC1Q6JCKfSZgBX3c/JBA7aBoQcnOy SROqKqsO9QqZyYFvnTCoOc+11PSMgCaQfZggRU2+b0PlEut4CDm2cB7R36bN5S7d4xJf C1sh0q+oaJtYxeUqU8u40EnNAZ9op5yDHAIdlnDpRzwIR413KQJXbIawd8N9fnv89m6v Kagsuezyd+FiaEZo5g1NB144BToTn6zIlocJ8bZa3kVCfHzc+Z/xyH7ZTpXPRbRqnoEh jM/A== 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=+oJDESPHf+XgcGX7L5g/QRsqM685yDrpdHkfza0EyIU=; fh=s4NEqlgyeQvuJ9M2FER/qIrd96lZ1v/frktaqQpFGmg=; b=F7xC3HImOsYQQvuCxjVmj5hdGgMSqYtb/ADOJPNwatD384G6QI39/sGnIk53b+r1ji qAYq6zhO9S47XdUw8AUVNBsZ69wbxv+iDvkXfOxRDAzcfYFDhsVSX7b3/PwI9yW1URqC f5Uyut9e20aVDIQ3q3nRUsGI5jEMOFw0RYVjzC/MXOIPt4kgNilUoqkufr7gYoXr0CLV 4eI28QdaA/iFqI73YTMxoHy2nlqmzM8uw3nH3QMgcm45PGlISMCEGz7Iq/9OOIORt7NG vkbSSQOIj44SK9Tpf5xC7oGQqE8vnnnHRcrlFakzbrOLcMKnKht/tZF9k73ogZpGgZt7 S/Mg==; 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=K+AvKZtu; 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; dara=fail header.i=@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 2adb3069b0e04-52fd5c05d2esi4932781e87.525.2024.08.01.04.14.08; Thu, 01 Aug 2024 04:14:08 -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=K+AvKZtu; 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; dara=fail header.i=@gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E2E2668D957; Thu, 1 Aug 2024 14:13:53 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f44.google.com (mail-ed1-f44.google.com [209.85.208.44]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8635C68D7AA for ; Thu, 1 Aug 2024 14:13:46 +0300 (EEST) Received: by mail-ed1-f44.google.com with SMTP id 4fb4d7f45d1cf-5a10bb7bcd0so10412660a12.3 for ; Thu, 01 Aug 2024 04:13:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722510825; x=1723115625; 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=/yZfVfdvyXoEHL3L13XbM1exE0lV781Ey6XCqA4E2Cg=; b=K+AvKZtuOh40+Ul/nijztEw/9v2/1Ua38RIibM9ECkP+Gxfyqt18pTDwthfpRUlZMY GR1r5HjJDia2IthNXzRMLzR/WM8AGIAe+9fCmwVdzsaQIgJnIkayThwCS2vXIYF0eMfW Nhyfu4i6hlJP5E++aRDCk79jyZnZDH0ZfPfbgqpvz4JgurRnc4fToNkwVR3z9GPp1FnX QeQ/+9tfhyIeKC7tTxivHB4QBD3MZZWWH32Puqet4YWKNdztxDOKAtC7UbrvNC5mrv2C OaPZCDjWyI0PtzuakzqKmhr3O9wXAYucvxgx+BVaQIZmBzSRCb2On9/oyUvklhGIXPX8 ZD3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722510825; x=1723115625; 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=/yZfVfdvyXoEHL3L13XbM1exE0lV781Ey6XCqA4E2Cg=; b=fW7d5mI8WKLarDu58+wj7eIDbjtwLlTk1tiWvXQvSqF8Kge6xXirE4ncesq3zGKfgo PnlvPXK5pXZ9NHQzTJVvrZsjTq8kbX4irJY1fuk4M0ddXrzaTWri3an7HbTwNtUbHIQa QkQa+NbRMVMmsCeXLlNXEjitn2kfwerB0O4LjgXHrWTyGeGZ7Dd5fe5D0xjXjmJlWfvD 8Ra6j5zKpqazcCAoiWRYDLubuZDpq4Q6lEgVm80GiejPkIVZ798IUqIkyFtp1I+oxoQM HpFfddR2TF/W+pBFtpbBeJ4+IiKKFg0yPOzu+K06C+ZKERZIOC/Dmeb0vAQ+JFClkCXk keQg== X-Gm-Message-State: AOJu0YzyueGgMQeAJVzwpKfgOnjLHWOyl5A2/gmn+rzsuEzW50SmlOn2 hljALTcilLu7c1+rwZ6ITzU1Ta8CEU0EX3XTOhBgpbz/sRew3QLPGbN1zcx4fWI= X-Received: by 2002:a17:906:7952:b0:a77:e2e3:3557 with SMTP id a640c23a62f3a-a7daf607064mr146160966b.57.1722510825267; Thu, 01 Aug 2024 04:13:45 -0700 (PDT) Received: from aiusubov.amd.com ([87.116.132.31]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a7acada2b1asm887691766b.158.2024.08.01.04.13.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Aug 2024 04:13:44 -0700 (PDT) From: Araz Iusubov X-Google-Original-From: Araz Iusubov To: ffmpeg-devel@ffmpeg.org Date: Thu, 1 Aug 2024 13:13:36 +0200 Message-ID: <20240801111338.1683-2-Primeadvice@gmail.com> X-Mailer: git-send-email 2.45.2.windows.1 In-Reply-To: <20240801111338.1683-1-Primeadvice@gmail.com> References: <20240801111338.1683-1-Primeadvice@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/4,v2] 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: DRyVCFO+Amul 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);