From patchwork Sun Feb 23 23:41:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 17877 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 DA05D449F1B for ; Mon, 24 Feb 2020 01:41:51 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BA72468B4BE; Mon, 24 Feb 2020 01:41:51 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E9199687F1F for ; Mon, 24 Feb 2020 01:41:50 +0200 (EET) Received: by mail-wm1-f51.google.com with SMTP id a5so7209851wmb.0 for ; Sun, 23 Feb 2020 15:41:50 -0800 (PST) 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=TnQk8VtuBLouVH3sg27FCCbyn3m9EI8ADSlsRhD1JzU=; b=CVP8G8Ob5uH1U4fGilY399ODWmkZKXkm08RMEg0Wpaocnq/SKewVM8vUdeJmEbcFvc HKVmx6oTydbtH8QOv2qX0jIB/VlHd5uyZnTXXhGQ5f4V1ICeC1Xjm/D0WYr//mTifIzR 8pJ2nEdiMKYiCQj/DhsLmRIRjoDzqG24Wl/D5+ThLvizTXX7RYe9ANnvltGq948/ZrQT QkbrKtp+KpLwNoc7VFa9SxFJwT+shmzsII3K3VA+ffgwqDJUcGO2CkDjM4Ud+/SxIQv/ uNXwshG3vVbm5Yoh3Kl2PjTTuROtmPZa9bo8QY/94Ce/D1IMP2iCSNwWk+MOVlkUbGO+ mgBg== 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=TnQk8VtuBLouVH3sg27FCCbyn3m9EI8ADSlsRhD1JzU=; b=Yq84d97LSh+0nJ0PpFognQgu89RucRxf5DuosNexxz/72q6n89J8AsE6MNiOJaQuxV 6EdfIFFGffP8qLLns8JOieda2eZbnULMRe6SrtqGcRsbt/GwXw6FoxCJCgplIyZRubLU dIU6a3jNU9h0793DqKDyHzVeV6Ks4t4riOsls4YYtTk1kOn0mY1yV1HJK/Q/+PmmDt18 HA8SZbciC1k8ING1Ii2A4twORBf+unVtsbRc/quwtQBalUFtsWXMyobBQTP63lw/sRiy rnmolSK3/2G2qiIIO+xr77T478jAJ4+gi65/bXId8ZC0fF5ah3nR78+pgjWXnIOBfowb BzsA== X-Gm-Message-State: APjAAAUDJ7DX3ZKsuTe7bZ9xI3zrGuA+NTj86wlS4iFcGNC2J9rRzkzR ZmBZkxf/NeXyDNSu559E8updWXZbz9c= X-Google-Smtp-Source: APXvYqyKTD55AcG8915QBWVo8Vl8AweurZP9OGxwLAIGq0LU8lXU9JHUB+tyj9JyE6l/klLsJoPdZQ== X-Received: by 2002:a1c:4144:: with SMTP id o65mr17655307wma.81.1582501309899; Sun, 23 Feb 2020 15:41:49 -0800 (PST) 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 y131sm16358070wmc.13.2020.02.23.15.41.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Feb 2020 15:41:49 -0800 (PST) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Sun, 23 Feb 2020 23:41:21 +0000 Message-Id: <20200223234124.17689-18-sw@jkqxz.net> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200223234124.17689-1-sw@jkqxz.net> References: <20200223234124.17689-1-sw@jkqxz.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v4 18/21] 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" --- libavcodec/Makefile | 2 +- libavcodec/cbs_h265.c | 99 +++++++++++++++++++++++++++++++++++++++++++ libavcodec/cbs_h265.h | 18 ++++++++ 3 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 libavcodec/cbs_h265.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 0c4547f3a1..1ce079687b 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..590977cf00 --- /dev/null +++ b/libavcodec/cbs_h265.c @@ -0,0 +1,99 @@ +/* + * 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 uint32_t rescale_clip(AVRational value, uint32_t scale, uint32_t max) +{ + int64_t scaled = av_rescale(scale, value.num, value.den); + return av_clip64(scaled, 0, max); +} + +static uint32_t rescale_fraction(AVRational value, uint32_t max) +{ + return rescale_clip(value, max, 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 structure are fractions between 0 and 1, + // while the SEI message 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 structure stores this in RGB order, but the SEI + // wants it in GBR order. + int b = (a + 1) % 3; + mdcv->display_primaries_x[a] = + rescale_fraction(mdm->display_primaries[b][0], 50000); + mdcv->display_primaries_y[a] = + rescale_fraction(mdm->display_primaries[b][1], 50000); + } + + mdcv->white_point_x = rescale_fraction(mdm->white_point[0], 50000); + mdcv->white_point_y = rescale_fraction(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, and clip to + // ensure that we don't overflow. + + mdcv->max_display_mastering_luminance = + rescale_clip(mdm->max_luminance, 10000, UINT32_MAX); + mdcv->min_display_mastering_luminance = + rescale_clip(mdm->min_luminance, 10000, UINT32_MAX); + + // 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 == UINT32_MAX) + 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->MaxFALL, 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 */