Re: [FFmpeg-devel] [PATCH v6 19/22] cbs_h265: Add functions to turn HDR metadata into SEI

2020-08-11 Thread Andreas Rheinhardt
Mark Thompson:
> ---
>  libavcodec/Makefile   |   2 +-
>  libavcodec/cbs_h265.c | 100 ++
>  libavcodec/cbs_h265.h |  18 
>  3 files changed, 119 insertions(+), 1 deletion(-)
>  create mode 100644 libavcodec/cbs_h265.c
> 
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index 6394694617..96dc1df343 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -71,7 +71,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 00..afbdd37896
> --- /dev/null
> +++ b/libavcodec/cbs_h265.c
> @@ -0,0 +1,100 @@
> +/*
> + * 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.2.  So, scale up by 5 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.
> +static const uint8_t mapping[] = { 1, 2, 0 };
> +int b = mapping[a];
> +mdcv->display_primaries_x[a] =
> +rescale_fraction(mdm->display_primaries[b][0], 5);
> +mdcv->display_primaries_y[a] =
> +rescale_fraction(mdm->display_primaries[b][1], 5);

Unfortunately the Mastering display colour volume SEI message semantics
have been rewritten in the revision 06/19 of the H.265 standard. The old
semantics for the x coordinate is only retained when the value is in the
range 5..37000, for y it is 5..42000. Values outside these ranges are
unknown/unspecified/specified by other specifications. So I suggest to
clip it the values into these ranges.

> +}
> +
> +mdcv->white_point_x = rescale_fraction(mdm->white_point[0], 5);
> +mdcv->white_point_y = rescale_fraction(mdm->white_point[1], 5);

The 5..37000/42000 ranges also apply to white_point_x/y.

> +}
> +
> +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 1 to convert between them, and clip to
> +// ensure that we don't overflow.
> +
> +mdcv->max_display_mastering_luminance =
> +rescale_clip(mdm->max_luminance, 1, UINT32_MAX);
> +mdcv->min_display_mastering_luminance =
> +rescale_clip(mdm->min_luminance, 1, UINT32_MAX);
> +

The old semantics of these fields is only retained in the ranges
5..1 and 1..5. min == max == 5 is forbidden.

> +// 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 <
> 

[FFmpeg-devel] [PATCH v6 19/22] cbs_h265: Add functions to turn HDR metadata into SEI

2020-07-27 Thread Mark Thompson
---
 libavcodec/Makefile   |   2 +-
 libavcodec/cbs_h265.c | 100 ++
 libavcodec/cbs_h265.h |  18 
 3 files changed, 119 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/cbs_h265.c

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 6394694617..96dc1df343 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -71,7 +71,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 00..afbdd37896
--- /dev/null
+++ b/libavcodec/cbs_h265.c
@@ -0,0 +1,100 @@
+/*
+ * 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.2.  So, scale up by 5 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.
+static const uint8_t mapping[] = { 1, 2, 0 };
+int b = mapping[a];
+mdcv->display_primaries_x[a] =
+rescale_fraction(mdm->display_primaries[b][0], 5);
+mdcv->display_primaries_y[a] =
+rescale_fraction(mdm->display_primaries[b][1], 5);
+}
+
+mdcv->white_point_x = rescale_fraction(mdm->white_point[0], 5);
+mdcv->white_point_y = rescale_fraction(mdm->white_point[1], 5);
+}
+
+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 1 to convert between them, and clip to
+// ensure that we don't overflow.
+
+mdcv->max_display_mastering_luminance =
+rescale_clip(mdm->max_luminance, 1, UINT32_MAX);
+mdcv->min_display_mastering_luminance =
+rescale_clip(mdm->min_luminance, 1, 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,