From patchwork Wed Oct 2 23:04:52 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 15474 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id A4C1C44A176 for ; Thu, 3 Oct 2019 02:10:29 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8BB10688154; Thu, 3 Oct 2019 02:10:29 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F40FB687F69 for ; Thu, 3 Oct 2019 02:10:21 +0300 (EEST) Received: by mail-wr1-f67.google.com with SMTP id y19so812791wrd.3 for ; Wed, 02 Oct 2019 16:10:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=ZaJQ/ctytG4QR22m1Xd0efbNiTHEXggK8VpVoDQMQls=; b=qrT9uK0LiJQ/LEAxPMxyEq+wWzfs5k1HK3t/tvKPyPm/5+gsMvmG+W6hsFNyNAL5MF EmR6zr1uBt91WQNRwnNDCSPUOjAPq3qw4iIhBAAUCdUhnGFJkf6E4e6ZQziNmNMwrejj m5u2wvQBetW5xQI85Egk+s0g9szcdwYiKK9BkaOB7vnzy49ATv9k0sEaeejA5KXsdafH eHaNQwYJqU9CBeCtNwXDk3AJOIvPnzMFue4y3kuclYkmlrzPKMl4vHS67dGTz8GBGEuz 67gbNjYVcKcbKBAPindDrmGgCtYeTxztl/ZjS2oGWIAjI5LdTRetsqU3PN/ceO6x/RmZ 0SPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZaJQ/ctytG4QR22m1Xd0efbNiTHEXggK8VpVoDQMQls=; b=mb3VZ2j/EBVaqZBQDCfrKhlyj4Seb8TXpYtlQyeSrSfZShDRrhyBjTcA3Wl4rFpoSd PFbr8QWleU1Ob+2RsTU+zbefJJAdlCmkxgIGXlhNKoK/2f7yfFGDfBl5JPz/ln9NxotZ qW8Vk0eUBYEOgWDIrathjJmCBdv3t2n5yNTh0pQgXvQWDDAnmcLwypgzUHXNhuPmT5/p vGAgrCq36AE4peGXr/msgDnAIzqK2SpfLSyllcw0GstMzv49dTeuOEKg1SLUefObUSNE F8SrhVeVsGriJXHBZytQrV0M7vknWsANFRzm4bz7RbYu/kcB8ovF17Odsh+n4EtiVymC uJTg== X-Gm-Message-State: APjAAAUhby/kvaV/dyvyhDcP1XncdGT+i/xURWh6EymrO+IlHyAVTZNC oo2eLkPuw8mEL8KkwBZCaqMxwBUeC7k= X-Google-Smtp-Source: APXvYqxHkd0VItskpoq0IH8TEAq6IHOxSujQEQ7DLJhPt7d4CJnDcox1kA/sJScWqbVEfnmitqItMA== X-Received: by 2002:adf:d1a8:: with SMTP id w8mr96676wrc.271.1570057511892; Wed, 02 Oct 2019 16:05:11 -0700 (PDT) Received: from rywe.jkqxz.net (cpc91242-cmbg18-2-0-cust650.5-4.cable.virginm.net. [82.8.130.139]) by smtp.gmail.com with ESMTPSA id o9sm1489808wrh.46.2019.10.02.16.05.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2019 16:05:11 -0700 (PDT) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Thu, 3 Oct 2019 00:04:52 +0100 Message-Id: <20191002230453.6462-17-sw@jkqxz.net> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191002230453.6462-1-sw@jkqxz.net> References: <20191002230453.6462-1-sw@jkqxz.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 17/18] cbs_h265: Add functions to turn HDR metadata into SEI X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- With intent to add some sort of support for this in H.265 metadata, though it's not in this set. Is there any thought on what a human-usable text serialisation of AVMasteringDisplayMetadata should look like? Ideally it wants to be something which would require no escaping when using it on the command-line. (This is relevant to video filters as well.) libavcodec/Makefile | 2 +- libavcodec/cbs_h265.c | 94 +++++++++++++++++++++++++++++++++++++++++++ libavcodec/cbs_h265.h | 18 +++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 libavcodec/cbs_h265.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 86e512b605..d383d4a539 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -65,7 +65,7 @@ OBJS-$(CONFIG_CABAC) += cabac.o OBJS-$(CONFIG_CBS) += cbs.o OBJS-$(CONFIG_CBS_AV1) += cbs_av1.o OBJS-$(CONFIG_CBS_H264) += cbs_h2645.o cbs_h264.o h2645_parse.o -OBJS-$(CONFIG_CBS_H265) += cbs_h2645.o h2645_parse.o +OBJS-$(CONFIG_CBS_H265) += cbs_h2645.o cbs_h265.o h2645_parse.o OBJS-$(CONFIG_CBS_JPEG) += cbs_jpeg.o OBJS-$(CONFIG_CBS_MPEG2) += cbs_mpeg2.o OBJS-$(CONFIG_CBS_VP9) += cbs_vp9.o diff --git a/libavcodec/cbs_h265.c b/libavcodec/cbs_h265.c new file mode 100644 index 0000000000..b4850bd565 --- /dev/null +++ b/libavcodec/cbs_h265.c @@ -0,0 +1,94 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/mathematics.h" +#include "libavutil/mastering_display_metadata.h" + +#include "cbs_h265.h" + + +static unsigned int rescale(AVRational value, unsigned int max) +{ + int64_t scaled = av_rescale(max, value.num, value.den); + return av_clip(scaled, 0, max); +} + +void ff_cbs_h265_fill_sei_mastering_display(H265RawSEIMasteringDisplayColourVolume *mdcv, + const AVMasteringDisplayMetadata *mdm) +{ + memset(mdcv, 0, sizeof(*mdcv)); + + if (mdm->has_primaries) { + // The values in the metadata are rationals in the range [0, 1], + // while the SEI structure contains fixed-point values with an + // increment of 0.00002. So, scale up by 50000 to convert between + // them. + + for (int a = 0; a < 3; a++) { + // The metadata stores this in RGB order, but the SEI wants it + // in GBR order. + int b = (a + 1) % 3; + mdcv->display_primaries_x[a] = + rescale(mdm->display_primaries[b][0], 50000); + mdcv->display_primaries_y[a] = + rescale(mdm->display_primaries[b][1], 50000); + } + + mdcv->white_point_x = rescale(mdm->white_point[0], 50000); + mdcv->white_point_y = rescale(mdm->white_point[1], 50000); + } + + if (mdm->has_luminance) { + // Metadata are rational values in candelas per square metre, SEI + // contains fixed point in units of 0.0001 candelas per square + // metre. So, scale up by 10000 to convert between them. + + mdcv->max_display_mastering_luminance = + rescale(mdm->max_luminance, 10000); + mdcv->min_display_mastering_luminance = + rescale(mdm->min_luminance, 10000); + + // The spec has a hard requirement that min is less than the max, + // and the SEI-writing code enforces that. + if (!(mdcv->min_display_mastering_luminance < + mdcv->max_display_mastering_luminance)) { + if (mdcv->max_display_mastering_luminance == 10000) + mdcv->min_display_mastering_luminance = + mdcv->max_display_mastering_luminance - 1; + else + mdcv->max_display_mastering_luminance = + mdcv->min_display_mastering_luminance + 1; + } + } else { + mdcv->max_display_mastering_luminance = 1; + mdcv->min_display_mastering_luminance = 0; + } +} + +void ff_cbs_h265_fill_sei_content_light_level(H265RawSEIContentLightLevelInfo *cll, + const AVContentLightMetadata *clm) +{ + memset(cll, 0, sizeof(*cll)); + + // Both the metadata and the SEI are in units of candelas per square + // metre, so we only need to clip to ensure that they are in the valid + // range. + + cll->max_content_light_level = av_clip_uintp2(clm->MaxCLL, 16); + cll->max_pic_average_light_level = av_clip_uintp2(clm->MaxCLL, 16); +} diff --git a/libavcodec/cbs_h265.h b/libavcodec/cbs_h265.h index f5eb5af5b2..793675ad41 100644 --- a/libavcodec/cbs_h265.h +++ b/libavcodec/cbs_h265.h @@ -746,4 +746,22 @@ typedef struct CodedBitstreamH265Context { } CodedBitstreamH265Context; +struct AVMasteringDisplayMetadata; +struct AVContentLightMetadata; + +/** + * Fill an SEI Mastering Display Colour Volume structure with values derived + * from the AVMasteringDisplayMetadata side-data structure. + */ +void ff_cbs_h265_fill_sei_mastering_display(H265RawSEIMasteringDisplayColourVolume *mdcv, + const struct AVMasteringDisplayMetadata *mdm); + +/** + * Fill an SEI Content Light Level Info structure with values derived from + * the AVContentLightMetadata side-data structure. + */ +void ff_cbs_h265_fill_sei_content_light_level(H265RawSEIContentLightLevelInfo *cll, + const struct AVContentLightMetadata *clm); + + #endif /* AVCODEC_CBS_H265_H */