* [FFmpeg-devel] [PATCH v9 1/6] lavu/frame: Add Dolby Vision metadata side data type
@ 2021-12-22 15:14 Niklas Haas
2021-12-22 15:14 ` [FFmpeg-devel] [PATCH v9 2/6] lavfi/showinfo: Support AV_FRAME_DATA_DOVI_METADATA Niklas Haas
` (4 more replies)
0 siblings, 5 replies; 13+ messages in thread
From: Niklas Haas @ 2021-12-22 15:14 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Niklas Haas
From: Niklas Haas <git@haasn.dev>
In order to be able to extend this struct later (as the Dolby Vision RPU
evolves), all of the structs are considered extensible, and the
individual constituent fields must instead be accessed via offsets. The
precedent for this style of access is set in
<libavutil/detection_bbox.h>
Signed-off-by: Niklas Haas <git@haasn.dev>
---
doc/APIchanges | 3 +
libavutil/dovi_meta.c | 25 +++++++
libavutil/dovi_meta.h | 165 ++++++++++++++++++++++++++++++++++++++++++
libavutil/frame.c | 1 +
libavutil/frame.h | 9 ++-
libavutil/version.h | 2 +-
6 files changed, 203 insertions(+), 2 deletions(-)
diff --git a/doc/APIchanges b/doc/APIchanges
index ac75040274..f692b8ad80 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,9 @@ libavutil: 2021-04-27
API changes, most recent first:
+2021-12-xx - xxxxxxxxxx - lavu 57.13.100 - frame.h
+ Add AV_FRAME_DATA_DOVI_METADATA.
+
2021-12-21 - xxxxxxxxxx - lavu 57.12.100 - cpu.h
Add AV_CPU_FLAG_SLOW_GATHER.
diff --git a/libavutil/dovi_meta.c b/libavutil/dovi_meta.c
index 7bd08f6c54..9c50da561e 100644
--- a/libavutil/dovi_meta.c
+++ b/libavutil/dovi_meta.c
@@ -33,3 +33,28 @@ AVDOVIDecoderConfigurationRecord *av_dovi_alloc(size_t *size)
return dovi;
}
+
+typedef struct AVDOVIMetadataInternal {
+ AVDOVIMetadata metadata;
+ AVDOVIRpuDataHeader header;
+ AVDOVIDataMapping mapping;
+ AVDOVIColorMetadata color;
+} AVDOVIMetadataInternal;
+
+AVDOVIMetadata *av_dovi_metadata_alloc(size_t *size)
+{
+ AVDOVIMetadataInternal *dovi = av_mallocz(sizeof(AVDOVIMetadataInternal));
+ if (!dovi)
+ return NULL;
+
+ if (size)
+ *size = sizeof(*dovi);
+
+ dovi->metadata = (struct AVDOVIMetadata) {
+ .header_offset = offsetof(AVDOVIMetadataInternal, header),
+ .mapping_offset = offsetof(AVDOVIMetadataInternal, mapping),
+ .color_offset = offsetof(AVDOVIMetadataInternal, color),
+ };
+
+ return &dovi->metadata;
+}
diff --git a/libavutil/dovi_meta.h b/libavutil/dovi_meta.h
index 299911d434..5bd67e1d49 100644
--- a/libavutil/dovi_meta.h
+++ b/libavutil/dovi_meta.h
@@ -29,6 +29,7 @@
#include <stdint.h>
#include <stddef.h>
+#include "rational.h"
/*
* DOVI configuration
@@ -67,4 +68,168 @@ typedef struct AVDOVIDecoderConfigurationRecord {
*/
AVDOVIDecoderConfigurationRecord *av_dovi_alloc(size_t *size);
+/**
+ * Dolby Vision RPU data header.
+ *
+ * @note sizeof(AVDOVIRpuDataHeader) is not part of the public ABI.
+ */
+typedef struct AVDOVIRpuDataHeader {
+ uint8_t rpu_type;
+ uint16_t rpu_format;
+ uint8_t vdr_rpu_profile;
+ uint8_t vdr_rpu_level;
+ uint8_t chroma_resampling_explicit_filter_flag;
+ uint8_t coef_data_type; /* informative, lavc always converts to fixed */
+ uint8_t coef_log2_denom;
+ uint8_t vdr_rpu_normalized_idc;
+ uint8_t bl_video_full_range_flag;
+ uint8_t bl_bit_depth; /* [8, 16] */
+ uint8_t el_bit_depth; /* [8, 16] */
+ uint8_t vdr_bit_depth; /* [8, 16] */
+ uint8_t spatial_resampling_filter_flag;
+ uint8_t el_spatial_resampling_filter_flag;
+ uint8_t disable_residual_flag;
+} AVDOVIRpuDataHeader;
+
+enum AVDOVIMappingMethod {
+ AV_DOVI_MAPPING_POLYNOMIAL = 0,
+ AV_DOVI_MAPPING_MMR = 1,
+};
+
+/**
+ * Coefficients of a piece-wise function. The pieces of the function span the
+ * value ranges between two adjacent pivot values.
+ *
+ * @note sizeof(AVDOVIReshapingCurve) is not part of the public ABI.
+ */
+#define AV_DOVI_MAX_PIECES 8
+typedef struct AVDOVIReshapingCurve {
+ uint8_t num_pivots; /* [2, 9] */
+ uint16_t pivots[AV_DOVI_MAX_PIECES + 1]; /* sorted ascending */
+ enum AVDOVIMappingMethod mapping_idc[AV_DOVI_MAX_PIECES];
+ /* AV_DOVI_MAPPING_POLYNOMIAL */
+ uint8_t poly_order[AV_DOVI_MAX_PIECES]; /* [1, 2] */
+ int64_t poly_coef[AV_DOVI_MAX_PIECES][3]; /* x^0, x^1, x^2 */
+ /* AV_DOVI_MAPPING_MMR */
+ uint8_t mmr_order[AV_DOVI_MAX_PIECES]; /* [1, 3] */
+ int64_t mmr_constant[AV_DOVI_MAX_PIECES];
+ int64_t mmr_coef[AV_DOVI_MAX_PIECES][3/* order - 1 */][7];
+} AVDOVIReshapingCurve;
+
+enum AVDOVINLQMethod {
+ AV_DOVI_NLQ_NONE = -1,
+ AV_DOVI_NLQ_LINEAR_DZ = 0,
+};
+
+/**
+ * Coefficients of the non-linear inverse quantization. For the interpretation
+ * of these, see ETSI GS CCM 001.
+ *
+ * @note sizeof(AVDOVINLQParams) is not part of the public ABI.
+ */
+typedef struct AVDOVINLQParams {
+ uint64_t nlq_offset;
+ uint64_t vdr_in_max;
+ /* AV_DOVI_NLQ_LINEAR_DZ */
+ uint64_t linear_deadzone_slope;
+ uint64_t linear_deadzone_threshold;
+} AVDOVINLQParams;
+
+/**
+ * Dolby Vision RPU data mapping parameters.
+ *
+ * @note sizeof(AVDOVIDataMapping) is not part of the public ABI.
+ */
+typedef struct AVDOVIDataMapping {
+ uint8_t vdr_rpu_id;
+ uint8_t mapping_color_space;
+ uint8_t mapping_chroma_format_idc;
+ AVDOVIReshapingCurve curves[3]; /* per component */
+
+ /* Non-linear inverse quantization */
+ enum AVDOVINLQMethod nlq_method_idc;
+ uint32_t num_x_partitions;
+ uint32_t num_y_partitions;
+ AVDOVINLQParams nlq[3]; /* per component */
+} AVDOVIDataMapping;
+
+typedef struct AVDOVIColorMetadata {
+ uint8_t dm_metadata_id;
+ uint8_t scene_refresh_flag;
+
+ /**
+ * Coefficients of the custom Dolby Vision IPT-PQ matrices. These are to be
+ * used instead of the matrices indicated by the frame's colorspace tags.
+ * The output of rgb_to_lms_matrix is to be fed into a BT.2020 LMS->RGB
+ * matrix based on a Hunt-Pointer-Estevez transform, but without any
+ * crosstalk. (See the definition of the ICtCp colorspace for more
+ * information.)
+ */
+ AVRational ycc_to_rgb_matrix[9]; /* before PQ linearization */
+ AVRational ycc_to_rgb_offset[3]; /* input offset of neutral value */
+ AVRational rgb_to_lms_matrix[9]; /* after PQ linearization */
+
+ /**
+ * Extra signal metadata (see Dolby patents for more info).
+ */
+ uint16_t signal_eotf;
+ uint16_t signal_eotf_param0;
+ uint16_t signal_eotf_param1;
+ uint32_t signal_eotf_param2;
+ uint8_t signal_bit_depth;
+ uint8_t signal_color_space;
+ uint8_t signal_chroma_format;
+ uint8_t signal_full_range_flag; /* [0, 3] */
+ uint16_t source_min_pq;
+ uint16_t source_max_pq;
+ uint16_t source_diagonal;
+} AVDOVIColorMetadata;
+
+/**
+ * Combined struct representing a combination of header, mapping and color
+ * metadata, for attaching to frames as side data.
+ *
+ * @note The struct must be allocated with av_dovi_metadata_alloc() and
+ * its size is not a part of the public ABI.
+ */
+
+typedef struct AVDOVIMetadata {
+ /**
+ * Offset in bytes from the beginning of this structure at which the
+ * respective structs start.
+ */
+ size_t header_offset; /* AVDOVIRpuDataHeader */
+ size_t mapping_offset; /* AVDOVIDataMapping */
+ size_t color_offset; /* AVDOVIColorMetadata */
+} AVDOVIMetadata;
+
+static av_always_inline AVDOVIRpuDataHeader *
+av_dovi_get_header(const AVDOVIMetadata *data)
+{
+ return (AVDOVIRpuDataHeader *)((uint8_t *) data + data->header_offset);
+}
+
+static av_always_inline AVDOVIDataMapping *
+av_dovi_get_mapping(const AVDOVIMetadata *data)
+{
+ return (AVDOVIDataMapping *)((uint8_t *) data + data->mapping_offset);
+}
+
+static av_always_inline AVDOVIColorMetadata *
+av_dovi_get_color(const AVDOVIMetadata *data)
+{
+ return (AVDOVIColorMetadata *)((uint8_t *) data + data->color_offset);
+}
+
+/**
+ * Allocate an AVDOVIMetadata structure and initialize its
+ * fields to default values.
+ *
+ * @param size If this parameter is non-NULL, the size in bytes of the
+ * allocated struct will be written here on success
+ *
+ * @return the newly allocated struct or NULL on failure
+ */
+AVDOVIMetadata *av_dovi_metadata_alloc(size_t *size);
+
#endif /* AVUTIL_DOVI_META_H */
diff --git a/libavutil/frame.c b/libavutil/frame.c
index 0912ad9131..8997c85e35 100644
--- a/libavutil/frame.c
+++ b/libavutil/frame.c
@@ -729,6 +729,7 @@ const char *av_frame_side_data_name(enum AVFrameSideDataType type)
case AV_FRAME_DATA_FILM_GRAIN_PARAMS: return "Film grain parameters";
case AV_FRAME_DATA_DETECTION_BBOXES: return "Bounding boxes for object detection and classification";
case AV_FRAME_DATA_DOVI_RPU_BUFFER: return "Dolby Vision RPU Data";
+ case AV_FRAME_DATA_DOVI_METADATA: return "Dolby Vision Metadata";
}
return NULL;
}
diff --git a/libavutil/frame.h b/libavutil/frame.h
index 3f295f6b9e..18e239f870 100644
--- a/libavutil/frame.h
+++ b/libavutil/frame.h
@@ -189,11 +189,18 @@ enum AVFrameSideDataType {
AV_FRAME_DATA_DETECTION_BBOXES,
/**
- * Dolby Vision RPU data, suitable for passing to x265
+ * Dolby Vision RPU raw data, suitable for passing to x265
* or other libraries. Array of uint8_t, with NAL emulation
* bytes intact.
*/
AV_FRAME_DATA_DOVI_RPU_BUFFER,
+
+ /**
+ * Parsed Dolby Vision metadata, suitable for passing to a software
+ * implementation. The payload is the AVDOVIMetadata struct defined in
+ * libavutil/dovi_meta.h.
+ */
+ AV_FRAME_DATA_DOVI_METADATA,
};
enum AVActiveFormatDescription {
diff --git a/libavutil/version.h b/libavutil/version.h
index 668f9206fe..3cac09cb96 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -79,7 +79,7 @@
*/
#define LIBAVUTIL_VERSION_MAJOR 57
-#define LIBAVUTIL_VERSION_MINOR 12
+#define LIBAVUTIL_VERSION_MINOR 13
#define LIBAVUTIL_VERSION_MICRO 100
#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
--
2.34.1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 13+ messages in thread
* [FFmpeg-devel] [PATCH v9 2/6] lavfi/showinfo: Support AV_FRAME_DATA_DOVI_METADATA
2021-12-22 15:14 [FFmpeg-devel] [PATCH v9 1/6] lavu/frame: Add Dolby Vision metadata side data type Niklas Haas
@ 2021-12-22 15:14 ` Niklas Haas
2021-12-22 15:14 ` [FFmpeg-devel] [PATCH v9 3/6] ffprobe: " Niklas Haas
` (3 subsequent siblings)
4 siblings, 0 replies; 13+ messages in thread
From: Niklas Haas @ 2021-12-22 15:14 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Niklas Haas
From: Niklas Haas <git@haasn.dev>
Signed-off-by: Niklas Haas <git@haasn.dev>
---
libavfilter/vf_showinfo.c | 108 ++++++++++++++++++++++++++++++++++++++
1 file changed, 108 insertions(+)
diff --git a/libavfilter/vf_showinfo.c b/libavfilter/vf_showinfo.c
index 62c7833247..909ad4f8d7 100644
--- a/libavfilter/vf_showinfo.c
+++ b/libavfilter/vf_showinfo.c
@@ -27,6 +27,7 @@
#include "libavutil/bswap.h"
#include "libavutil/adler32.h"
#include "libavutil/display.h"
+#include "libavutil/dovi_meta.h"
#include "libavutil/imgutils.h"
#include "libavutil/internal.h"
#include "libavutil/film_grain_params.h"
@@ -429,6 +430,110 @@ static void dump_sei_film_grain_params_metadata(AVFilterContext *ctx, const AVFr
}
}
+static void dump_dovi_metadata(AVFilterContext *ctx, const AVFrameSideData *sd)
+{
+ const AVDOVIMetadata *dovi = (AVDOVIMetadata *) sd->data;
+ const AVDOVIRpuDataHeader *hdr = av_dovi_get_header(dovi);
+ const AVDOVIDataMapping *mapping = av_dovi_get_mapping(dovi);
+ const AVDOVIColorMetadata *color = av_dovi_get_color(dovi);
+
+ av_log(ctx, AV_LOG_INFO, "Dolby Vision Metadata:\n");
+ av_log(ctx, AV_LOG_INFO, " rpu_type=%"PRIu8"; ", hdr->rpu_type);
+ av_log(ctx, AV_LOG_INFO, "rpu_format=%"PRIu16"; ", hdr->rpu_format);
+ av_log(ctx, AV_LOG_INFO, "vdr_rpu_profile=%"PRIu8"; ", hdr->vdr_rpu_profile);
+ av_log(ctx, AV_LOG_INFO, "vdr_rpu_level=%"PRIu8"; ", hdr->vdr_rpu_level);
+ av_log(ctx, AV_LOG_INFO, "chroma_resampling_explicit_filter_flag=%"PRIu8"; ", hdr->chroma_resampling_explicit_filter_flag);
+ av_log(ctx, AV_LOG_INFO, "coef_data_type=%"PRIu8"; ", hdr->coef_data_type);
+ av_log(ctx, AV_LOG_INFO, "coef_log2_denom=%"PRIu8"; ", hdr->coef_log2_denom);
+ av_log(ctx, AV_LOG_INFO, "vdr_rpu_normalized_idc=%"PRIu8"; ", hdr->vdr_rpu_normalized_idc);
+ av_log(ctx, AV_LOG_INFO, "bl_video_full_range_flag=%"PRIu8"; ", hdr->bl_video_full_range_flag);
+ av_log(ctx, AV_LOG_INFO, "bl_bit_depth=%"PRIu8"; ", hdr->bl_bit_depth);
+ av_log(ctx, AV_LOG_INFO, "el_bit_depth=%"PRIu8"; ", hdr->el_bit_depth);
+ av_log(ctx, AV_LOG_INFO, "vdr_bit_depth=%"PRIu8"; ", hdr->vdr_bit_depth);
+ av_log(ctx, AV_LOG_INFO, "spatial_resampling_filter_flag=%"PRIu8"; ", hdr->spatial_resampling_filter_flag);
+ av_log(ctx, AV_LOG_INFO, "el_spatial_resampling_filter_flag=%"PRIu8"; ", hdr->el_spatial_resampling_filter_flag);
+ av_log(ctx, AV_LOG_INFO, "disable_residual_flag=%"PRIu8"\n", hdr->disable_residual_flag);
+
+ av_log(ctx, AV_LOG_INFO, " data mapping: ");
+ av_log(ctx, AV_LOG_INFO, "vdr_rpu_id=%"PRIu8"; ", mapping->vdr_rpu_id);
+ av_log(ctx, AV_LOG_INFO, "mapping_color_space=%"PRIu8"; ", mapping->mapping_color_space);
+ av_log(ctx, AV_LOG_INFO, "mapping_chroma_format_idc=%"PRIu8"; ", mapping->mapping_chroma_format_idc);
+ av_log(ctx, AV_LOG_INFO, "nlq_method_idc=%d; ", (int) mapping->nlq_method_idc);
+ av_log(ctx, AV_LOG_INFO, "num_x_partitions=%"PRIu32"; ", mapping->num_x_partitions);
+ av_log(ctx, AV_LOG_INFO, "num_y_partitions=%"PRIu32"\n", mapping->num_y_partitions);
+
+ for (int c = 0; c < 3; c++) {
+ const AVDOVIReshapingCurve *curve = &mapping->curves[c];
+ const AVDOVINLQParams *nlq = &mapping->nlq[c];
+ av_log(ctx, AV_LOG_INFO, " channel %d: ", c);
+ av_log(ctx, AV_LOG_INFO, "pivots={ ");
+ for (int i = 0; i < curve->num_pivots; i++)
+ av_log(ctx, AV_LOG_INFO, "%"PRIu16" ", curve->pivots[i]);
+ av_log(ctx, AV_LOG_INFO, "}; mapping_idc={ ");
+ for (int i = 0; i < curve->num_pivots - 1; i++)
+ av_log(ctx, AV_LOG_INFO, "%d ", (int) curve->mapping_idc[i]);
+ av_log(ctx, AV_LOG_INFO, "}; poly_order={ ");
+ for (int i = 0; i < curve->num_pivots - 1; i++)
+ av_log(ctx, AV_LOG_INFO, "%"PRIu8" ", curve->poly_order[i]);
+ av_log(ctx, AV_LOG_INFO, "}; poly_coef={ ");
+ for (int i = 0; i < curve->num_pivots - 1; i++) {
+ av_log(ctx, AV_LOG_INFO, "{%"PRIi64", %"PRIi64", %"PRIi64"} ",
+ curve->poly_coef[i][0],
+ curve->poly_coef[i][1],
+ curve->poly_coef[i][2]);
+ }
+
+ av_log(ctx, AV_LOG_INFO, "}; mmr_order={ ");
+ for (int i = 0; i < curve->num_pivots - 1; i++)
+ av_log(ctx, AV_LOG_INFO, "%"PRIu8" ", curve->mmr_order[i]);
+ av_log(ctx, AV_LOG_INFO, "}; mmr_constant={ ");
+ for (int i = 0; i < curve->num_pivots - 1; i++)
+ av_log(ctx, AV_LOG_INFO, "%"PRIi64" ", curve->mmr_constant[i]);
+ av_log(ctx, AV_LOG_INFO, "}; mmr_coef={ ");
+ for (int i = 0; i < curve->num_pivots - 1; i++) {
+ av_log(ctx, AV_LOG_INFO, "{");
+ for (int j = 0; j < curve->mmr_order[i]; j++) {
+ for (int k = 0; k < 7; k++)
+ av_log(ctx, AV_LOG_INFO, "%"PRIi64" ", curve->mmr_coef[i][j][k]);
+ }
+ av_log(ctx, AV_LOG_INFO, "} ");
+ }
+
+ av_log(ctx, AV_LOG_INFO, "}; nlq_offset=%"PRIu64"; ", nlq->nlq_offset);
+ av_log(ctx, AV_LOG_INFO, "vdr_in_max=%"PRIu64"; ", nlq->vdr_in_max);
+ switch (mapping->nlq_method_idc) {
+ case AV_DOVI_NLQ_LINEAR_DZ:
+ av_log(ctx, AV_LOG_INFO, "linear_deadzone_slope=%"PRIu64"; ", nlq->linear_deadzone_slope);
+ av_log(ctx, AV_LOG_INFO, "linear_deadzone_threshold=%"PRIu64"\n", nlq->linear_deadzone_threshold);
+ break;
+ }
+ }
+
+ av_log(ctx, AV_LOG_INFO, " color metadata: ");
+ av_log(ctx, AV_LOG_INFO, "dm_metadata_id=%"PRIu8"; ", color->dm_metadata_id);
+ av_log(ctx, AV_LOG_INFO, "scene_refresh_flag=%"PRIu8"; ", color->scene_refresh_flag);
+ av_log(ctx, AV_LOG_INFO, "ycc_to_rgb_matrix={ ");
+ for (int i = 0; i < 9; i++)
+ av_log(ctx, AV_LOG_INFO, "%f ", av_q2d(color->ycc_to_rgb_matrix[i]));
+ av_log(ctx, AV_LOG_INFO, "}; ycc_to_rgb_offset={ ");
+ for (int i = 0; i < 3; i++)
+ av_log(ctx, AV_LOG_INFO, "%f ", av_q2d(color->ycc_to_rgb_offset[i]));
+ av_log(ctx, AV_LOG_INFO, "}; rgb_to_lms_matrix={ ");
+ for (int i = 0; i < 9; i++)
+ av_log(ctx, AV_LOG_INFO, "%f ", av_q2d(color->rgb_to_lms_matrix[i]));
+ av_log(ctx, AV_LOG_INFO, "}; signal_eotf=%"PRIu16"; ", color->signal_eotf);
+ av_log(ctx, AV_LOG_INFO, "signal_eotf_param0=%"PRIu16"; ", color->signal_eotf_param0);
+ av_log(ctx, AV_LOG_INFO, "signal_eotf_param1=%"PRIu16"; ", color->signal_eotf_param1);
+ av_log(ctx, AV_LOG_INFO, "signal_eotf_param2=%"PRIu32"; ", color->signal_eotf_param2);
+ av_log(ctx, AV_LOG_INFO, "signal_bit_depth=%"PRIu8"; ", color->signal_bit_depth);
+ av_log(ctx, AV_LOG_INFO, "signal_color_space=%"PRIu8"; ", color->signal_color_space);
+ av_log(ctx, AV_LOG_INFO, "signal_chroma_format=%"PRIu8"; ", color->signal_chroma_format);
+ av_log(ctx, AV_LOG_INFO, "signal_full_range_flag=%"PRIu8"; ", color->signal_full_range_flag);
+ av_log(ctx, AV_LOG_INFO, "source_min_pq=%"PRIu16"; ", color->source_min_pq);
+ av_log(ctx, AV_LOG_INFO, "source_max_pq=%"PRIu16"; ", color->source_max_pq);
+ av_log(ctx, AV_LOG_INFO, "source_diagonal=%"PRIu16"; ", color->source_diagonal);
+}
+
static void dump_color_property(AVFilterContext *ctx, AVFrame *frame)
{
const char *color_range_str = av_color_range_name(frame->color_range);
@@ -617,6 +722,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
case AV_FRAME_DATA_FILM_GRAIN_PARAMS:
dump_sei_film_grain_params_metadata(ctx, sd);
break;
+ case AV_FRAME_DATA_DOVI_METADATA:
+ dump_dovi_metadata(ctx, sd);
+ break;
default:
av_log(ctx, AV_LOG_WARNING, "unknown side data type %d "
"(%"SIZE_SPECIFIER" bytes)\n", sd->type, sd->size);
--
2.34.1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 13+ messages in thread
* [FFmpeg-devel] [PATCH v9 3/6] ffprobe: Support AV_FRAME_DATA_DOVI_METADATA
2021-12-22 15:14 [FFmpeg-devel] [PATCH v9 1/6] lavu/frame: Add Dolby Vision metadata side data type Niklas Haas
2021-12-22 15:14 ` [FFmpeg-devel] [PATCH v9 2/6] lavfi/showinfo: Support AV_FRAME_DATA_DOVI_METADATA Niklas Haas
@ 2021-12-22 15:14 ` Niklas Haas
2021-12-22 15:14 ` [FFmpeg-devel] [PATCH v9 4/6] lavc: Implement Dolby Vision RPU parsing Niklas Haas
` (2 subsequent siblings)
4 siblings, 0 replies; 13+ messages in thread
From: Niklas Haas @ 2021-12-22 15:14 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Niklas Haas, Jan Ekström
From: Jan Ekström <jeebjp@gmail.com>
Co-authored-by: Niklas Haas <git@haasn.dev>
---
fftools/ffprobe.c | 173 +++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 170 insertions(+), 3 deletions(-)
diff --git a/fftools/ffprobe.c b/fftools/ffprobe.c
index 906bb03b16..20582ca7ac 100644
--- a/fftools/ffprobe.c
+++ b/fftools/ffprobe.c
@@ -175,6 +175,10 @@ typedef enum {
SECTION_ID_FRAME_SIDE_DATA,
SECTION_ID_FRAME_SIDE_DATA_TIMECODE_LIST,
SECTION_ID_FRAME_SIDE_DATA_TIMECODE,
+ SECTION_ID_FRAME_SIDE_DATA_COMPONENT_LIST,
+ SECTION_ID_FRAME_SIDE_DATA_COMPONENT,
+ SECTION_ID_FRAME_SIDE_DATA_PIECE_LIST,
+ SECTION_ID_FRAME_SIDE_DATA_PIECE,
SECTION_ID_FRAME_LOG,
SECTION_ID_FRAME_LOGS,
SECTION_ID_LIBRARY_VERSION,
@@ -219,9 +223,13 @@ static struct section sections[] = {
[SECTION_ID_FRAME] = { SECTION_ID_FRAME, "frame", 0, { SECTION_ID_FRAME_TAGS, SECTION_ID_FRAME_SIDE_DATA_LIST, SECTION_ID_FRAME_LOGS, -1 } },
[SECTION_ID_FRAME_TAGS] = { SECTION_ID_FRAME_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "frame_tags" },
[SECTION_ID_FRAME_SIDE_DATA_LIST] ={ SECTION_ID_FRAME_SIDE_DATA_LIST, "side_data_list", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME_SIDE_DATA, -1 }, .element_name = "side_data", .unique_name = "frame_side_data_list" },
- [SECTION_ID_FRAME_SIDE_DATA] = { SECTION_ID_FRAME_SIDE_DATA, "side_data", 0, { SECTION_ID_FRAME_SIDE_DATA_TIMECODE_LIST, -1 } },
- [SECTION_ID_FRAME_SIDE_DATA_TIMECODE_LIST] = { SECTION_ID_FRAME_SIDE_DATA_TIMECODE_LIST, "timecodes", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME_SIDE_DATA_TIMECODE, -1 } },
- [SECTION_ID_FRAME_SIDE_DATA_TIMECODE] = { SECTION_ID_FRAME_SIDE_DATA_TIMECODE, "timecode", 0, { -1 } },
+ [SECTION_ID_FRAME_SIDE_DATA] = { SECTION_ID_FRAME_SIDE_DATA, "side_data", 0, { SECTION_ID_FRAME_SIDE_DATA_TIMECODE_LIST, SECTION_ID_FRAME_SIDE_DATA_COMPONENT_LIST, -1 } },
+ [SECTION_ID_FRAME_SIDE_DATA_TIMECODE_LIST] = { SECTION_ID_FRAME_SIDE_DATA_TIMECODE_LIST, "timecodes", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME_SIDE_DATA_TIMECODE, -1 } },
+ [SECTION_ID_FRAME_SIDE_DATA_TIMECODE] = { SECTION_ID_FRAME_SIDE_DATA_TIMECODE, "timecode", 0, { -1 } },
+ [SECTION_ID_FRAME_SIDE_DATA_COMPONENT_LIST] = { SECTION_ID_FRAME_SIDE_DATA_COMPONENT_LIST, "components", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME_SIDE_DATA_COMPONENT, -1 } },
+ [SECTION_ID_FRAME_SIDE_DATA_COMPONENT] = { SECTION_ID_FRAME_SIDE_DATA_COMPONENT, "component", 0, { SECTION_ID_FRAME_SIDE_DATA_PIECE_LIST, -1 } },
+ [SECTION_ID_FRAME_SIDE_DATA_PIECE_LIST] = { SECTION_ID_FRAME_SIDE_DATA_PIECE_LIST, "pieces", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME_SIDE_DATA_PIECE, -1 } },
+ [SECTION_ID_FRAME_SIDE_DATA_PIECE] = { SECTION_ID_FRAME_SIDE_DATA_PIECE, "section", 0, { -1 } },
[SECTION_ID_FRAME_LOGS] = { SECTION_ID_FRAME_LOGS, "logs", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME_LOG, -1 } },
[SECTION_ID_FRAME_LOG] = { SECTION_ID_FRAME_LOG, "log", 0, { -1 }, },
[SECTION_ID_LIBRARY_VERSIONS] = { SECTION_ID_LIBRARY_VERSIONS, "library_versions", SECTION_FLAG_IS_ARRAY, { SECTION_ID_LIBRARY_VERSION, -1 } },
@@ -1809,6 +1817,16 @@ static void writer_register_all(void)
writer_print_string(w, k, pbuf.str, 0); \
} while (0)
+#define print_list_fmt(k, f, n, ...) do { \
+ av_bprint_clear(&pbuf); \
+ for (int idx = 0; idx < n; idx++) { \
+ if (idx > 0) \
+ av_bprint_chars(&pbuf, ' ', 1); \
+ av_bprintf(&pbuf, f, __VA_ARGS__); \
+ } \
+ writer_print_string(w, k, pbuf.str, 0); \
+} while (0)
+
#define print_int(k, v) writer_print_integer(w, k, v)
#define print_q(k, v, s) writer_print_rational(w, k, v, s)
#define print_str(k, v) writer_print_string(w, k, v, 0)
@@ -1854,6 +1872,153 @@ static inline int show_tags(WriterContext *w, AVDictionary *tags, int section_id
return ret;
}
+static void print_dovi_metadata(WriterContext *w, const AVDOVIMetadata *dovi)
+{
+ if (!dovi)
+ return;
+
+ {
+ const AVDOVIRpuDataHeader *hdr = av_dovi_get_header(dovi);
+ const AVDOVIDataMapping *mapping = av_dovi_get_mapping(dovi);
+ const AVDOVIColorMetadata *color = av_dovi_get_color(dovi);
+ AVBPrint pbuf;
+
+ av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
+
+ // header
+ print_int("rpu_type", hdr->rpu_type);
+ print_int("rpu_format", hdr->rpu_format);
+ print_int("vdr_rpu_profile", hdr->vdr_rpu_profile);
+ print_int("vdr_rpu_level", hdr->vdr_rpu_level);
+ print_int("chroma_resampling_explicit_filter_flag",
+ hdr->chroma_resampling_explicit_filter_flag);
+ print_int("coef_data_type", hdr->coef_data_type);
+ print_int("coef_log2_denom", hdr->coef_log2_denom);
+ print_int("vdr_rpu_normalized_idc", hdr->vdr_rpu_normalized_idc);
+ print_int("bl_video_full_range_flag", hdr->bl_video_full_range_flag);
+ print_int("bl_bit_depth", hdr->bl_bit_depth);
+ print_int("el_bit_depth", hdr->el_bit_depth);
+ print_int("vdr_bit_depth", hdr->vdr_bit_depth);
+ print_int("spatial_resampling_filter_flag",
+ hdr->spatial_resampling_filter_flag);
+ print_int("el_spatial_resampling_filter_flag",
+ hdr->el_spatial_resampling_filter_flag);
+ print_int("disable_residual_flag", hdr->disable_residual_flag);
+
+ // data mapping values
+ print_int("vdr_rpu_id", mapping->vdr_rpu_id);
+ print_int("mapping_color_space", mapping->mapping_color_space);
+ print_int("mapping_chroma_format_idc",
+ mapping->mapping_chroma_format_idc);
+
+ print_int("nlq_method_idc", mapping->nlq_method_idc);
+ switch (mapping->nlq_method_idc) {
+ case AV_DOVI_NLQ_NONE:
+ print_str("nlq_method_idc_name", "none");
+ break;
+ case AV_DOVI_NLQ_LINEAR_DZ:
+ print_str("nlq_method_idc_name", "linear_dz");
+ break;
+ default:
+ print_str("nlq_method_idc_name", "unknown");
+ break;
+ }
+
+ print_int("num_x_partitions", mapping->num_x_partitions);
+ print_int("num_y_partitions", mapping->num_y_partitions);
+
+ writer_print_section_header(w, SECTION_ID_FRAME_SIDE_DATA_COMPONENT_LIST);
+
+ for (int c = 0; c < 3; c++) {
+ const AVDOVIReshapingCurve *curve = &mapping->curves[c];
+ writer_print_section_header(w, SECTION_ID_FRAME_SIDE_DATA_COMPONENT);
+
+ print_list_fmt("pivots", "%"PRIu16, curve->num_pivots, curve->pivots[idx]);
+
+ writer_print_section_header(w, SECTION_ID_FRAME_SIDE_DATA_PIECE_LIST);
+ for (int i = 0; i < curve->num_pivots - 1; i++) {
+
+ writer_print_section_header(w, SECTION_ID_FRAME_SIDE_DATA_PIECE);
+ print_int("mapping_idc", curve->mapping_idc[i]);
+ switch (curve->mapping_idc[i]) {
+ case AV_DOVI_MAPPING_POLYNOMIAL:
+ print_str("mapping_idc_name", "polynomial");
+ print_int("poly_order", curve->poly_order[i]);
+ print_list_fmt("poly_coef", "%"PRIi64,
+ curve->poly_order[i] + 1,
+ curve->poly_coef[i][idx]);
+ break;
+ case AV_DOVI_MAPPING_MMR:
+ print_str("mapping_idc_name", "mmr");
+ print_int("mmr_order", curve->mmr_order[i]);
+ print_int("mmr_constant", curve->mmr_constant[i]);
+ print_list_fmt("mmr_coef", "%"PRIi64,
+ curve->mmr_order[i] * 7,
+ curve->mmr_coef[i][0][idx]);
+ break;
+ default:
+ print_str("mapping_idc_name", "unknown");
+ break;
+ }
+
+ // SECTION_ID_FRAME_SIDE_DATA_PIECE
+ writer_print_section_footer(w);
+ }
+
+ // SECTION_ID_FRAME_SIDE_DATA_PIECE_LIST
+ writer_print_section_footer(w);
+
+ if (mapping->nlq_method_idc != AV_DOVI_NLQ_NONE) {
+ const AVDOVINLQParams *nlq = &mapping->nlq[c];
+ print_int("nlq_offset", nlq->nlq_offset);
+ print_int("vdr_in_max", nlq->vdr_in_max);
+
+ switch (mapping->nlq_method_idc) {
+ case AV_DOVI_NLQ_LINEAR_DZ:
+ print_int("linear_deadzone_slope", nlq->linear_deadzone_slope);
+ print_int("linear_deadzone_threshold", nlq->linear_deadzone_threshold);
+ break;
+ }
+ }
+
+ // SECTION_ID_FRAME_SIDE_DATA_COMPONENT
+ writer_print_section_footer(w);
+ }
+
+ // SECTION_ID_FRAME_SIDE_DATA_COMPONENT_LIST
+ writer_print_section_footer(w);
+
+ // color metadata
+ print_int("dm_metadata_id", color->dm_metadata_id);
+ print_int("scene_refresh_flag", color->scene_refresh_flag);
+ print_list_fmt("ycc_to_rgb_matrix", "%d/%d",
+ FF_ARRAY_ELEMS(color->ycc_to_rgb_matrix),
+ color->ycc_to_rgb_matrix[idx].num,
+ color->ycc_to_rgb_matrix[idx].den);
+ print_list_fmt("ycc_to_rgb_offset", "%d/%d",
+ FF_ARRAY_ELEMS(color->ycc_to_rgb_offset),
+ color->ycc_to_rgb_offset[idx].num,
+ color->ycc_to_rgb_offset[idx].den);
+ print_list_fmt("rgb_to_lms_matrix", "%d/%d",
+ FF_ARRAY_ELEMS(color->rgb_to_lms_matrix),
+ color->rgb_to_lms_matrix[idx].num,
+ color->rgb_to_lms_matrix[idx].den);
+ print_int("signal_eotf", color->signal_eotf);
+ print_int("signal_eotf_param0", color->signal_eotf_param0);
+ print_int("signal_eotf_param1", color->signal_eotf_param1);
+ print_int("signal_eotf_param2", color->signal_eotf_param2);
+ print_int("signal_bit_depth", color->signal_bit_depth);
+ print_int("signal_color_space", color->signal_color_space);
+ print_int("signal_chroma_format", color->signal_chroma_format);
+ print_int("signal_full_range_flag", color->signal_full_range_flag);
+ print_int("source_min_pq", color->source_min_pq);
+ print_int("source_max_pq", color->source_max_pq);
+ print_int("source_diagonal", color->source_diagonal);
+
+ av_bprint_finalize(&pbuf, NULL);
+ }
+}
+
static void print_dynamic_hdr10_plus(WriterContext *w, const AVDynamicHDRPlus *metadata)
{
if (!metadata)
@@ -2370,6 +2535,8 @@ static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
if (tag)
print_str(tag->key, tag->value);
print_int("size", sd->size);
+ } else if (sd->type == AV_FRAME_DATA_DOVI_METADATA) {
+ print_dovi_metadata(w, (const AVDOVIMetadata *)sd->data);
}
writer_print_section_footer(w);
}
--
2.34.1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 13+ messages in thread
* [FFmpeg-devel] [PATCH v9 4/6] lavc: Implement Dolby Vision RPU parsing
2021-12-22 15:14 [FFmpeg-devel] [PATCH v9 1/6] lavu/frame: Add Dolby Vision metadata side data type Niklas Haas
2021-12-22 15:14 ` [FFmpeg-devel] [PATCH v9 2/6] lavfi/showinfo: Support AV_FRAME_DATA_DOVI_METADATA Niklas Haas
2021-12-22 15:14 ` [FFmpeg-devel] [PATCH v9 3/6] ffprobe: " Niklas Haas
@ 2021-12-22 15:14 ` Niklas Haas
2022-01-02 4:18 ` Andreas Rheinhardt
2021-12-22 15:14 ` [FFmpeg-devel] [PATCH v9 5/6] fate: Limit Dolby Vision RPU test frame count Niklas Haas
2021-12-22 15:14 ` [FFmpeg-devel] [PATCH v9 6/6] lavc/hevcdec: Parse DOVI RPU NALs Niklas Haas
4 siblings, 1 reply; 13+ messages in thread
From: Niklas Haas @ 2021-12-22 15:14 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Niklas Haas
From: Niklas Haas <git@haasn.dev>
Based on a mixture of guesswork, partial documentation in patents, and
reverse engineering of real-world samples. Confirmed working for all the
samples I've thrown at it.
Contains some annoying machinery to persist these values in between
frames, which is needed in theory even though I've never actually seen a
sample that relies on it in practice. May or may not work.
Since the distinction matters greatly for parsing the color matrix
values, this includes a small helper function to guess the right profile
from the RPU itself in case the user has forgotten to forward the dovi
configuration record to the decoder. (Which in practice, only ffmpeg.c
and ffplay do..)
Notable omissions / deviations:
- CRC32 verification. This is based on the MPEG2 CRC32 type, which does
not seem to be implemented in lavu. (And I don't care enough to do so)
- Linear interpolation support. Nothing documents this (beyond its
existence) and no samples use it, so impossible to implement.
- All of the extension metadata blocks, but these contain values that
seem largely congruent with ST2094, HDR10, or other existing forms of
side data, so I will defer parsing/attaching them to a future commit.
- The patent describes a mechanism for predicting coefficients from
previous RPUs, but the bit for the flag whether to use the
prediction deltas or signal entirely new coefficients does not seem to
be present in actual RPUs, so we ignore this subsystem entirely.
- In the patent's spec, the NLQ subsystem also loops over
num_nlq_pivots, but even in the patent the number is hard-coded to one
iteration rather than signalled. So we only store one set of coefs.
Heavily influenced by https://github.com/quietvoid/dovi_tool
Documentation drawn from US Patent 10,701,399 B2 and ETSI GS CCM 001
Signed-off-by: Niklas Haas <git@haasn.dev>
---
configure | 2 +
libavcodec/Makefile | 1 +
libavcodec/dovi_rpu.c | 430 ++++++++++++++++++++++++++++++++++++++++++
libavcodec/dovi_rpu.h | 71 +++++++
4 files changed, 504 insertions(+)
create mode 100644 libavcodec/dovi_rpu.c
create mode 100644 libavcodec/dovi_rpu.h
diff --git a/configure b/configure
index 0ccd3bda11..68658a847f 100755
--- a/configure
+++ b/configure
@@ -2434,6 +2434,7 @@ CONFIG_EXTRA="
cbs_vp9
dirac_parse
dnn
+ dovi_rpu
dvprofile
exif
faandct
@@ -2706,6 +2707,7 @@ cbs_mpeg2_select="cbs"
cbs_vp9_select="cbs"
dct_select="rdft"
dirac_parse_select="golomb"
+dovi_rpu_select="golomb"
dnn_suggest="libtensorflow libopenvino"
dnn_deps="avformat swscale"
error_resilience_select="me_cmp"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index fb90ecea84..7364c7a91f 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -77,6 +77,7 @@ OBJS-$(CONFIG_CBS_MPEG2) += cbs_mpeg2.o
OBJS-$(CONFIG_CBS_VP9) += cbs_vp9.o
OBJS-$(CONFIG_CRYSTALHD) += crystalhd.o
OBJS-$(CONFIG_DCT) += dct.o dct32_fixed.o dct32_float.o
+OBJS-$(CONFIG_DOVI_RPU) += dovi_rpu.o
OBJS-$(CONFIG_ERROR_RESILIENCE) += error_resilience.o
OBJS-$(CONFIG_EXIF) += exif.o tiff_common.o
OBJS-$(CONFIG_FAANDCT) += faandct.o
diff --git a/libavcodec/dovi_rpu.c b/libavcodec/dovi_rpu.c
new file mode 100644
index 0000000000..fc2e1fb2a1
--- /dev/null
+++ b/libavcodec/dovi_rpu.c
@@ -0,0 +1,430 @@
+/*
+ * Dolby Vision RPU decoder
+ *
+ * Copyright (C) 2021 Jan Ekström
+ * Copyright (C) 2021 Niklas Haas
+ *
+ * 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/buffer.h"
+
+#include "dovi_rpu.h"
+#include "golomb.h"
+#include "get_bits.h"
+
+enum {
+ RPU_COEFF_FIXED = 0,
+ RPU_COEFF_FLOAT = 1,
+};
+
+/**
+ * Private contents of vdr_ref.
+ */
+typedef struct DOVIVdrRef {
+ AVDOVIDataMapping mapping;
+ AVDOVIColorMetadata color;
+} DOVIVdrRef;
+
+void ff_dovi_ctx_unref(DOVIContext *s)
+{
+ for (int i = 0; i < FF_ARRAY_ELEMS(s->vdr_ref); i++)
+ av_buffer_unref(&s->vdr_ref[i]);
+
+ /* Preserve the user-provided fields explicitly, reset everything else */
+ *s = (DOVIContext) {
+ .logctx = s->logctx,
+ .config = s->config,
+ };
+}
+
+int ff_dovi_ctx_replace(DOVIContext *s, const DOVIContext *s0)
+{
+ int ret;
+ s->logctx = s0->logctx;
+ s->config = s0->config;
+ s->mapping = s0->mapping;
+ s->color = s0->color;
+ for (int i = 0; i < DOVI_MAX_DM_ID; i++) {
+ if ((ret = av_buffer_replace(&s->vdr_ref[i], s0->vdr_ref[i])) < 0)
+ goto fail;
+ }
+
+ return 0;
+
+fail:
+ ff_dovi_ctx_unref(s);
+ return ret;
+}
+
+int ff_dovi_attach_side_data(DOVIContext *s, AVFrame *frame)
+{
+ AVFrameSideData *sd;
+ AVBufferRef *buf;
+ AVDOVIMetadata *dovi;
+ size_t dovi_size;
+
+ if (!s->mapping || !s->color)
+ return 0; /* incomplete dovi metadata */
+
+ dovi = av_dovi_metadata_alloc(&dovi_size);
+ if (!dovi)
+ return AVERROR(ENOMEM);
+
+ buf = av_buffer_create((uint8_t *) dovi, dovi_size, NULL, NULL, 0);
+ if (!buf) {
+ av_free(dovi);
+ return AVERROR(ENOMEM);
+ }
+
+ sd = av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_DOVI_METADATA, buf);
+ if (!sd) {
+ av_buffer_unref(&buf);
+ return AVERROR(ENOMEM);
+ }
+
+ memcpy(av_dovi_get_header(dovi), &s->header, sizeof(s->header));
+ memcpy(av_dovi_get_mapping(dovi), s->mapping, sizeof(*s->mapping));
+ memcpy(av_dovi_get_color(dovi), s->color, sizeof(*s->color));
+ return 0;
+}
+
+static int guess_profile(const AVDOVIRpuDataHeader *hdr)
+{
+ switch (hdr->vdr_rpu_profile) {
+ case 0:
+ if (hdr->bl_video_full_range_flag)
+ return 5;
+ break;
+ case 1:
+ if (hdr->el_spatial_resampling_filter_flag && !hdr->disable_residual_flag) {
+ if (hdr->vdr_bit_depth == 12) {
+ return 7;
+ } else {
+ return 4;
+ }
+ } else {
+ return 8;
+ }
+ }
+
+ return 0; /* unknown */
+}
+
+static inline uint64_t get_ue_coef(GetBitContext *gb, const AVDOVIRpuDataHeader *hdr)
+{
+ uint64_t ipart;
+ union { uint32_t u32; float f32; } fpart;
+
+ switch (hdr->coef_data_type) {
+ case RPU_COEFF_FIXED:
+ ipart = get_ue_golomb_long(gb);
+ fpart.u32 = get_bits_long(gb, hdr->coef_log2_denom);
+ return (ipart << hdr->coef_log2_denom) + fpart.u32;
+
+ case RPU_COEFF_FLOAT:
+ fpart.u32 = get_bits_long(gb, 32);
+ return fpart.f32 * (1 << hdr->coef_log2_denom);
+ }
+
+ return 0; /* unreachable */
+}
+
+static inline int64_t get_se_coef(GetBitContext *gb, const AVDOVIRpuDataHeader *hdr)
+{
+ int64_t ipart;
+ union { uint32_t u32; float f32; } fpart;
+
+ switch (hdr->coef_data_type) {
+ case RPU_COEFF_FIXED:
+ ipart = get_se_golomb_long(gb);
+ fpart.u32 = get_bits_long(gb, hdr->coef_log2_denom);
+ return (ipart << hdr->coef_log2_denom) + fpart.u32;
+
+ case RPU_COEFF_FLOAT:
+ fpart.u32 = get_bits_long(gb, 32);
+ return fpart.f32 * (1 << hdr->coef_log2_denom);
+ }
+
+ return 0; /* unreachable */
+}
+
+#define VALIDATE(VAR, MIN, MAX) \
+ do { \
+ if (VAR < MIN || VAR > MAX) { \
+ av_log(s->logctx, AV_LOG_ERROR, "RPU validation failed: " \
+ #MIN" <= "#VAR" = %d <= "#MAX"\n", (int) VAR); \
+ goto fail; \
+ } \
+ } while (0)
+
+int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size)
+{
+ AVDOVIRpuDataHeader *hdr = &s->header;
+ GetBitContext *gb = &(GetBitContext){0};
+ DOVIVdrRef *vdr;
+ int ret;
+
+ uint8_t nal_prefix;
+ uint8_t rpu_type;
+ uint8_t vdr_seq_info_present;
+ uint8_t vdr_dm_metadata_present;
+ uint8_t use_prev_vdr_rpu;
+ uint8_t use_nlq;
+ uint8_t profile;
+ if ((ret = init_get_bits8(gb, rpu, rpu_size)) < 0)
+ return ret;
+
+ /* RPU header, common values */
+ nal_prefix = get_bits(gb, 8);
+ VALIDATE(nal_prefix, 25, 25);
+ rpu_type = get_bits(gb, 6);
+ if (rpu_type != 2) {
+ av_log(s->logctx, AV_LOG_WARNING, "Unrecognized RPU type "
+ "%"PRIu8", ignoring\n", rpu_type);
+ return 0;
+ }
+
+ hdr->rpu_type = rpu_type;
+ hdr->rpu_format = get_bits(gb, 11);
+
+ /* Values specific to RPU type 2 */
+ hdr->vdr_rpu_profile = get_bits(gb, 4);
+ hdr->vdr_rpu_level = get_bits(gb, 4);
+
+ vdr_seq_info_present = get_bits1(gb);
+ if (vdr_seq_info_present) {
+ hdr->chroma_resampling_explicit_filter_flag = get_bits1(gb);
+ hdr->coef_data_type = get_bits(gb, 2);
+ VALIDATE(hdr->coef_data_type, RPU_COEFF_FIXED, RPU_COEFF_FLOAT);
+ switch (hdr->coef_data_type) {
+ case RPU_COEFF_FIXED:
+ hdr->coef_log2_denom = get_ue_golomb(gb);
+ VALIDATE(hdr->coef_log2_denom, 13, 32);
+ break;
+ case RPU_COEFF_FLOAT:
+ hdr->coef_log2_denom = 32; /* arbitrary, choose maximum precision */
+ break;
+ }
+
+ hdr->vdr_rpu_normalized_idc = get_bits(gb, 2);
+ hdr->bl_video_full_range_flag = get_bits1(gb);
+
+ if ((hdr->rpu_format & 0x700) == 0) {
+ int bl_bit_depth_minus8 = get_ue_golomb_31(gb);
+ int el_bit_depth_minus8 = get_ue_golomb_31(gb);
+ int vdr_bit_depth_minus8 = get_ue_golomb_31(gb);
+ VALIDATE(bl_bit_depth_minus8, 0, 8);
+ VALIDATE(el_bit_depth_minus8, 0, 8);
+ VALIDATE(vdr_bit_depth_minus8, 0, 8);
+ hdr->bl_bit_depth = bl_bit_depth_minus8 + 8;
+ hdr->el_bit_depth = el_bit_depth_minus8 + 8;
+ hdr->vdr_bit_depth = vdr_bit_depth_minus8 + 8;
+ hdr->spatial_resampling_filter_flag = get_bits1(gb);
+ skip_bits(gb, 3); /* reserved_zero_3bits */
+ hdr->el_spatial_resampling_filter_flag = get_bits1(gb);
+ hdr->disable_residual_flag = get_bits1(gb);
+ }
+ }
+
+ if (!hdr->bl_bit_depth) {
+ av_log(s->logctx, AV_LOG_ERROR, "Missing RPU VDR sequence info?\n");
+ goto fail;
+ }
+
+ vdr_dm_metadata_present = get_bits1(gb);
+ use_prev_vdr_rpu = get_bits1(gb);
+ use_nlq = (hdr->rpu_format & 0x700) == 0 && !hdr->disable_residual_flag;
+
+ profile = s->config ? s->config->dv_profile : guess_profile(hdr);
+ if (profile == 5 && use_nlq) {
+ av_log(s->logctx, AV_LOG_ERROR, "Profile 5 RPUs should not use NLQ\n");
+ goto fail;
+ }
+
+ if (use_prev_vdr_rpu) {
+ int prev_vdr_rpu_id = get_ue_golomb_31(gb);
+ VALIDATE(prev_vdr_rpu_id, 0, DOVI_MAX_DM_ID);
+ if (!s->vdr_ref[prev_vdr_rpu_id]) {
+ av_log(s->logctx, AV_LOG_ERROR, "Unknown previous RPU ID: %u\n",
+ prev_vdr_rpu_id);
+ goto fail;
+ }
+ vdr = (DOVIVdrRef *) s->vdr_ref[prev_vdr_rpu_id]->data;
+ s->mapping = &vdr->mapping;
+ } else {
+ int vdr_rpu_id = get_ue_golomb_31(gb);
+ VALIDATE(vdr_rpu_id, 0, DOVI_MAX_DM_ID);
+ if (!s->vdr_ref[vdr_rpu_id]) {
+ s->vdr_ref[vdr_rpu_id] = av_buffer_allocz(sizeof(DOVIVdrRef));
+ if (!s->vdr_ref[vdr_rpu_id])
+ return AVERROR(ENOMEM);
+ }
+
+ vdr = (DOVIVdrRef *) s->vdr_ref[vdr_rpu_id]->data;
+ s->mapping = &vdr->mapping;
+
+ vdr->mapping.vdr_rpu_id = vdr_rpu_id;
+ vdr->mapping.mapping_color_space = get_ue_golomb_31(gb);
+ vdr->mapping.mapping_chroma_format_idc = get_ue_golomb_31(gb);
+
+ for (int c = 0; c < 3; c++) {
+ AVDOVIReshapingCurve *curve = &vdr->mapping.curves[c];
+ int num_pivots_minus_2 = get_ue_golomb_31(gb);
+ int pivot = 0;
+
+ VALIDATE(num_pivots_minus_2, 0, AV_DOVI_MAX_PIECES - 1);
+ curve->num_pivots = num_pivots_minus_2 + 2;
+ for (int i = 0; i < curve->num_pivots; i++) {
+ pivot += get_bits(gb, hdr->bl_bit_depth);
+ curve->pivots[i] = av_clip_uint16(pivot);
+ }
+ }
+
+ if (use_nlq) {
+ vdr->mapping.nlq_method_idc = get_bits(gb, 3);
+ /**
+ * The patent mentions another legal value, NLQ_MU_LAW, but it's
+ * not documented anywhere how to parse or apply that type of NLQ.
+ */
+ VALIDATE(vdr->mapping.nlq_method_idc, 0, AV_DOVI_NLQ_LINEAR_DZ);
+ } else {
+ vdr->mapping.nlq_method_idc = AV_DOVI_NLQ_NONE;
+ }
+
+ vdr->mapping.num_x_partitions = get_ue_golomb_long(gb) + 1;
+ vdr->mapping.num_y_partitions = get_ue_golomb_long(gb) + 1;
+ /* End of rpu_data_header(), start of vdr_rpu_data_payload() */
+
+ for (int c = 0; c < 3; c++) {
+ AVDOVIReshapingCurve *curve = &vdr->mapping.curves[c];
+ for (int i = 0; i < curve->num_pivots - 1; i++) {
+ int mapping_idc = get_ue_golomb_31(gb);
+ VALIDATE(mapping_idc, 0, 1);
+ curve->mapping_idc[i] = mapping_idc;
+ switch (mapping_idc) {
+ case AV_DOVI_MAPPING_POLYNOMIAL: {
+ int poly_order_minus1 = get_ue_golomb_31(gb);
+ VALIDATE(poly_order_minus1, 0, 1);
+ curve->poly_order[i] = poly_order_minus1 + 1;
+ if (poly_order_minus1 == 0) {
+ int linear_interp_flag = get_bits1(gb);
+ if (linear_interp_flag) {
+ /* lack of documentation/samples */
+ avpriv_request_sample(s->logctx, "Dolby Vision "
+ "linear interpolation");
+ ff_dovi_ctx_unref(s);
+ return AVERROR_PATCHWELCOME;
+ }
+ }
+ for (int k = 0; k <= curve->poly_order[i]; k++)
+ curve->poly_coef[i][k] = get_se_coef(gb, hdr);
+ break;
+ }
+ case AV_DOVI_MAPPING_MMR: {
+ int mmr_order_minus1 = get_bits(gb, 2);
+ VALIDATE(mmr_order_minus1, 0, 2);
+ curve->mmr_order[i] = mmr_order_minus1 + 1;
+ curve->mmr_constant[i] = get_se_coef(gb, hdr);
+ for (int j = 0; j < curve->mmr_order[i]; j++) {
+ for (int k = 0; k < 7; k++)
+ curve->mmr_coef[i][j][k] = get_se_coef(gb, hdr);
+ }
+ break;
+ }
+ }
+ }
+ }
+
+ if (use_nlq) {
+ for (int c = 0; c < 3; c++) {
+ AVDOVINLQParams *nlq = &vdr->mapping.nlq[c];
+ nlq->nlq_offset = get_bits(gb, hdr->el_bit_depth);
+ nlq->vdr_in_max = get_ue_coef(gb, hdr);
+ switch (vdr->mapping.nlq_method_idc) {
+ case AV_DOVI_NLQ_LINEAR_DZ:
+ nlq->linear_deadzone_slope = get_ue_coef(gb, hdr);
+ nlq->linear_deadzone_threshold = get_ue_coef(gb, hdr);
+ break;
+ }
+ }
+ }
+ }
+
+ if (vdr_dm_metadata_present) {
+ AVDOVIColorMetadata *color;
+ int affected_dm_id = get_ue_golomb_31(gb);
+ int current_dm_id = get_ue_golomb_31(gb);
+ VALIDATE(affected_dm_id, 0, DOVI_MAX_DM_ID);
+ VALIDATE(current_dm_id, 0, DOVI_MAX_DM_ID);
+ if (!s->vdr_ref[affected_dm_id]) {
+ s->vdr_ref[affected_dm_id] = av_buffer_allocz(sizeof(DOVIVdrRef));
+ if (!s->vdr_ref[affected_dm_id])
+ return AVERROR(ENOMEM);
+ }
+
+ if (!s->vdr_ref[current_dm_id]) {
+ av_log(s->logctx, AV_LOG_ERROR, "Unknown previous RPU DM ID: %u\n",
+ current_dm_id);
+ goto fail;
+ }
+
+ /* Update current pointer based on current_dm_id */
+ vdr = (DOVIVdrRef *) s->vdr_ref[current_dm_id]->data;
+ s->color = &vdr->color;
+
+ /* Update values of affected_dm_id */
+ vdr = (DOVIVdrRef *) s->vdr_ref[affected_dm_id]->data;
+ color = &vdr->color;
+ color->dm_metadata_id = affected_dm_id;
+ color->scene_refresh_flag = get_ue_golomb_31(gb);
+ for (int i = 0; i < 9; i++)
+ color->ycc_to_rgb_matrix[i] = av_make_q(get_sbits(gb, 16), 1 << 13);
+ for (int i = 0; i < 3; i++) {
+ int denom = profile == 4 ? (1 << 30) : (1 << 28);
+ unsigned offset = get_bits_long(gb, 32);
+ if (offset > INT_MAX) {
+ /* Ensure the result fits inside AVRational */
+ offset >>= 1;
+ denom >>= 1;
+ }
+ color->ycc_to_rgb_offset[i] = av_make_q(offset, denom);
+ }
+ for (int i = 0; i < 9; i++)
+ color->rgb_to_lms_matrix[i] = av_make_q(get_sbits(gb, 16), 1 << 14);
+
+ color->signal_eotf = get_bits(gb, 16);
+ color->signal_eotf_param0 = get_bits(gb, 16);
+ color->signal_eotf_param1 = get_bits(gb, 16);
+ color->signal_eotf_param2 = get_bits_long(gb, 32);
+ color->signal_bit_depth = get_bits(gb, 5);
+ VALIDATE(color->signal_bit_depth, 8, 16);
+ color->signal_color_space = get_bits(gb, 2);
+ color->signal_chroma_format = get_bits(gb, 2);
+ color->signal_full_range_flag = get_bits(gb, 2);
+ color->source_min_pq = get_bits(gb, 12);
+ color->source_max_pq = get_bits(gb, 12);
+ color->source_diagonal = get_bits(gb, 10);
+ }
+
+ /* FIXME: verify CRC32, requires implementation of AV_CRC_32_MPEG_2 */
+ return 0;
+
+fail:
+ ff_dovi_ctx_unref(s); /* don't leak potentially invalid state */
+ return AVERROR(EINVAL);
+}
diff --git a/libavcodec/dovi_rpu.h b/libavcodec/dovi_rpu.h
new file mode 100644
index 0000000000..6925940dd1
--- /dev/null
+++ b/libavcodec/dovi_rpu.h
@@ -0,0 +1,71 @@
+/*
+ * Dolby Vision RPU decoder
+ *
+ * Copyright (C) 2021 Jan Ekström
+ * Copyright (C) 2021 Niklas Haas
+ *
+ * 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
+ */
+
+#ifndef AVCODEC_DOVI_RPU_H
+#define AVCODEC_DOVI_RPU_H
+
+#include "libavutil/dovi_meta.h"
+#include "libavutil/frame.h"
+
+#define DOVI_MAX_DM_ID 15
+typedef struct DOVIContext {
+ void *logctx;
+ const AVDOVIDecoderConfigurationRecord *config; ///< provided by API user
+ AVBufferRef *vdr_ref[DOVI_MAX_DM_ID+1]; ///< decoded VDR data mappings
+
+ /**
+ * Currently active RPU data header, updates on every dovi_rpu_parse().
+ */
+ AVDOVIRpuDataHeader header;
+
+ /**
+ * Currently active data mappings, or NULL. Points into memory owned by the
+ * corresponding rpu/vdr_ref, which becomes invalid on the next call to
+ * dovi_rpu_parse.
+ */
+ const AVDOVIDataMapping *mapping;
+ const AVDOVIColorMetadata *color;
+} DOVIContext;
+
+/**
+ * Completely resets the internal state of a DOVIContext, but explicitly
+ * preserves any user-provided fields.
+ */
+void ff_dovi_ctx_unref(DOVIContext *s);
+
+int ff_dovi_ctx_replace(DOVIContext *s, const DOVIContext *s0);
+
+/**
+ * Parse the contents of a Dovi RPU NAL and update the parsed values in the
+ * DOVIContext struct.
+ *
+ * Returns 0 or an error code.
+ */
+int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size);
+
+/**
+ * Attach the decoded AVDOVIMetadata as side data to an AVFrame.
+ */
+int ff_dovi_attach_side_data(DOVIContext *s, AVFrame *frame);
+
+#endif /* AVCODEC_DOVI_RPU_H */
--
2.34.1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 13+ messages in thread
* [FFmpeg-devel] [PATCH v9 5/6] fate: Limit Dolby Vision RPU test frame count
2021-12-22 15:14 [FFmpeg-devel] [PATCH v9 1/6] lavu/frame: Add Dolby Vision metadata side data type Niklas Haas
` (2 preceding siblings ...)
2021-12-22 15:14 ` [FFmpeg-devel] [PATCH v9 4/6] lavc: Implement Dolby Vision RPU parsing Niklas Haas
@ 2021-12-22 15:14 ` Niklas Haas
2021-12-22 15:14 ` [FFmpeg-devel] [PATCH v9 6/6] lavc/hevcdec: Parse DOVI RPU NALs Niklas Haas
4 siblings, 0 replies; 13+ messages in thread
From: Niklas Haas @ 2021-12-22 15:14 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Niklas Haas
From: Niklas Haas <git@haasn.dev>
To avoid the ref for this growing to a very large size when attaching
the parsed RPU side data. Since this sample does not have any dynamic
metadata, two frames will serve just as well as 100.
Signed-off-by: Niklas Haas <git@haasn.dev>
---
tests/fate/hevc.mak | 2 +-
tests/ref/fate/hevc-dv-rpu | 499 -------------------------------------
2 files changed, 1 insertion(+), 500 deletions(-)
diff --git a/tests/fate/hevc.mak b/tests/fate/hevc.mak
index abe4264662..95dcf5bfc5 100644
--- a/tests/fate/hevc.mak
+++ b/tests/fate/hevc.mak
@@ -273,7 +273,7 @@ FATE_HEVC_FFPROBE-$(call DEMDEC, HEVC, HEVC) += fate-hevc-monochrome-crop
fate-hevc-hdr10-plus-metadata: CMD = probeframes -show_entries frame=side_data_list $(TARGET_SAMPLES)/hevc/hdr10_plus_h265_sample.hevc
FATE_HEVC_FFPROBE-$(call DEMDEC, HEVC, HEVC) += fate-hevc-hdr10-plus-metadata
-fate-hevc-dv-rpu: CMD = probeframes -show_entries frame=side_data_list -select_streams 0 $(TARGET_SAMPLES)/hevc/dv84.mov
+fate-hevc-dv-rpu: CMD = probeframes -show_entries frame=side_data_list -select_streams 0 -read_intervals "%+\#2" $(TARGET_SAMPLES)/hevc/dv84.mov
FATE_HEVC_FFPROBE-$(call DEMDEC, HEVC, HEVC) += fate-hevc-dv-rpu
fate-hevc-two-first-slice: CMD = threads=2 framemd5 -i $(TARGET_SAMPLES)/hevc/two_first_slice.mp4 -sws_flags bitexact -t 00:02.00 -an
diff --git a/tests/ref/fate/hevc-dv-rpu b/tests/ref/fate/hevc-dv-rpu
index 37ad9ffec2..416d9c51a6 100644
--- a/tests/ref/fate/hevc-dv-rpu
+++ b/tests/ref/fate/hevc-dv-rpu
@@ -11,502 +11,3 @@ side_data_type=Dolby Vision RPU Data
side_data_type=Dolby Vision RPU Data
[/SIDE_DATA]
[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=H.26[45] User Data Unregistered SEI message
-[/SIDE_DATA]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=H.26[45] User Data Unregistered SEI message
-[/SIDE_DATA]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=H.26[45] User Data Unregistered SEI message
-[/SIDE_DATA]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
-[FRAME]
-[SIDE_DATA]
-side_data_type=Dolby Vision RPU Data
-[/SIDE_DATA]
-[/FRAME]
--
2.34.1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 13+ messages in thread
* [FFmpeg-devel] [PATCH v9 6/6] lavc/hevcdec: Parse DOVI RPU NALs
2021-12-22 15:14 [FFmpeg-devel] [PATCH v9 1/6] lavu/frame: Add Dolby Vision metadata side data type Niklas Haas
` (3 preceding siblings ...)
2021-12-22 15:14 ` [FFmpeg-devel] [PATCH v9 5/6] fate: Limit Dolby Vision RPU test frame count Niklas Haas
@ 2021-12-22 15:14 ` Niklas Haas
2021-12-22 19:55 ` Andreas Rheinhardt
2022-01-02 4:31 ` Andreas Rheinhardt
4 siblings, 2 replies; 13+ messages in thread
From: Niklas Haas @ 2021-12-22 15:14 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Niklas Haas
From: Niklas Haas <git@haasn.dev>
And expose the parsed values as frame side data. Update FATE results to
match.
It's worth documenting that this relies on the dovi configuration record
being present on the first AVPacket fed to the decoder, which in
practice is the case if if the API user has called something like
av_format_inject_global_side_data, which is unfortunately not the
default.
This commit is not the time and place to change that behavior, though.
Signed-off-by: Niklas Haas <git@haasn.dev>
---
configure | 2 +-
libavcodec/hevcdec.c | 63 +++++++++--
libavcodec/hevcdec.h | 3 +
tests/ref/fate/hevc-dv-rpu | 224 +++++++++++++++++++++++++++++++++++++
4 files changed, 283 insertions(+), 9 deletions(-)
diff --git a/configure b/configure
index 68658a847f..7803aa47af 100755
--- a/configure
+++ b/configure
@@ -2826,7 +2826,7 @@ h264_decoder_suggest="error_resilience"
hap_decoder_select="snappy texturedsp"
hap_encoder_deps="libsnappy"
hap_encoder_select="texturedspenc"
-hevc_decoder_select="atsc_a53 bswapdsp cabac golomb hevcparse videodsp"
+hevc_decoder_select="atsc_a53 bswapdsp cabac dovi_rpu golomb hevcparse videodsp"
huffyuv_decoder_select="bswapdsp huffyuvdsp llviddsp"
huffyuv_encoder_select="bswapdsp huffman huffyuvencdsp llvidencdsp"
hymt_decoder_select="huffyuv_decoder"
diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
index 46d9edf8eb..298d89fea6 100644
--- a/libavcodec/hevcdec.c
+++ b/libavcodec/hevcdec.c
@@ -2723,6 +2723,7 @@ error:
static int set_side_data(HEVCContext *s)
{
AVFrame *out = s->ref->frame;
+ int ret;
if (s->sei.frame_packing.present &&
s->sei.frame_packing.arrangement_type >= 3 &&
@@ -2967,6 +2968,9 @@ static int set_side_data(HEVCContext *s)
s->rpu_buf = NULL;
}
+ if ((ret = ff_dovi_attach_side_data(&s->dovi_ctx, out)) < 0)
+ return ret;
+
return 0;
}
@@ -3298,16 +3302,24 @@ static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
if (s->pkt.nb_nals > 1 && s->pkt.nals[s->pkt.nb_nals - 1].type == HEVC_NAL_UNSPEC62 &&
s->pkt.nals[s->pkt.nb_nals - 1].size > 2 && !s->pkt.nals[s->pkt.nb_nals - 1].nuh_layer_id
&& !s->pkt.nals[s->pkt.nb_nals - 1].temporal_id) {
+ H2645NAL *nal = &s->pkt.nals[s->pkt.nb_nals - 1];
if (s->rpu_buf) {
av_buffer_unref(&s->rpu_buf);
av_log(s->avctx, AV_LOG_WARNING, "Multiple Dolby Vision RPUs found in one AU. Skipping previous.\n");
}
- s->rpu_buf = av_buffer_alloc(s->pkt.nals[s->pkt.nb_nals - 1].raw_size - 2);
+ s->rpu_buf = av_buffer_alloc(nal->raw_size - 2);
if (!s->rpu_buf)
return AVERROR(ENOMEM);
+ memcpy(s->rpu_buf->data, nal->raw_data + 2, nal->raw_size - 2);
- memcpy(s->rpu_buf->data, s->pkt.nals[s->pkt.nb_nals - 1].raw_data + 2, s->pkt.nals[s->pkt.nb_nals - 1].raw_size - 2);
+ s->dovi_ctx.config = s->dovi_cfg ? (void *) s->dovi_cfg->data : NULL;
+ ret = ff_dovi_rpu_parse(&s->dovi_ctx, nal->data + 2, nal->size - 2);
+ if (ret < 0) {
+ av_buffer_unref(&s->rpu_buf);
+ av_log(s->avctx, AV_LOG_WARNING, "Error parsing DOVI NAL unit.\n");
+ /* ignore */
+ }
}
/* decode the NAL units */
@@ -3440,8 +3452,8 @@ static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
AVPacket *avpkt)
{
int ret;
- size_t new_extradata_size;
- uint8_t *new_extradata;
+ uint8_t *sd;
+ size_t sd_size;
HEVCContext *s = avctx->priv_data;
if (!avpkt->size) {
@@ -3453,14 +3465,37 @@ static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
return 0;
}
- new_extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
- &new_extradata_size);
- if (new_extradata && new_extradata_size > 0) {
- ret = hevc_decode_extradata(s, new_extradata, new_extradata_size, 0);
+ sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &sd_size);
+ if (sd && sd_size > 0) {
+ ret = hevc_decode_extradata(s, sd, sd_size, 0);
if (ret < 0)
return ret;
}
+ sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_DOVI_CONF, &sd_size);
+ if (sd && sd_size > 0) {
+ if (s->dovi_cfg) {
+ /* Reuse existing buffer */
+ if ((ret = av_buffer_make_writable(&s->dovi_cfg)) < 0)
+ return ret;
+ } else {
+ /* Allocate new buffer */
+ AVDOVIDecoderConfigurationRecord *cfg;
+ size_t cfg_size;
+ cfg = av_dovi_alloc(&cfg_size);
+ if (!cfg)
+ return AVERROR(ENOMEM);
+ s->dovi_cfg = av_buffer_create((uint8_t *) cfg, cfg_size, NULL, NULL, 0);
+ if (!s->dovi_cfg) {
+ av_free(cfg);
+ return AVERROR(ENOMEM);
+ }
+ }
+
+ av_assert0(sd_size >= s->dovi_cfg->size);
+ memcpy(s->dovi_cfg->data, sd, s->dovi_cfg->size);
+ }
+
s->ref = NULL;
ret = decode_nal_units(s, avpkt->data, avpkt->size);
if (ret < 0)
@@ -3553,6 +3588,8 @@ static av_cold int hevc_decode_free(AVCodecContext *avctx)
pic_arrays_free(s);
+ ff_dovi_ctx_unref(&s->dovi_ctx);
+ av_buffer_unref(&s->dovi_cfg);
av_buffer_unref(&s->rpu_buf);
av_freep(&s->md5_ctx);
@@ -3637,6 +3674,7 @@ static av_cold int hevc_init_context(AVCodecContext *avctx)
ff_bswapdsp_init(&s->bdsp);
+ s->dovi_ctx.logctx = avctx;
s->context_initialized = 1;
s->eos = 0;
@@ -3745,6 +3783,14 @@ static int hevc_update_thread_context(AVCodecContext *dst,
if (ret < 0)
return ret;
+ ret = av_buffer_replace(&s->dovi_cfg, s0->dovi_cfg);
+ if (ret < 0)
+ return ret;
+
+ ret = ff_dovi_ctx_replace(&s->dovi_ctx, &s0->dovi_ctx);
+ if (ret < 0)
+ return ret;
+
s->sei.frame_packing = s0->sei.frame_packing;
s->sei.display_orientation = s0->sei.display_orientation;
s->sei.mastering_display = s0->sei.mastering_display;
@@ -3801,6 +3847,7 @@ static void hevc_decode_flush(AVCodecContext *avctx)
HEVCContext *s = avctx->priv_data;
ff_hevc_flush_dpb(s);
ff_hevc_reset_sei(&s->sei);
+ ff_dovi_ctx_unref(&s->dovi_ctx);
av_buffer_unref(&s->rpu_buf);
s->max_ra = INT_MAX;
s->eos = 1;
diff --git a/libavcodec/hevcdec.h b/libavcodec/hevcdec.h
index 870ff178d4..c8dde6fd17 100644
--- a/libavcodec/hevcdec.h
+++ b/libavcodec/hevcdec.h
@@ -32,6 +32,7 @@
#include "avcodec.h"
#include "bswapdsp.h"
#include "cabac.h"
+#include "dovi_rpu.h"
#include "get_bits.h"
#include "hevcpred.h"
#include "h2645_parse.h"
@@ -574,6 +575,8 @@ typedef struct HEVCContext {
int nuh_layer_id;
AVBufferRef *rpu_buf; ///< 0 or 1 Dolby Vision RPUs.
+ AVBufferRef *dovi_cfg; ///< contains AVDOVIDecoderConfigurationRecord
+ DOVIContext dovi_ctx; ///< Dolby Vision decoding context
} HEVCContext;
/**
diff --git a/tests/ref/fate/hevc-dv-rpu b/tests/ref/fate/hevc-dv-rpu
index 416d9c51a6..1980ab13ea 100644
--- a/tests/ref/fate/hevc-dv-rpu
+++ b/tests/ref/fate/hevc-dv-rpu
@@ -5,9 +5,233 @@ side_data_type=H.26[45] User Data Unregistered SEI message
[SIDE_DATA]
side_data_type=Dolby Vision RPU Data
[/SIDE_DATA]
+[SIDE_DATA]
+side_data_type=Dolby Vision Metadata
+rpu_type=2
+rpu_format=18
+vdr_rpu_profile=1
+vdr_rpu_level=0
+chroma_resampling_explicit_filter_flag=0
+coef_data_type=0
+coef_log2_denom=23
+vdr_rpu_normalized_idc=1
+bl_video_full_range_flag=0
+bl_bit_depth=10
+el_bit_depth=10
+vdr_bit_depth=12
+spatial_resampling_filter_flag=0
+el_spatial_resampling_filter_flag=0
+disable_residual_flag=1
+vdr_rpu_id=0
+mapping_color_space=0
+mapping_chroma_format_idc=0
+nlq_method_idc=-1
+nlq_method_idc_name=none
+num_x_partitions=1
+num_y_partitions=1
+[COMPONENT]
+pivots=63 132 362 618 874 911 927 935 942
+[SECTION]
+mapping_idc=0
+mapping_idc_name=polynomial
+poly_order=2
+poly_coef=-409680 16721463 -20276640
+[/SECTION]
+[SECTION]
+mapping_idc=0
+mapping_idc_name=polynomial
+poly_order=2
+poly_coef=-119056 13575212 -12867889
+[/SECTION]
+[SECTION]
+mapping_idc=0
+mapping_idc_name=polynomial
+poly_order=2
+poly_coef=1317527 5338528 -948122
+[/SECTION]
+[SECTION]
+mapping_idc=0
+mapping_idc_name=polynomial
+poly_order=2
+poly_coef=2119979 2065496 2288524
+[/SECTION]
+[SECTION]
+mapping_idc=0
+mapping_idc_name=polynomial
+poly_order=2
+poly_coef=7982780 -11367226 9973944
+[/SECTION]
+[SECTION]
+mapping_idc=0
+mapping_idc_name=polynomial
+poly_order=2
+poly_coef=53792084 -114243184 67724328
+[/SECTION]
+[SECTION]
+mapping_idc=0
+mapping_idc_name=polynomial
+poly_order=2
+poly_coef=112973872 -244837568 139764480
+[/SECTION]
+[SECTION]
+mapping_idc=0
+mapping_idc_name=polynomial
+poly_order=2
+poly_coef=236828416 -518849056 291306944
+[/SECTION]
+[/COMPONENT]
+[COMPONENT]
+pivots=0 1023
+[SECTION]
+mapping_idc=1
+mapping_idc_name=mmr
+mmr_order=3
+mmr_constant=-4200453
+mmr_coef=9094176 31944476 739652 -27103344 -3431599 -11015088 22758042 -2028643 -30021218 -906885 26272992 7291404 16488745 -78087176 -1487777 12496543 762460 -4281948 -5768035 -7843163 103636960
+[/SECTION]
+[/COMPONENT]
+[COMPONENT]
+pivots=0 1023
+[SECTION]
+mapping_idc=1
+mapping_idc_name=mmr
+mmr_order=3
+mmr_constant=-10387889
+mmr_coef=29604202 3214133 46206184 -8564340 -53383996 1628407 5426045 -21634202 -5251953 -50812292 19221972 76726656 -425892 -35041240 5917361 2863974 25030554 -14404292 -41230120 1229046 53575140
+[/SECTION]
+[/COMPONENT]
+dm_metadata_id=0
+scene_refresh_flag=1
+ycc_to_rgb_matrix=9574/8192 0/8192 13802/8192 9574/8192 -1540/8192 -5348/8192 9574/8192 17610/8192 0/8192
+ycc_to_rgb_offset=16777216/268435456 134217728/268435456 134217728/268435456
+rgb_to_lms_matrix=7222/16384 8771/16384 390/16384 2654/16384 12430/16384 1300/16384 0/16384 422/16384 15962/16384
+signal_eotf=65535
+signal_eotf_param0=0
+signal_eotf_param1=0
+signal_eotf_param2=0
+signal_bit_depth=12
+signal_color_space=0
+signal_chroma_format=0
+signal_full_range_flag=1
+source_min_pq=0
+source_max_pq=3079
+source_diagonal=42
+[/SIDE_DATA]
[/FRAME]
[FRAME]
[SIDE_DATA]
side_data_type=Dolby Vision RPU Data
[/SIDE_DATA]
+[SIDE_DATA]
+side_data_type=Dolby Vision Metadata
+rpu_type=2
+rpu_format=18
+vdr_rpu_profile=1
+vdr_rpu_level=0
+chroma_resampling_explicit_filter_flag=0
+coef_data_type=0
+coef_log2_denom=23
+vdr_rpu_normalized_idc=1
+bl_video_full_range_flag=0
+bl_bit_depth=10
+el_bit_depth=10
+vdr_bit_depth=12
+spatial_resampling_filter_flag=0
+el_spatial_resampling_filter_flag=0
+disable_residual_flag=1
+vdr_rpu_id=0
+mapping_color_space=0
+mapping_chroma_format_idc=0
+nlq_method_idc=-1
+nlq_method_idc_name=none
+num_x_partitions=1
+num_y_partitions=1
+[COMPONENT]
+pivots=63 132 362 618 874 911 927 935 942
+[SECTION]
+mapping_idc=0
+mapping_idc_name=polynomial
+poly_order=2
+poly_coef=-409680 16721463 -20276640
+[/SECTION]
+[SECTION]
+mapping_idc=0
+mapping_idc_name=polynomial
+poly_order=2
+poly_coef=-119056 13575212 -12867889
+[/SECTION]
+[SECTION]
+mapping_idc=0
+mapping_idc_name=polynomial
+poly_order=2
+poly_coef=1317527 5338528 -948122
+[/SECTION]
+[SECTION]
+mapping_idc=0
+mapping_idc_name=polynomial
+poly_order=2
+poly_coef=2119979 2065496 2288524
+[/SECTION]
+[SECTION]
+mapping_idc=0
+mapping_idc_name=polynomial
+poly_order=2
+poly_coef=7982780 -11367226 9973944
+[/SECTION]
+[SECTION]
+mapping_idc=0
+mapping_idc_name=polynomial
+poly_order=2
+poly_coef=53792084 -114243184 67724328
+[/SECTION]
+[SECTION]
+mapping_idc=0
+mapping_idc_name=polynomial
+poly_order=2
+poly_coef=112973872 -244837568 139764480
+[/SECTION]
+[SECTION]
+mapping_idc=0
+mapping_idc_name=polynomial
+poly_order=2
+poly_coef=236828416 -518849056 291306944
+[/SECTION]
+[/COMPONENT]
+[COMPONENT]
+pivots=0 1023
+[SECTION]
+mapping_idc=1
+mapping_idc_name=mmr
+mmr_order=3
+mmr_constant=-4200453
+mmr_coef=9094176 31944476 739652 -27103344 -3431599 -11015088 22758042 -2028643 -30021218 -906885 26272992 7291404 16488745 -78087176 -1487777 12496543 762460 -4281948 -5768035 -7843163 103636960
+[/SECTION]
+[/COMPONENT]
+[COMPONENT]
+pivots=0 1023
+[SECTION]
+mapping_idc=1
+mapping_idc_name=mmr
+mmr_order=3
+mmr_constant=-10387889
+mmr_coef=29604202 3214133 46206184 -8564340 -53383996 1628407 5426045 -21634202 -5251953 -50812292 19221972 76726656 -425892 -35041240 5917361 2863974 25030554 -14404292 -41230120 1229046 53575140
+[/SECTION]
+[/COMPONENT]
+dm_metadata_id=0
+scene_refresh_flag=1
+ycc_to_rgb_matrix=9574/8192 0/8192 13802/8192 9574/8192 -1540/8192 -5348/8192 9574/8192 17610/8192 0/8192
+ycc_to_rgb_offset=16777216/268435456 134217728/268435456 134217728/268435456
+rgb_to_lms_matrix=7222/16384 8771/16384 390/16384 2654/16384 12430/16384 1300/16384 0/16384 422/16384 15962/16384
+signal_eotf=65535
+signal_eotf_param0=0
+signal_eotf_param1=0
+signal_eotf_param2=0
+signal_bit_depth=12
+signal_color_space=0
+signal_chroma_format=0
+signal_full_range_flag=1
+source_min_pq=0
+source_max_pq=3079
+source_diagonal=42
+[/SIDE_DATA]
[/FRAME]
--
2.34.1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [FFmpeg-devel] [PATCH v9 6/6] lavc/hevcdec: Parse DOVI RPU NALs
2021-12-22 15:14 ` [FFmpeg-devel] [PATCH v9 6/6] lavc/hevcdec: Parse DOVI RPU NALs Niklas Haas
@ 2021-12-22 19:55 ` Andreas Rheinhardt
2022-01-02 4:31 ` Andreas Rheinhardt
1 sibling, 0 replies; 13+ messages in thread
From: Andreas Rheinhardt @ 2021-12-22 19:55 UTC (permalink / raw)
To: ffmpeg-devel
Niklas Haas:
> From: Niklas Haas <git@haasn.dev>
>
> And expose the parsed values as frame side data. Update FATE results to
> match.
>
> It's worth documenting that this relies on the dovi configuration record
> being present on the first AVPacket fed to the decoder, which in
> practice is the case if if the API user has called something like
> av_format_inject_global_side_data, which is unfortunately not the
> default.
>
> This commit is not the time and place to change that behavior, though.
>
> Signed-off-by: Niklas Haas <git@haasn.dev>
> ---
> configure | 2 +-
> libavcodec/hevcdec.c | 63 +++++++++--
> libavcodec/hevcdec.h | 3 +
> tests/ref/fate/hevc-dv-rpu | 224 +++++++++++++++++++++++++++++++++++++
> 4 files changed, 283 insertions(+), 9 deletions(-)
>
> diff --git a/configure b/configure
> index 68658a847f..7803aa47af 100755
> --- a/configure
> +++ b/configure
> @@ -2826,7 +2826,7 @@ h264_decoder_suggest="error_resilience"
> hap_decoder_select="snappy texturedsp"
> hap_encoder_deps="libsnappy"
> hap_encoder_select="texturedspenc"
> -hevc_decoder_select="atsc_a53 bswapdsp cabac golomb hevcparse videodsp"
> +hevc_decoder_select="atsc_a53 bswapdsp cabac dovi_rpu golomb hevcparse videodsp"
> huffyuv_decoder_select="bswapdsp huffyuvdsp llviddsp"
> huffyuv_encoder_select="bswapdsp huffman huffyuvencdsp llvidencdsp"
> hymt_decoder_select="huffyuv_decoder"
> diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
> index 46d9edf8eb..298d89fea6 100644
> --- a/libavcodec/hevcdec.c
> +++ b/libavcodec/hevcdec.c
> @@ -2723,6 +2723,7 @@ error:
> static int set_side_data(HEVCContext *s)
> {
> AVFrame *out = s->ref->frame;
> + int ret;
>
> if (s->sei.frame_packing.present &&
> s->sei.frame_packing.arrangement_type >= 3 &&
> @@ -2967,6 +2968,9 @@ static int set_side_data(HEVCContext *s)
> s->rpu_buf = NULL;
> }
>
> + if ((ret = ff_dovi_attach_side_data(&s->dovi_ctx, out)) < 0)
> + return ret;
> +
> return 0;
> }
>
> @@ -3298,16 +3302,24 @@ static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
> if (s->pkt.nb_nals > 1 && s->pkt.nals[s->pkt.nb_nals - 1].type == HEVC_NAL_UNSPEC62 &&
> s->pkt.nals[s->pkt.nb_nals - 1].size > 2 && !s->pkt.nals[s->pkt.nb_nals - 1].nuh_layer_id
> && !s->pkt.nals[s->pkt.nb_nals - 1].temporal_id) {
> + H2645NAL *nal = &s->pkt.nals[s->pkt.nb_nals - 1];
> if (s->rpu_buf) {
> av_buffer_unref(&s->rpu_buf);
> av_log(s->avctx, AV_LOG_WARNING, "Multiple Dolby Vision RPUs found in one AU. Skipping previous.\n");
> }
>
> - s->rpu_buf = av_buffer_alloc(s->pkt.nals[s->pkt.nb_nals - 1].raw_size - 2);
> + s->rpu_buf = av_buffer_alloc(nal->raw_size - 2);
> if (!s->rpu_buf)
> return AVERROR(ENOMEM);
> + memcpy(s->rpu_buf->data, nal->raw_data + 2, nal->raw_size - 2);
>
> - memcpy(s->rpu_buf->data, s->pkt.nals[s->pkt.nb_nals - 1].raw_data + 2, s->pkt.nals[s->pkt.nb_nals - 1].raw_size - 2);
> + s->dovi_ctx.config = s->dovi_cfg ? (void *) s->dovi_cfg->data : NULL;
> + ret = ff_dovi_rpu_parse(&s->dovi_ctx, nal->data + 2, nal->size - 2);
> + if (ret < 0) {
> + av_buffer_unref(&s->rpu_buf);
> + av_log(s->avctx, AV_LOG_WARNING, "Error parsing DOVI NAL unit.\n");
> + /* ignore */
> + }
> }
>
> /* decode the NAL units */
> @@ -3440,8 +3452,8 @@ static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
> AVPacket *avpkt)
> {
> int ret;
> - size_t new_extradata_size;
> - uint8_t *new_extradata;
> + uint8_t *sd;
> + size_t sd_size;
> HEVCContext *s = avctx->priv_data;
>
> if (!avpkt->size) {
> @@ -3453,14 +3465,37 @@ static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
> return 0;
> }
>
> - new_extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
> - &new_extradata_size);
> - if (new_extradata && new_extradata_size > 0) {
> - ret = hevc_decode_extradata(s, new_extradata, new_extradata_size, 0);
> + sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &sd_size);
> + if (sd && sd_size > 0) {
> + ret = hevc_decode_extradata(s, sd, sd_size, 0);
> if (ret < 0)
> return ret;
> }
>
> + sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_DOVI_CONF, &sd_size);
> + if (sd && sd_size > 0) {
> + if (s->dovi_cfg) {
> + /* Reuse existing buffer */
> + if ((ret = av_buffer_make_writable(&s->dovi_cfg)) < 0)
> + return ret;
> + } else {
> + /* Allocate new buffer */
> + AVDOVIDecoderConfigurationRecord *cfg;
> + size_t cfg_size;
> + cfg = av_dovi_alloc(&cfg_size);
> + if (!cfg)
> + return AVERROR(ENOMEM);
> + s->dovi_cfg = av_buffer_create((uint8_t *) cfg, cfg_size, NULL, NULL, 0);
> + if (!s->dovi_cfg) {
> + av_free(cfg);
> + return AVERROR(ENOMEM);
> + }
> + }
> +
> + av_assert0(sd_size >= s->dovi_cfg->size);
Actually, your dovi_cfg comes from libavutil (allocated via
av_dovi_alloc()), so it should be the newest and biggest dovi struct
there is. And the side data's AV_PKT_DATA_DOVI_CONF should
also emanate from av_dovi_alloc(), so its size should coincide with
dovi_cfg->size.
> + memcpy(s->dovi_cfg->data, sd, s->dovi_cfg->size);
> + }
> +
> s->ref = NULL;
> ret = decode_nal_units(s, avpkt->data, avpkt->size);
> if (ret < 0)
> @@ -3553,6 +3588,8 @@ static av_cold int hevc_decode_free(AVCodecContext *avctx)
>
> pic_arrays_free(s);
>
> + ff_dovi_ctx_unref(&s->dovi_ctx);
> + av_buffer_unref(&s->dovi_cfg);
> av_buffer_unref(&s->rpu_buf);
>
> av_freep(&s->md5_ctx);
> @@ -3637,6 +3674,7 @@ static av_cold int hevc_init_context(AVCodecContext *avctx)
>
> ff_bswapdsp_init(&s->bdsp);
>
> + s->dovi_ctx.logctx = avctx;
> s->context_initialized = 1;
> s->eos = 0;
>
> @@ -3745,6 +3783,14 @@ static int hevc_update_thread_context(AVCodecContext *dst,
> if (ret < 0)
> return ret;
>
> + ret = av_buffer_replace(&s->dovi_cfg, s0->dovi_cfg);
> + if (ret < 0)
> + return ret;
> +
> + ret = ff_dovi_ctx_replace(&s->dovi_ctx, &s0->dovi_ctx);
> + if (ret < 0)
> + return ret;
> +
> s->sei.frame_packing = s0->sei.frame_packing;
> s->sei.display_orientation = s0->sei.display_orientation;
> s->sei.mastering_display = s0->sei.mastering_display;
> @@ -3801,6 +3847,7 @@ static void hevc_decode_flush(AVCodecContext *avctx)
> HEVCContext *s = avctx->priv_data;
> ff_hevc_flush_dpb(s);
> ff_hevc_reset_sei(&s->sei);
> + ff_dovi_ctx_unref(&s->dovi_ctx);
> av_buffer_unref(&s->rpu_buf);
> s->max_ra = INT_MAX;
> s->eos = 1;
> diff --git a/libavcodec/hevcdec.h b/libavcodec/hevcdec.h
> index 870ff178d4..c8dde6fd17 100644
> --- a/libavcodec/hevcdec.h
> +++ b/libavcodec/hevcdec.h
> @@ -32,6 +32,7 @@
> #include "avcodec.h"
> #include "bswapdsp.h"
> #include "cabac.h"
> +#include "dovi_rpu.h"
> #include "get_bits.h"
> #include "hevcpred.h"
> #include "h2645_parse.h"
> @@ -574,6 +575,8 @@ typedef struct HEVCContext {
> int nuh_layer_id;
>
> AVBufferRef *rpu_buf; ///< 0 or 1 Dolby Vision RPUs.
> + AVBufferRef *dovi_cfg; ///< contains AVDOVIDecoderConfigurationRecord
> + DOVIContext dovi_ctx; ///< Dolby Vision decoding context
> } HEVCContext;
>
> /**
1. Patchwork said there was a new warning when applying this patch.
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [FFmpeg-devel] [PATCH v9 4/6] lavc: Implement Dolby Vision RPU parsing
2021-12-22 15:14 ` [FFmpeg-devel] [PATCH v9 4/6] lavc: Implement Dolby Vision RPU parsing Niklas Haas
@ 2022-01-02 4:18 ` Andreas Rheinhardt
2022-01-02 12:45 ` James Almer
2022-01-02 19:01 ` Hendrik Leppkes
0 siblings, 2 replies; 13+ messages in thread
From: Andreas Rheinhardt @ 2022-01-02 4:18 UTC (permalink / raw)
To: ffmpeg-devel
Niklas Haas:
> From: Niklas Haas <git@haasn.dev>
>
> Based on a mixture of guesswork, partial documentation in patents, and
> reverse engineering of real-world samples. Confirmed working for all the
> samples I've thrown at it.
>
> Contains some annoying machinery to persist these values in between
> frames, which is needed in theory even though I've never actually seen a
> sample that relies on it in practice. May or may not work.
>
> Since the distinction matters greatly for parsing the color matrix
> values, this includes a small helper function to guess the right profile
> from the RPU itself in case the user has forgotten to forward the dovi
> configuration record to the decoder. (Which in practice, only ffmpeg.c
> and ffplay do..)
>
> Notable omissions / deviations:
> - CRC32 verification. This is based on the MPEG2 CRC32 type, which does
> not seem to be implemented in lavu. (And I don't care enough to do so)
> - Linear interpolation support. Nothing documents this (beyond its
> existence) and no samples use it, so impossible to implement.
> - All of the extension metadata blocks, but these contain values that
> seem largely congruent with ST2094, HDR10, or other existing forms of
> side data, so I will defer parsing/attaching them to a future commit.
> - The patent describes a mechanism for predicting coefficients from
> previous RPUs, but the bit for the flag whether to use the
> prediction deltas or signal entirely new coefficients does not seem to
> be present in actual RPUs, so we ignore this subsystem entirely.
> - In the patent's spec, the NLQ subsystem also loops over
> num_nlq_pivots, but even in the patent the number is hard-coded to one
> iteration rather than signalled. So we only store one set of coefs.
>
> Heavily influenced by https://github.com/quietvoid/dovi_tool
> Documentation drawn from US Patent 10,701,399 B2 and ETSI GS CCM 001
>
> Signed-off-by: Niklas Haas <git@haasn.dev>
> ---
> configure | 2 +
> libavcodec/Makefile | 1 +
> libavcodec/dovi_rpu.c | 430 ++++++++++++++++++++++++++++++++++++++++++
> libavcodec/dovi_rpu.h | 71 +++++++
> 4 files changed, 504 insertions(+)
> create mode 100644 libavcodec/dovi_rpu.c
> create mode 100644 libavcodec/dovi_rpu.h
>
> diff --git a/configure b/configure
> index 0ccd3bda11..68658a847f 100755
> --- a/configure
> +++ b/configure
> @@ -2434,6 +2434,7 @@ CONFIG_EXTRA="
> cbs_vp9
> dirac_parse
> dnn
> + dovi_rpu
> dvprofile
> exif
> faandct
> @@ -2706,6 +2707,7 @@ cbs_mpeg2_select="cbs"
> cbs_vp9_select="cbs"
> dct_select="rdft"
> dirac_parse_select="golomb"
> +dovi_rpu_select="golomb"
> dnn_suggest="libtensorflow libopenvino"
> dnn_deps="avformat swscale"
> error_resilience_select="me_cmp"
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index fb90ecea84..7364c7a91f 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -77,6 +77,7 @@ OBJS-$(CONFIG_CBS_MPEG2) += cbs_mpeg2.o
> OBJS-$(CONFIG_CBS_VP9) += cbs_vp9.o
> OBJS-$(CONFIG_CRYSTALHD) += crystalhd.o
> OBJS-$(CONFIG_DCT) += dct.o dct32_fixed.o dct32_float.o
> +OBJS-$(CONFIG_DOVI_RPU) += dovi_rpu.o
> OBJS-$(CONFIG_ERROR_RESILIENCE) += error_resilience.o
> OBJS-$(CONFIG_EXIF) += exif.o tiff_common.o
> OBJS-$(CONFIG_FAANDCT) += faandct.o
> diff --git a/libavcodec/dovi_rpu.c b/libavcodec/dovi_rpu.c
> new file mode 100644
> index 0000000000..fc2e1fb2a1
> --- /dev/null
> +++ b/libavcodec/dovi_rpu.c
> @@ -0,0 +1,430 @@
> +/*
> + * Dolby Vision RPU decoder
> + *
> + * Copyright (C) 2021 Jan Ekström
> + * Copyright (C) 2021 Niklas Haas
> + *
> + * 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/buffer.h"
> +
> +#include "dovi_rpu.h"
> +#include "golomb.h"
> +#include "get_bits.h"
> +
> +enum {
> + RPU_COEFF_FIXED = 0,
> + RPU_COEFF_FLOAT = 1,
> +};
> +
> +/**
> + * Private contents of vdr_ref.
> + */
> +typedef struct DOVIVdrRef {
> + AVDOVIDataMapping mapping;
> + AVDOVIColorMetadata color;
> +} DOVIVdrRef;
> +
> +void ff_dovi_ctx_unref(DOVIContext *s)
> +{
> + for (int i = 0; i < FF_ARRAY_ELEMS(s->vdr_ref); i++)
> + av_buffer_unref(&s->vdr_ref[i]);
> +
> + /* Preserve the user-provided fields explicitly, reset everything else */
> + *s = (DOVIContext) {
> + .logctx = s->logctx,
> + .config = s->config,
> + };
> +}
> +
> +int ff_dovi_ctx_replace(DOVIContext *s, const DOVIContext *s0)
> +{
> + int ret;
> + s->logctx = s0->logctx;
> + s->config = s0->config;
> + s->mapping = s0->mapping;
> + s->color = s0->color;
> + for (int i = 0; i < DOVI_MAX_DM_ID; i++) {
> + if ((ret = av_buffer_replace(&s->vdr_ref[i], s0->vdr_ref[i])) < 0)
> + goto fail;
> + }
> +
> + return 0;
> +
> +fail:
> + ff_dovi_ctx_unref(s);
> + return ret;
> +}
> +
> +int ff_dovi_attach_side_data(DOVIContext *s, AVFrame *frame)
> +{
> + AVFrameSideData *sd;
> + AVBufferRef *buf;
> + AVDOVIMetadata *dovi;
> + size_t dovi_size;
> +
> + if (!s->mapping || !s->color)
> + return 0; /* incomplete dovi metadata */
> +
> + dovi = av_dovi_metadata_alloc(&dovi_size);
> + if (!dovi)
> + return AVERROR(ENOMEM);
> +
> + buf = av_buffer_create((uint8_t *) dovi, dovi_size, NULL, NULL, 0);
> + if (!buf) {
> + av_free(dovi);
> + return AVERROR(ENOMEM);
> + }
> +
> + sd = av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_DOVI_METADATA, buf);
> + if (!sd) {
> + av_buffer_unref(&buf);
> + return AVERROR(ENOMEM);
> + }
> +
> + memcpy(av_dovi_get_header(dovi), &s->header, sizeof(s->header));
> + memcpy(av_dovi_get_mapping(dovi), s->mapping, sizeof(*s->mapping));
> + memcpy(av_dovi_get_color(dovi), s->color, sizeof(*s->color));
These are potentially problematic due to trailing padding in the
structures. This trailing padding might become valid fields in newer
versions of this structure. E.g. av_dovi_metadata_alloc() might
intentionally set the default values of these new fields to invalid
values. Yet lavc (with the old structure) sees them as padding, allowing
the compiler to trash these values on every write to these structures.
More likely though is that they will never be touched at all and
therefore retain their default (zero) initialization. Which might
conflict with the default value of these fields.
This can be fixed by only copying everything up to and including the
last field that we know of for every of these structures. This will
unfortunately increase the maintainence burden a bit.
> + return 0;
> +}
> +
> +static int guess_profile(const AVDOVIRpuDataHeader *hdr)
> +{
> + switch (hdr->vdr_rpu_profile) {
> + case 0:
> + if (hdr->bl_video_full_range_flag)
> + return 5;
> + break;
> + case 1:
> + if (hdr->el_spatial_resampling_filter_flag && !hdr->disable_residual_flag) {
> + if (hdr->vdr_bit_depth == 12) {
> + return 7;
> + } else {
> + return 4;
> + }
> + } else {
> + return 8;
> + }
> + }
> +
> + return 0; /* unknown */
> +}
> +
> +static inline uint64_t get_ue_coef(GetBitContext *gb, const AVDOVIRpuDataHeader *hdr)
> +{
> + uint64_t ipart;
> + union { uint32_t u32; float f32; } fpart;
> +
> + switch (hdr->coef_data_type) {
> + case RPU_COEFF_FIXED:
> + ipart = get_ue_golomb_long(gb);
> + fpart.u32 = get_bits_long(gb, hdr->coef_log2_denom);
> + return (ipart << hdr->coef_log2_denom) + fpart.u32;
Here and in get_se_coef below the f32 member of the union is not used
with RPU_COEFF_FIXED, so using the union seems unnecessary. It is also
slightly confusing, because upon seeing this and seeing that the union
is used with RPU_COEFF_FIXED, I expect it to be used for type-punning.
> +
> + case RPU_COEFF_FLOAT:
> + fpart.u32 = get_bits_long(gb, 32);
> + return fpart.f32 * (1 << hdr->coef_log2_denom);
You could just use av_int2float() here instead of adding the union yourself.
> + }
> +
> + return 0; /* unreachable */
> +}
> +
> +static inline int64_t get_se_coef(GetBitContext *gb, const AVDOVIRpuDataHeader *hdr)
> +{
> + int64_t ipart;
> + union { uint32_t u32; float f32; } fpart;
> +
> + switch (hdr->coef_data_type) {
> + case RPU_COEFF_FIXED:
> + ipart = get_se_golomb_long(gb);
> + fpart.u32 = get_bits_long(gb, hdr->coef_log2_denom);
> + return (ipart << hdr->coef_log2_denom) + fpart.u32;
> +
> + case RPU_COEFF_FLOAT:
> + fpart.u32 = get_bits_long(gb, 32);
> + return fpart.f32 * (1 << hdr->coef_log2_denom);
> + }
> +
> + return 0; /* unreachable */
> +}
> +
> +#define VALIDATE(VAR, MIN, MAX) \
> + do { \
> + if (VAR < MIN || VAR > MAX) { \
> + av_log(s->logctx, AV_LOG_ERROR, "RPU validation failed: " \
> + #MIN" <= "#VAR" = %d <= "#MAX"\n", (int) VAR); \
> + goto fail; \
> + } \
> + } while (0)
> +
> +int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size)
> +{
> + AVDOVIRpuDataHeader *hdr = &s->header;
> + GetBitContext *gb = &(GetBitContext){0};
> + DOVIVdrRef *vdr;
> + int ret;
> +
> + uint8_t nal_prefix;
> + uint8_t rpu_type;
> + uint8_t vdr_seq_info_present;
> + uint8_t vdr_dm_metadata_present;
> + uint8_t use_prev_vdr_rpu;
> + uint8_t use_nlq;
> + uint8_t profile;
> + if ((ret = init_get_bits8(gb, rpu, rpu_size)) < 0)
Notice that rpu_size is converted to int before init_get_bits8() is
called. (It is not problematic with the use in 6/6 because the size has
already been checked in this case.)
> + return ret;
> +
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [FFmpeg-devel] [PATCH v9 6/6] lavc/hevcdec: Parse DOVI RPU NALs
2021-12-22 15:14 ` [FFmpeg-devel] [PATCH v9 6/6] lavc/hevcdec: Parse DOVI RPU NALs Niklas Haas
2021-12-22 19:55 ` Andreas Rheinhardt
@ 2022-01-02 4:31 ` Andreas Rheinhardt
1 sibling, 0 replies; 13+ messages in thread
From: Andreas Rheinhardt @ 2022-01-02 4:31 UTC (permalink / raw)
To: ffmpeg-devel
Niklas Haas:
> From: Niklas Haas <git@haasn.dev>
>
> And expose the parsed values as frame side data. Update FATE results to
> match.
>
> It's worth documenting that this relies on the dovi configuration record
> being present on the first AVPacket fed to the decoder, which in
> practice is the case if if the API user has called something like
> av_format_inject_global_side_data, which is unfortunately not the
> default.
>
> This commit is not the time and place to change that behavior, though.
>
> Signed-off-by: Niklas Haas <git@haasn.dev>
> ---
> configure | 2 +-
> libavcodec/hevcdec.c | 63 +++++++++--
> libavcodec/hevcdec.h | 3 +
> tests/ref/fate/hevc-dv-rpu | 224 +++++++++++++++++++++++++++++++++++++
> 4 files changed, 283 insertions(+), 9 deletions(-)
>
> diff --git a/configure b/configure
> index 68658a847f..7803aa47af 100755
> --- a/configure
> +++ b/configure
> @@ -2826,7 +2826,7 @@ h264_decoder_suggest="error_resilience"
> hap_decoder_select="snappy texturedsp"
> hap_encoder_deps="libsnappy"
> hap_encoder_select="texturedspenc"
> -hevc_decoder_select="atsc_a53 bswapdsp cabac golomb hevcparse videodsp"
> +hevc_decoder_select="atsc_a53 bswapdsp cabac dovi_rpu golomb hevcparse videodsp"
> huffyuv_decoder_select="bswapdsp huffyuvdsp llviddsp"
> huffyuv_encoder_select="bswapdsp huffman huffyuvencdsp llvidencdsp"
> hymt_decoder_select="huffyuv_decoder"
> diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
> index 46d9edf8eb..298d89fea6 100644
> --- a/libavcodec/hevcdec.c
> +++ b/libavcodec/hevcdec.c
> @@ -2723,6 +2723,7 @@ error:
> static int set_side_data(HEVCContext *s)
> {
> AVFrame *out = s->ref->frame;
> + int ret;
>
> if (s->sei.frame_packing.present &&
> s->sei.frame_packing.arrangement_type >= 3 &&
> @@ -2967,6 +2968,9 @@ static int set_side_data(HEVCContext *s)
> s->rpu_buf = NULL;
> }
>
> + if ((ret = ff_dovi_attach_side_data(&s->dovi_ctx, out)) < 0)
> + return ret;
> +
> return 0;
> }
>
> @@ -3298,16 +3302,24 @@ static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
> if (s->pkt.nb_nals > 1 && s->pkt.nals[s->pkt.nb_nals - 1].type == HEVC_NAL_UNSPEC62 &&
> s->pkt.nals[s->pkt.nb_nals - 1].size > 2 && !s->pkt.nals[s->pkt.nb_nals - 1].nuh_layer_id
> && !s->pkt.nals[s->pkt.nb_nals - 1].temporal_id) {
> + H2645NAL *nal = &s->pkt.nals[s->pkt.nb_nals - 1];
> if (s->rpu_buf) {
> av_buffer_unref(&s->rpu_buf);
> av_log(s->avctx, AV_LOG_WARNING, "Multiple Dolby Vision RPUs found in one AU. Skipping previous.\n");
> }
>
> - s->rpu_buf = av_buffer_alloc(s->pkt.nals[s->pkt.nb_nals - 1].raw_size - 2);
> + s->rpu_buf = av_buffer_alloc(nal->raw_size - 2);
> if (!s->rpu_buf)
> return AVERROR(ENOMEM);
> + memcpy(s->rpu_buf->data, nal->raw_data + 2, nal->raw_size - 2);
>
> - memcpy(s->rpu_buf->data, s->pkt.nals[s->pkt.nb_nals - 1].raw_data + 2, s->pkt.nals[s->pkt.nb_nals - 1].raw_size - 2);
> + s->dovi_ctx.config = s->dovi_cfg ? (void *) s->dovi_cfg->data : NULL;
> + ret = ff_dovi_rpu_parse(&s->dovi_ctx, nal->data + 2, nal->size - 2);
> + if (ret < 0) {
> + av_buffer_unref(&s->rpu_buf);
> + av_log(s->avctx, AV_LOG_WARNING, "Error parsing DOVI NAL unit.\n");
> + /* ignore */
> + }
> }
>
> /* decode the NAL units */
> @@ -3440,8 +3452,8 @@ static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
> AVPacket *avpkt)
> {
> int ret;
> - size_t new_extradata_size;
> - uint8_t *new_extradata;
> + uint8_t *sd;
> + size_t sd_size;
> HEVCContext *s = avctx->priv_data;
>
> if (!avpkt->size) {
> @@ -3453,14 +3465,37 @@ static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
> return 0;
> }
>
> - new_extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
> - &new_extradata_size);
> - if (new_extradata && new_extradata_size > 0) {
> - ret = hevc_decode_extradata(s, new_extradata, new_extradata_size, 0);
> + sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &sd_size);
> + if (sd && sd_size > 0) {
> + ret = hevc_decode_extradata(s, sd, sd_size, 0);
> if (ret < 0)
> return ret;
> }
>
> + sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_DOVI_CONF, &sd_size);
> + if (sd && sd_size > 0) {
> + if (s->dovi_cfg) {
> + /* Reuse existing buffer */
> + if ((ret = av_buffer_make_writable(&s->dovi_cfg)) < 0)
> + return ret;
> + } else {
> + /* Allocate new buffer */
> + AVDOVIDecoderConfigurationRecord *cfg;
> + size_t cfg_size;
> + cfg = av_dovi_alloc(&cfg_size);
> + if (!cfg)
> + return AVERROR(ENOMEM);
> + s->dovi_cfg = av_buffer_create((uint8_t *) cfg, cfg_size, NULL, NULL, 0);
> + if (!s->dovi_cfg) {
> + av_free(cfg);
> + return AVERROR(ENOMEM);
> + }
> + }
> +
> + av_assert0(sd_size >= s->dovi_cfg->size);
> + memcpy(s->dovi_cfg->data, sd, s->dovi_cfg->size);
dovi_cfg is only used for exactly one thing: To read its dv_profile in
ff_dovi_rpu_parse() later. This is quite a lot of effort to get this bit
of data. Why not add a ff_dovi_parse_config() that just copies the
needed field(s) from the user-supplied AVDOVIDecoderConfigurationRecord
to the DOVIContext instead?
(Furthermore, said config is explicitly intended to survive an unref as
happens in a flush, leading to a dangling pointer in case the user
unrefs dovi_cfg in flush. (Yes, the hevc decoder ensures that config is
always set correctly before every ff_dovi_rpu_parse() call, but it is
nevertheless nowhere stated that the profile is intended to be persistent.))
> + }
> +
> s->ref = NULL;
> ret = decode_nal_units(s, avpkt->data, avpkt->size);
> if (ret < 0)
> @@ -3553,6 +3588,8 @@ static av_cold int hevc_decode_free(AVCodecContext *avctx)
>
> pic_arrays_free(s);
>
> + ff_dovi_ctx_unref(&s->dovi_ctx);
> + av_buffer_unref(&s->dovi_cfg);
> av_buffer_unref(&s->rpu_buf);
>
> av_freep(&s->md5_ctx);
> @@ -3637,6 +3674,7 @@ static av_cold int hevc_init_context(AVCodecContext *avctx)
>
> ff_bswapdsp_init(&s->bdsp);
>
> + s->dovi_ctx.logctx = avctx;
> s->context_initialized = 1;
> s->eos = 0;
>
> @@ -3745,6 +3783,14 @@ static int hevc_update_thread_context(AVCodecContext *dst,
> if (ret < 0)
> return ret;
>
> + ret = av_buffer_replace(&s->dovi_cfg, s0->dovi_cfg);
> + if (ret < 0)
> + return ret;
> +
> + ret = ff_dovi_ctx_replace(&s->dovi_ctx, &s0->dovi_ctx);
> + if (ret < 0)
> + return ret;
> +
> s->sei.frame_packing = s0->sei.frame_packing;
> s->sei.display_orientation = s0->sei.display_orientation;
> s->sei.mastering_display = s0->sei.mastering_display;
> @@ -3801,6 +3847,7 @@ static void hevc_decode_flush(AVCodecContext *avctx)
> HEVCContext *s = avctx->priv_data;
> ff_hevc_flush_dpb(s);
> ff_hevc_reset_sei(&s->sei);
> + ff_dovi_ctx_unref(&s->dovi_ctx);
> av_buffer_unref(&s->rpu_buf);
> s->max_ra = INT_MAX;
> s->eos = 1;
> diff --git a/libavcodec/hevcdec.h b/libavcodec/hevcdec.h
> index 870ff178d4..c8dde6fd17 100644
> --- a/libavcodec/hevcdec.h
> +++ b/libavcodec/hevcdec.h
> @@ -32,6 +32,7 @@
> #include "avcodec.h"
> #include "bswapdsp.h"
> #include "cabac.h"
> +#include "dovi_rpu.h"
> #include "get_bits.h"
> #include "hevcpred.h"
> #include "h2645_parse.h"
> @@ -574,6 +575,8 @@ typedef struct HEVCContext {
> int nuh_layer_id;
>
> AVBufferRef *rpu_buf; ///< 0 or 1 Dolby Vision RPUs.
> + AVBufferRef *dovi_cfg; ///< contains AVDOVIDecoderConfigurationRecord
> + DOVIContext dovi_ctx; ///< Dolby Vision decoding context
> } HEVCContext;
>
> /**
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [FFmpeg-devel] [PATCH v9 4/6] lavc: Implement Dolby Vision RPU parsing
2022-01-02 4:18 ` Andreas Rheinhardt
@ 2022-01-02 12:45 ` James Almer
2022-01-02 18:55 ` Hendrik Leppkes
2022-01-02 19:01 ` Hendrik Leppkes
1 sibling, 1 reply; 13+ messages in thread
From: James Almer @ 2022-01-02 12:45 UTC (permalink / raw)
To: ffmpeg-devel
On 1/2/2022 1:18 AM, Andreas Rheinhardt wrote:
> Niklas Haas:
>> From: Niklas Haas <git@haasn.dev>
>>
>> Based on a mixture of guesswork, partial documentation in patents, and
>> reverse engineering of real-world samples. Confirmed working for all the
>> samples I've thrown at it.
>>
>> Contains some annoying machinery to persist these values in between
>> frames, which is needed in theory even though I've never actually seen a
>> sample that relies on it in practice. May or may not work.
>>
>> Since the distinction matters greatly for parsing the color matrix
>> values, this includes a small helper function to guess the right profile
>> from the RPU itself in case the user has forgotten to forward the dovi
>> configuration record to the decoder. (Which in practice, only ffmpeg.c
>> and ffplay do..)
>>
>> Notable omissions / deviations:
>> - CRC32 verification. This is based on the MPEG2 CRC32 type, which does
>> not seem to be implemented in lavu. (And I don't care enough to do so)
>> - Linear interpolation support. Nothing documents this (beyond its
>> existence) and no samples use it, so impossible to implement.
>> - All of the extension metadata blocks, but these contain values that
>> seem largely congruent with ST2094, HDR10, or other existing forms of
>> side data, so I will defer parsing/attaching them to a future commit.
>> - The patent describes a mechanism for predicting coefficients from
>> previous RPUs, but the bit for the flag whether to use the
>> prediction deltas or signal entirely new coefficients does not seem to
>> be present in actual RPUs, so we ignore this subsystem entirely.
>> - In the patent's spec, the NLQ subsystem also loops over
>> num_nlq_pivots, but even in the patent the number is hard-coded to one
>> iteration rather than signalled. So we only store one set of coefs.
>>
>> Heavily influenced by https://github.com/quietvoid/dovi_tool
>> Documentation drawn from US Patent 10,701,399 B2 and ETSI GS CCM 001
>>
>> Signed-off-by: Niklas Haas <git@haasn.dev>
>> ---
>> configure | 2 +
>> libavcodec/Makefile | 1 +
>> libavcodec/dovi_rpu.c | 430 ++++++++++++++++++++++++++++++++++++++++++
>> libavcodec/dovi_rpu.h | 71 +++++++
>> 4 files changed, 504 insertions(+)
>> create mode 100644 libavcodec/dovi_rpu.c
>> create mode 100644 libavcodec/dovi_rpu.h
>>
>> diff --git a/configure b/configure
>> index 0ccd3bda11..68658a847f 100755
>> --- a/configure
>> +++ b/configure
>> @@ -2434,6 +2434,7 @@ CONFIG_EXTRA="
>> cbs_vp9
>> dirac_parse
>> dnn
>> + dovi_rpu
>> dvprofile
>> exif
>> faandct
>> @@ -2706,6 +2707,7 @@ cbs_mpeg2_select="cbs"
>> cbs_vp9_select="cbs"
>> dct_select="rdft"
>> dirac_parse_select="golomb"
>> +dovi_rpu_select="golomb"
>> dnn_suggest="libtensorflow libopenvino"
>> dnn_deps="avformat swscale"
>> error_resilience_select="me_cmp"
>> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
>> index fb90ecea84..7364c7a91f 100644
>> --- a/libavcodec/Makefile
>> +++ b/libavcodec/Makefile
>> @@ -77,6 +77,7 @@ OBJS-$(CONFIG_CBS_MPEG2) += cbs_mpeg2.o
>> OBJS-$(CONFIG_CBS_VP9) += cbs_vp9.o
>> OBJS-$(CONFIG_CRYSTALHD) += crystalhd.o
>> OBJS-$(CONFIG_DCT) += dct.o dct32_fixed.o dct32_float.o
>> +OBJS-$(CONFIG_DOVI_RPU) += dovi_rpu.o
>> OBJS-$(CONFIG_ERROR_RESILIENCE) += error_resilience.o
>> OBJS-$(CONFIG_EXIF) += exif.o tiff_common.o
>> OBJS-$(CONFIG_FAANDCT) += faandct.o
>> diff --git a/libavcodec/dovi_rpu.c b/libavcodec/dovi_rpu.c
>> new file mode 100644
>> index 0000000000..fc2e1fb2a1
>> --- /dev/null
>> +++ b/libavcodec/dovi_rpu.c
>> @@ -0,0 +1,430 @@
>> +/*
>> + * Dolby Vision RPU decoder
>> + *
>> + * Copyright (C) 2021 Jan Ekström
>> + * Copyright (C) 2021 Niklas Haas
>> + *
>> + * 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/buffer.h"
>> +
>> +#include "dovi_rpu.h"
>> +#include "golomb.h"
>> +#include "get_bits.h"
>> +
>> +enum {
>> + RPU_COEFF_FIXED = 0,
>> + RPU_COEFF_FLOAT = 1,
>> +};
>> +
>> +/**
>> + * Private contents of vdr_ref.
>> + */
>> +typedef struct DOVIVdrRef {
>> + AVDOVIDataMapping mapping;
>> + AVDOVIColorMetadata color;
>> +} DOVIVdrRef;
>> +
>> +void ff_dovi_ctx_unref(DOVIContext *s)
>> +{
>> + for (int i = 0; i < FF_ARRAY_ELEMS(s->vdr_ref); i++)
>> + av_buffer_unref(&s->vdr_ref[i]);
>> +
>> + /* Preserve the user-provided fields explicitly, reset everything else */
>> + *s = (DOVIContext) {
>> + .logctx = s->logctx,
>> + .config = s->config,
>> + };
>> +}
>> +
>> +int ff_dovi_ctx_replace(DOVIContext *s, const DOVIContext *s0)
>> +{
>> + int ret;
>> + s->logctx = s0->logctx;
>> + s->config = s0->config;
>> + s->mapping = s0->mapping;
>> + s->color = s0->color;
>> + for (int i = 0; i < DOVI_MAX_DM_ID; i++) {
>> + if ((ret = av_buffer_replace(&s->vdr_ref[i], s0->vdr_ref[i])) < 0)
>> + goto fail;
>> + }
>> +
>> + return 0;
>> +
>> +fail:
>> + ff_dovi_ctx_unref(s);
>> + return ret;
>> +}
>> +
>> +int ff_dovi_attach_side_data(DOVIContext *s, AVFrame *frame)
>> +{
>> + AVFrameSideData *sd;
>> + AVBufferRef *buf;
>> + AVDOVIMetadata *dovi;
>> + size_t dovi_size;
>> +
>> + if (!s->mapping || !s->color)
>> + return 0; /* incomplete dovi metadata */
>> +
>> + dovi = av_dovi_metadata_alloc(&dovi_size);
>> + if (!dovi)
>> + return AVERROR(ENOMEM);
>> +
>> + buf = av_buffer_create((uint8_t *) dovi, dovi_size, NULL, NULL, 0);
>> + if (!buf) {
>> + av_free(dovi);
>> + return AVERROR(ENOMEM);
>> + }
>> +
>> + sd = av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_DOVI_METADATA, buf);
>> + if (!sd) {
>> + av_buffer_unref(&buf);
>> + return AVERROR(ENOMEM);
>> + }
>> +
>> + memcpy(av_dovi_get_header(dovi), &s->header, sizeof(s->header));
>> + memcpy(av_dovi_get_mapping(dovi), s->mapping, sizeof(*s->mapping));
>> + memcpy(av_dovi_get_color(dovi), s->color, sizeof(*s->color));
>
> These are potentially problematic due to trailing padding in the
> structures. This trailing padding might become valid fields in newer
> versions of this structure. E.g. av_dovi_metadata_alloc() might
> intentionally set the default values of these new fields to invalid
> values. Yet lavc (with the old structure) sees them as padding, allowing
> the compiler to trash these values on every write to these structures.
> More likely though is that they will never be touched at all and
> therefore retain their default (zero) initialization. Which might
> conflict with the default value of these fields.
> This can be fixed by only copying everything up to and including the
> last field that we know of for every of these structures. This will
> unfortunately increase the maintainence burden a bit.
I mean, both sizeof(AVDOVIRpuDataHeader) and sizeof(AVDOVIDataMapping)
are defined as not being part of the ABI (but
sizeof(AVDOVIColorMetadata) is? Is that intended?), so their usage in
this patch is wrong to begin with.
>
>> + return 0;
>> +}
>> +
>> +static int guess_profile(const AVDOVIRpuDataHeader *hdr)
>> +{
>> + switch (hdr->vdr_rpu_profile) {
>> + case 0:
>> + if (hdr->bl_video_full_range_flag)
>> + return 5;
>> + break;
>> + case 1:
>> + if (hdr->el_spatial_resampling_filter_flag && !hdr->disable_residual_flag) {
>> + if (hdr->vdr_bit_depth == 12) {
>> + return 7;
>> + } else {
>> + return 4;
>> + }
>> + } else {
>> + return 8;
>> + }
>> + }
>> +
>> + return 0; /* unknown */
>> +}
>> +
>> +static inline uint64_t get_ue_coef(GetBitContext *gb, const AVDOVIRpuDataHeader *hdr)
>> +{
>> + uint64_t ipart;
>> + union { uint32_t u32; float f32; } fpart;
>> +
>> + switch (hdr->coef_data_type) {
>> + case RPU_COEFF_FIXED:
>> + ipart = get_ue_golomb_long(gb);
>> + fpart.u32 = get_bits_long(gb, hdr->coef_log2_denom);
>> + return (ipart << hdr->coef_log2_denom) + fpart.u32;
>
> Here and in get_se_coef below the f32 member of the union is not used
> with RPU_COEFF_FIXED, so using the union seems unnecessary. It is also
> slightly confusing, because upon seeing this and seeing that the union
> is used with RPU_COEFF_FIXED, I expect it to be used for type-punning.
>
>> +
>> + case RPU_COEFF_FLOAT:
>> + fpart.u32 = get_bits_long(gb, 32);
>> + return fpart.f32 * (1 << hdr->coef_log2_denom);
>
> You could just use av_int2float() here instead of adding the union yourself.
>
>> + }
>> +
>> + return 0; /* unreachable */
>> +}
>> +
>> +static inline int64_t get_se_coef(GetBitContext *gb, const AVDOVIRpuDataHeader *hdr)
>> +{
>> + int64_t ipart;
>> + union { uint32_t u32; float f32; } fpart;
>> +
>> + switch (hdr->coef_data_type) {
>> + case RPU_COEFF_FIXED:
>> + ipart = get_se_golomb_long(gb);
>> + fpart.u32 = get_bits_long(gb, hdr->coef_log2_denom);
>> + return (ipart << hdr->coef_log2_denom) + fpart.u32;
>> +
>> + case RPU_COEFF_FLOAT:
>> + fpart.u32 = get_bits_long(gb, 32);
>> + return fpart.f32 * (1 << hdr->coef_log2_denom);
>> + }
>> +
>> + return 0; /* unreachable */
>> +}
>> +
>> +#define VALIDATE(VAR, MIN, MAX) \
>> + do { \
>> + if (VAR < MIN || VAR > MAX) { \
>> + av_log(s->logctx, AV_LOG_ERROR, "RPU validation failed: " \
>> + #MIN" <= "#VAR" = %d <= "#MAX"\n", (int) VAR); \
>> + goto fail; \
>> + } \
>> + } while (0)
>> +
>> +int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size)
>> +{
>> + AVDOVIRpuDataHeader *hdr = &s->header;
>> + GetBitContext *gb = &(GetBitContext){0};
>> + DOVIVdrRef *vdr;
>> + int ret;
>> +
>> + uint8_t nal_prefix;
>> + uint8_t rpu_type;
>> + uint8_t vdr_seq_info_present;
>> + uint8_t vdr_dm_metadata_present;
>> + uint8_t use_prev_vdr_rpu;
>> + uint8_t use_nlq;
>> + uint8_t profile;
>> + if ((ret = init_get_bits8(gb, rpu, rpu_size)) < 0)
>
> Notice that rpu_size is converted to int before init_get_bits8() is
> called. (It is not problematic with the use in 6/6 because the size has
> already been checked in this case.)
>
>> + return ret;
>> +
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [FFmpeg-devel] [PATCH v9 4/6] lavc: Implement Dolby Vision RPU parsing
2022-01-02 12:45 ` James Almer
@ 2022-01-02 18:55 ` Hendrik Leppkes
0 siblings, 0 replies; 13+ messages in thread
From: Hendrik Leppkes @ 2022-01-02 18:55 UTC (permalink / raw)
To: FFmpeg development discussions and patches
James Almer <jamrial@gmail.com> schrieb am So., 2. Jan. 2022, 13:46:
> On 1/2/2022 1:18 AM, Andreas Rheinhardt wrote:
> > Niklas Haas:
> >> From: Niklas Haas <git@haasn.dev>
> >>
> >> Based on a mixture of guesswork, partial documentation in patents, and
> >> reverse engineering of real-world samples. Confirmed working for all the
> >> samples I've thrown at it.
> >>
> >> Contains some annoying machinery to persist these values in between
> >> frames, which is needed in theory even though I've never actually seen a
> >> sample that relies on it in practice. May or may not work.
> >>
> >> Since the distinction matters greatly for parsing the color matrix
> >> values, this includes a small helper function to guess the right profile
> >> from the RPU itself in case the user has forgotten to forward the dovi
> >> configuration record to the decoder. (Which in practice, only ffmpeg.c
> >> and ffplay do..)
> >>
> >> Notable omissions / deviations:
> >> - CRC32 verification. This is based on the MPEG2 CRC32 type, which does
> >> not seem to be implemented in lavu. (And I don't care enough to do
> so)
> >> - Linear interpolation support. Nothing documents this (beyond its
> >> existence) and no samples use it, so impossible to implement.
> >> - All of the extension metadata blocks, but these contain values that
> >> seem largely congruent with ST2094, HDR10, or other existing forms of
> >> side data, so I will defer parsing/attaching them to a future commit.
> >> - The patent describes a mechanism for predicting coefficients from
> >> previous RPUs, but the bit for the flag whether to use the
> >> prediction deltas or signal entirely new coefficients does not seem
> to
> >> be present in actual RPUs, so we ignore this subsystem entirely.
> >> - In the patent's spec, the NLQ subsystem also loops over
> >> num_nlq_pivots, but even in the patent the number is hard-coded to
> one
> >> iteration rather than signalled. So we only store one set of coefs.
> >>
> >> Heavily influenced by https://github.com/quietvoid/dovi_tool
> >> Documentation drawn from US Patent 10,701,399 B2 and ETSI GS CCM 001
> >>
> >> Signed-off-by: Niklas Haas <git@haasn.dev>
> >> ---
> >> configure | 2 +
> >> libavcodec/Makefile | 1 +
> >> libavcodec/dovi_rpu.c | 430 ++++++++++++++++++++++++++++++++++++++++++
> >> libavcodec/dovi_rpu.h | 71 +++++++
> >> 4 files changed, 504 insertions(+)
> >> create mode 100644 libavcodec/dovi_rpu.c
> >> create mode 100644 libavcodec/dovi_rpu.h
> >>
> >> diff --git a/configure b/configure
> >> index 0ccd3bda11..68658a847f 100755
> >> --- a/configure
> >> +++ b/configure
> >> @@ -2434,6 +2434,7 @@ CONFIG_EXTRA="
> >> cbs_vp9
> >> dirac_parse
> >> dnn
> >> + dovi_rpu
> >> dvprofile
> >> exif
> >> faandct
> >> @@ -2706,6 +2707,7 @@ cbs_mpeg2_select="cbs"
> >> cbs_vp9_select="cbs"
> >> dct_select="rdft"
> >> dirac_parse_select="golomb"
> >> +dovi_rpu_select="golomb"
> >> dnn_suggest="libtensorflow libopenvino"
> >> dnn_deps="avformat swscale"
> >> error_resilience_select="me_cmp"
> >> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> >> index fb90ecea84..7364c7a91f 100644
> >> --- a/libavcodec/Makefile
> >> +++ b/libavcodec/Makefile
> >> @@ -77,6 +77,7 @@ OBJS-$(CONFIG_CBS_MPEG2) += cbs_mpeg2.o
> >> OBJS-$(CONFIG_CBS_VP9) += cbs_vp9.o
> >> OBJS-$(CONFIG_CRYSTALHD) += crystalhd.o
> >> OBJS-$(CONFIG_DCT) += dct.o dct32_fixed.o
> dct32_float.o
> >> +OBJS-$(CONFIG_DOVI_RPU) += dovi_rpu.o
> >> OBJS-$(CONFIG_ERROR_RESILIENCE) += error_resilience.o
> >> OBJS-$(CONFIG_EXIF) += exif.o tiff_common.o
> >> OBJS-$(CONFIG_FAANDCT) += faandct.o
> >> diff --git a/libavcodec/dovi_rpu.c b/libavcodec/dovi_rpu.c
> >> new file mode 100644
> >> index 0000000000..fc2e1fb2a1
> >> --- /dev/null
> >> +++ b/libavcodec/dovi_rpu.c
> >> @@ -0,0 +1,430 @@
> >> +/*
> >> + * Dolby Vision RPU decoder
> >> + *
> >> + * Copyright (C) 2021 Jan Ekström
> >> + * Copyright (C) 2021 Niklas Haas
> >> + *
> >> + * 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/buffer.h"
> >> +
> >> +#include "dovi_rpu.h"
> >> +#include "golomb.h"
> >> +#include "get_bits.h"
> >> +
> >> +enum {
> >> + RPU_COEFF_FIXED = 0,
> >> + RPU_COEFF_FLOAT = 1,
> >> +};
> >> +
> >> +/**
> >> + * Private contents of vdr_ref.
> >> + */
> >> +typedef struct DOVIVdrRef {
> >> + AVDOVIDataMapping mapping;
> >> + AVDOVIColorMetadata color;
> >> +} DOVIVdrRef;
> >> +
> >> +void ff_dovi_ctx_unref(DOVIContext *s)
> >> +{
> >> + for (int i = 0; i < FF_ARRAY_ELEMS(s->vdr_ref); i++)
> >> + av_buffer_unref(&s->vdr_ref[i]);
> >> +
> >> + /* Preserve the user-provided fields explicitly, reset everything
> else */
> >> + *s = (DOVIContext) {
> >> + .logctx = s->logctx,
> >> + .config = s->config,
> >> + };
> >> +}
> >> +
> >> +int ff_dovi_ctx_replace(DOVIContext *s, const DOVIContext *s0)
> >> +{
> >> + int ret;
> >> + s->logctx = s0->logctx;
> >> + s->config = s0->config;
> >> + s->mapping = s0->mapping;
> >> + s->color = s0->color;
> >> + for (int i = 0; i < DOVI_MAX_DM_ID; i++) {
> >> + if ((ret = av_buffer_replace(&s->vdr_ref[i], s0->vdr_ref[i]))
> < 0)
> >> + goto fail;
> >> + }
> >> +
> >> + return 0;
> >> +
> >> +fail:
> >> + ff_dovi_ctx_unref(s);
> >> + return ret;
> >> +}
> >> +
> >> +int ff_dovi_attach_side_data(DOVIContext *s, AVFrame *frame)
> >> +{
> >> + AVFrameSideData *sd;
> >> + AVBufferRef *buf;
> >> + AVDOVIMetadata *dovi;
> >> + size_t dovi_size;
> >> +
> >> + if (!s->mapping || !s->color)
> >> + return 0; /* incomplete dovi metadata */
> >> +
> >> + dovi = av_dovi_metadata_alloc(&dovi_size);
> >> + if (!dovi)
> >> + return AVERROR(ENOMEM);
> >> +
> >> + buf = av_buffer_create((uint8_t *) dovi, dovi_size, NULL, NULL, 0);
> >> + if (!buf) {
> >> + av_free(dovi);
> >> + return AVERROR(ENOMEM);
> >> + }
> >> +
> >> + sd = av_frame_new_side_data_from_buf(frame,
> AV_FRAME_DATA_DOVI_METADATA, buf);
> >> + if (!sd) {
> >> + av_buffer_unref(&buf);
> >> + return AVERROR(ENOMEM);
> >> + }
> >> +
> >> + memcpy(av_dovi_get_header(dovi), &s->header, sizeof(s->header));
> >> + memcpy(av_dovi_get_mapping(dovi), s->mapping, sizeof(*s->mapping));
> >> + memcpy(av_dovi_get_color(dovi), s->color, sizeof(*s->color));
> >
> > These are potentially problematic due to trailing padding in the
> > structures. This trailing padding might become valid fields in newer
> > versions of this structure. E.g. av_dovi_metadata_alloc() might
> > intentionally set the default values of these new fields to invalid
> > values. Yet lavc (with the old structure) sees them as padding, allowing
> > the compiler to trash these values on every write to these structures.
> > More likely though is that they will never be touched at all and
> > therefore retain their default (zero) initialization. Which might
> > conflict with the default value of these fields.
> > This can be fixed by only copying everything up to and including the
> > last field that we know of for every of these structures. This will
> > unfortunately increase the maintainence burden a bit.
> I mean, both sizeof(AVDOVIRpuDataHeader) and sizeof(AVDOVIDataMapping)
> are defined as not being part of the ABI (but
> sizeof(AVDOVIColorMetadata) is? Is that intended?), so their usage in
> this patch is wrong to begin with.
>
This is not actually a problem since it doesn't rely on a stable ABI for
the size, but only knowing how big the struct was it was built with, and
filled itself. If the struct grows in size in lavu then the remainder will
just be left alone, since every struct is a distinct pointer into the
overall buffer.
> >
> >> + return 0;
> >> +}
> >> +
> >> +static int guess_profile(const AVDOVIRpuDataHeader *hdr)
> >> +{
> >> + switch (hdr->vdr_rpu_profile) {
> >> + case 0:
> >> + if (hdr->bl_video_full_range_flag)
> >> + return 5;
> >> + break;
> >> + case 1:
> >> + if (hdr->el_spatial_resampling_filter_flag &&
> !hdr->disable_residual_flag) {
> >> + if (hdr->vdr_bit_depth == 12) {
> >> + return 7;
> >> + } else {
> >> + return 4;
> >> + }
> >> + } else {
> >> + return 8;
> >> + }
> >> + }
> >> +
> >> + return 0; /* unknown */
> >> +}
> >> +
> >> +static inline uint64_t get_ue_coef(GetBitContext *gb, const
> AVDOVIRpuDataHeader *hdr)
> >> +{
> >> + uint64_t ipart;
> >> + union { uint32_t u32; float f32; } fpart;
> >> +
> >> + switch (hdr->coef_data_type) {
> >> + case RPU_COEFF_FIXED:
> >> + ipart = get_ue_golomb_long(gb);
> >> + fpart.u32 = get_bits_long(gb, hdr->coef_log2_denom);
> >> + return (ipart << hdr->coef_log2_denom) + fpart.u32;
> >
> > Here and in get_se_coef below the f32 member of the union is not used
> > with RPU_COEFF_FIXED, so using the union seems unnecessary. It is also
> > slightly confusing, because upon seeing this and seeing that the union
> > is used with RPU_COEFF_FIXED, I expect it to be used for type-punning.
> >
> >> +
> >> + case RPU_COEFF_FLOAT:
> >> + fpart.u32 = get_bits_long(gb, 32);
> >> + return fpart.f32 * (1 << hdr->coef_log2_denom);
> >
> > You could just use av_int2float() here instead of adding the union
> yourself.
> >
> >> + }
> >> +
> >> + return 0; /* unreachable */
> >> +}
> >> +
> >> +static inline int64_t get_se_coef(GetBitContext *gb, const
> AVDOVIRpuDataHeader *hdr)
> >> +{
> >> + int64_t ipart;
> >> + union { uint32_t u32; float f32; } fpart;
> >> +
> >> + switch (hdr->coef_data_type) {
> >> + case RPU_COEFF_FIXED:
> >> + ipart = get_se_golomb_long(gb);
> >> + fpart.u32 = get_bits_long(gb, hdr->coef_log2_denom);
> >> + return (ipart << hdr->coef_log2_denom) + fpart.u32;
> >> +
> >> + case RPU_COEFF_FLOAT:
> >> + fpart.u32 = get_bits_long(gb, 32);
> >> + return fpart.f32 * (1 << hdr->coef_log2_denom);
> >> + }
> >> +
> >> + return 0; /* unreachable */
> >> +}
> >> +
> >> +#define VALIDATE(VAR, MIN, MAX)
> \
> >> + do {
> \
> >> + if (VAR < MIN || VAR > MAX) {
> \
> >> + av_log(s->logctx, AV_LOG_ERROR, "RPU validation failed: "
> \
> >> + #MIN" <= "#VAR" = %d <= "#MAX"\n", (int) VAR);
> \
> >> + goto fail;
> \
> >> + }
> \
> >> + } while (0)
> >> +
> >> +int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t
> rpu_size)
> >> +{
> >> + AVDOVIRpuDataHeader *hdr = &s->header;
> >> + GetBitContext *gb = &(GetBitContext){0};
> >> + DOVIVdrRef *vdr;
> >> + int ret;
> >> +
> >> + uint8_t nal_prefix;
> >> + uint8_t rpu_type;
> >> + uint8_t vdr_seq_info_present;
> >> + uint8_t vdr_dm_metadata_present;
> >> + uint8_t use_prev_vdr_rpu;
> >> + uint8_t use_nlq;
> >> + uint8_t profile;
> >> + if ((ret = init_get_bits8(gb, rpu, rpu_size)) < 0)
> >
> > Notice that rpu_size is converted to int before init_get_bits8() is
> > called. (It is not problematic with the use in 6/6 because the size has
> > already been checked in this case.)
> >
> >> + return ret;
> >> +
> > _______________________________________________
> > ffmpeg-devel mailing list
> > ffmpeg-devel@ffmpeg.org
> > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >
> > To unsubscribe, visit link above, or email
> > ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
>
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [FFmpeg-devel] [PATCH v9 4/6] lavc: Implement Dolby Vision RPU parsing
2022-01-02 4:18 ` Andreas Rheinhardt
2022-01-02 12:45 ` James Almer
@ 2022-01-02 19:01 ` Hendrik Leppkes
2022-01-03 5:42 ` Andreas Rheinhardt
1 sibling, 1 reply; 13+ messages in thread
From: Hendrik Leppkes @ 2022-01-02 19:01 UTC (permalink / raw)
To: FFmpeg development discussions and patches
Andreas Rheinhardt <andreas.rheinhardt@outlook.com> schrieb am So., 2. Jan.
2022, 05:18:
> Niklas Haas:
> > From: Niklas Haas <git@haasn.dev>
> >
> > Based on a mixture of guesswork, partial documentation in patents, and
> > reverse engineering of real-world samples. Confirmed working for all the
> > samples I've thrown at it.
> >
> > Contains some annoying machinery to persist these values in between
> > frames, which is needed in theory even though I've never actually seen a
> > sample that relies on it in practice. May or may not work.
> >
> > Since the distinction matters greatly for parsing the color matrix
> > values, this includes a small helper function to guess the right profile
> > from the RPU itself in case the user has forgotten to forward the dovi
> > configuration record to the decoder. (Which in practice, only ffmpeg.c
> > and ffplay do..)
> >
> > Notable omissions / deviations:
> > - CRC32 verification. This is based on the MPEG2 CRC32 type, which does
> > not seem to be implemented in lavu. (And I don't care enough to do so)
> > - Linear interpolation support. Nothing documents this (beyond its
> > existence) and no samples use it, so impossible to implement.
> > - All of the extension metadata blocks, but these contain values that
> > seem largely congruent with ST2094, HDR10, or other existing forms of
> > side data, so I will defer parsing/attaching them to a future commit.
> > - The patent describes a mechanism for predicting coefficients from
> > previous RPUs, but the bit for the flag whether to use the
> > prediction deltas or signal entirely new coefficients does not seem to
> > be present in actual RPUs, so we ignore this subsystem entirely.
> > - In the patent's spec, the NLQ subsystem also loops over
> > num_nlq_pivots, but even in the patent the number is hard-coded to one
> > iteration rather than signalled. So we only store one set of coefs.
> >
> > Heavily influenced by https://github.com/quietvoid/dovi_tool
> > Documentation drawn from US Patent 10,701,399 B2 and ETSI GS CCM 001
> >
> > Signed-off-by: Niklas Haas <git@haasn.dev>
> > ---
> > configure | 2 +
> > libavcodec/Makefile | 1 +
> > libavcodec/dovi_rpu.c | 430 ++++++++++++++++++++++++++++++++++++++++++
> > libavcodec/dovi_rpu.h | 71 +++++++
> > 4 files changed, 504 insertions(+)
> > create mode 100644 libavcodec/dovi_rpu.c
> > create mode 100644 libavcodec/dovi_rpu.h
> >
> > diff --git a/configure b/configure
> > index 0ccd3bda11..68658a847f 100755
> > --- a/configure
> > +++ b/configure
> > @@ -2434,6 +2434,7 @@ CONFIG_EXTRA="
> > cbs_vp9
> > dirac_parse
> > dnn
> > + dovi_rpu
> > dvprofile
> > exif
> > faandct
> > @@ -2706,6 +2707,7 @@ cbs_mpeg2_select="cbs"
> > cbs_vp9_select="cbs"
> > dct_select="rdft"
> > dirac_parse_select="golomb"
> > +dovi_rpu_select="golomb"
> > dnn_suggest="libtensorflow libopenvino"
> > dnn_deps="avformat swscale"
> > error_resilience_select="me_cmp"
> > diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> > index fb90ecea84..7364c7a91f 100644
> > --- a/libavcodec/Makefile
> > +++ b/libavcodec/Makefile
> > @@ -77,6 +77,7 @@ OBJS-$(CONFIG_CBS_MPEG2) += cbs_mpeg2.o
> > OBJS-$(CONFIG_CBS_VP9) += cbs_vp9.o
> > OBJS-$(CONFIG_CRYSTALHD) += crystalhd.o
> > OBJS-$(CONFIG_DCT) += dct.o dct32_fixed.o
> dct32_float.o
> > +OBJS-$(CONFIG_DOVI_RPU) += dovi_rpu.o
> > OBJS-$(CONFIG_ERROR_RESILIENCE) += error_resilience.o
> > OBJS-$(CONFIG_EXIF) += exif.o tiff_common.o
> > OBJS-$(CONFIG_FAANDCT) += faandct.o
> > diff --git a/libavcodec/dovi_rpu.c b/libavcodec/dovi_rpu.c
> > new file mode 100644
> > index 0000000000..fc2e1fb2a1
> > --- /dev/null
> > +++ b/libavcodec/dovi_rpu.c
> > @@ -0,0 +1,430 @@
> > +/*
> > + * Dolby Vision RPU decoder
> > + *
> > + * Copyright (C) 2021 Jan Ekström
> > + * Copyright (C) 2021 Niklas Haas
> > + *
> > + * 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/buffer.h"
> > +
> > +#include "dovi_rpu.h"
> > +#include "golomb.h"
> > +#include "get_bits.h"
> > +
> > +enum {
> > + RPU_COEFF_FIXED = 0,
> > + RPU_COEFF_FLOAT = 1,
> > +};
> > +
> > +/**
> > + * Private contents of vdr_ref.
> > + */
> > +typedef struct DOVIVdrRef {
> > + AVDOVIDataMapping mapping;
> > + AVDOVIColorMetadata color;
> > +} DOVIVdrRef;
> > +
> > +void ff_dovi_ctx_unref(DOVIContext *s)
> > +{
> > + for (int i = 0; i < FF_ARRAY_ELEMS(s->vdr_ref); i++)
> > + av_buffer_unref(&s->vdr_ref[i]);
> > +
> > + /* Preserve the user-provided fields explicitly, reset everything
> else */
> > + *s = (DOVIContext) {
> > + .logctx = s->logctx,
> > + .config = s->config,
> > + };
> > +}
> > +
> > +int ff_dovi_ctx_replace(DOVIContext *s, const DOVIContext *s0)
> > +{
> > + int ret;
> > + s->logctx = s0->logctx;
> > + s->config = s0->config;
> > + s->mapping = s0->mapping;
> > + s->color = s0->color;
> > + for (int i = 0; i < DOVI_MAX_DM_ID; i++) {
> > + if ((ret = av_buffer_replace(&s->vdr_ref[i], s0->vdr_ref[i])) <
> 0)
> > + goto fail;
> > + }
> > +
> > + return 0;
> > +
> > +fail:
> > + ff_dovi_ctx_unref(s);
> > + return ret;
> > +}
> > +
> > +int ff_dovi_attach_side_data(DOVIContext *s, AVFrame *frame)
> > +{
> > + AVFrameSideData *sd;
> > + AVBufferRef *buf;
> > + AVDOVIMetadata *dovi;
> > + size_t dovi_size;
> > +
> > + if (!s->mapping || !s->color)
> > + return 0; /* incomplete dovi metadata */
> > +
> > + dovi = av_dovi_metadata_alloc(&dovi_size);
> > + if (!dovi)
> > + return AVERROR(ENOMEM);
> > +
> > + buf = av_buffer_create((uint8_t *) dovi, dovi_size, NULL, NULL, 0);
> > + if (!buf) {
> > + av_free(dovi);
> > + return AVERROR(ENOMEM);
> > + }
> > +
> > + sd = av_frame_new_side_data_from_buf(frame,
> AV_FRAME_DATA_DOVI_METADATA, buf);
> > + if (!sd) {
> > + av_buffer_unref(&buf);
> > + return AVERROR(ENOMEM);
> > + }
> > +
> > + memcpy(av_dovi_get_header(dovi), &s->header, sizeof(s->header));
> > + memcpy(av_dovi_get_mapping(dovi), s->mapping, sizeof(*s->mapping));
> > + memcpy(av_dovi_get_color(dovi), s->color, sizeof(*s->color));
>
> These are potentially problematic due to trailing padding in the
> structures. This trailing padding might become valid fields in newer
> versions of this structure. E.g. av_dovi_metadata_alloc() might
> intentionally set the default values of these new fields to invalid
> values. Yet lavc (with the old structure) sees them as padding, allowing
> the compiler to trash these values on every write to these structures.
> More likely though is that they will never be touched at all and
> therefore retain their default (zero) initialization. Which might
> conflict with the default value of these fields.
> This can be fixed by only copying everything up to and including the
> last field that we know of for every of these structures. This will
> unfortunately increase the maintainence burden a bit.
>
This code is the primary source of any data in this struct. If it ever gets
extended, this parser wanting to store more fields would be why, so
initialization doesn't seem overly worrysome.
> > + return 0;
> > +}
> > +
> > +static int guess_profile(const AVDOVIRpuDataHeader *hdr)
> > +{
> > + switch (hdr->vdr_rpu_profile) {
> > + case 0:
> > + if (hdr->bl_video_full_range_flag)
> > + return 5;
> > + break;
> > + case 1:
> > + if (hdr->el_spatial_resampling_filter_flag &&
> !hdr->disable_residual_flag) {
> > + if (hdr->vdr_bit_depth == 12) {
> > + return 7;
> > + } else {
> > + return 4;
> > + }
> > + } else {
> > + return 8;
> > + }
> > + }
> > +
> > + return 0; /* unknown */
> > +}
> > +
> > +static inline uint64_t get_ue_coef(GetBitContext *gb, const
> AVDOVIRpuDataHeader *hdr)
> > +{
> > + uint64_t ipart;
> > + union { uint32_t u32; float f32; } fpart;
> > +
> > + switch (hdr->coef_data_type) {
> > + case RPU_COEFF_FIXED:
> > + ipart = get_ue_golomb_long(gb);
> > + fpart.u32 = get_bits_long(gb, hdr->coef_log2_denom);
> > + return (ipart << hdr->coef_log2_denom) + fpart.u32;
>
> Here and in get_se_coef below the f32 member of the union is not used
> with RPU_COEFF_FIXED, so using the union seems unnecessary. It is also
> slightly confusing, because upon seeing this and seeing that the union
> is used with RPU_COEFF_FIXED, I expect it to be used for type-punning.
>
> > +
> > + case RPU_COEFF_FLOAT:
> > + fpart.u32 = get_bits_long(gb, 32);
> > + return fpart.f32 * (1 << hdr->coef_log2_denom);
>
> You could just use av_int2float() here instead of adding the union
> yourself.
>
> > + }
> > +
> > + return 0; /* unreachable */
> > +}
> > +
> > +static inline int64_t get_se_coef(GetBitContext *gb, const
> AVDOVIRpuDataHeader *hdr)
> > +{
> > + int64_t ipart;
> > + union { uint32_t u32; float f32; } fpart;
> > +
> > + switch (hdr->coef_data_type) {
> > + case RPU_COEFF_FIXED:
> > + ipart = get_se_golomb_long(gb);
> > + fpart.u32 = get_bits_long(gb, hdr->coef_log2_denom);
> > + return (ipart << hdr->coef_log2_denom) + fpart.u32;
> > +
> > + case RPU_COEFF_FLOAT:
> > + fpart.u32 = get_bits_long(gb, 32);
> > + return fpart.f32 * (1 << hdr->coef_log2_denom);
> > + }
> > +
> > + return 0; /* unreachable */
> > +}
> > +
> > +#define VALIDATE(VAR, MIN, MAX)
> \
> > + do {
> \
> > + if (VAR < MIN || VAR > MAX) {
> \
> > + av_log(s->logctx, AV_LOG_ERROR, "RPU validation failed: "
> \
> > + #MIN" <= "#VAR" = %d <= "#MAX"\n", (int) VAR);
> \
> > + goto fail;
> \
> > + }
> \
> > + } while (0)
> > +
> > +int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t
> rpu_size)
> > +{
> > + AVDOVIRpuDataHeader *hdr = &s->header;
> > + GetBitContext *gb = &(GetBitContext){0};
> > + DOVIVdrRef *vdr;
> > + int ret;
> > +
> > + uint8_t nal_prefix;
> > + uint8_t rpu_type;
> > + uint8_t vdr_seq_info_present;
> > + uint8_t vdr_dm_metadata_present;
> > + uint8_t use_prev_vdr_rpu;
> > + uint8_t use_nlq;
> > + uint8_t profile;
> > + if ((ret = init_get_bits8(gb, rpu, rpu_size)) < 0)
>
> Notice that rpu_size is converted to int before init_get_bits8() is
> called. (It is not problematic with the use in 6/6 because the size has
> already been checked in this case.)
>
> > + return ret;
> > +
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
>
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [FFmpeg-devel] [PATCH v9 4/6] lavc: Implement Dolby Vision RPU parsing
2022-01-02 19:01 ` Hendrik Leppkes
@ 2022-01-03 5:42 ` Andreas Rheinhardt
0 siblings, 0 replies; 13+ messages in thread
From: Andreas Rheinhardt @ 2022-01-03 5:42 UTC (permalink / raw)
To: ffmpeg-devel
Hendrik Leppkes:
> Andreas Rheinhardt <andreas.rheinhardt@outlook.com> schrieb am So., 2. Jan.
> 2022, 05:18:
>
>> Niklas Haas:
>>> From: Niklas Haas <git@haasn.dev>
>>>
>>> Based on a mixture of guesswork, partial documentation in patents, and
>>> reverse engineering of real-world samples. Confirmed working for all the
>>> samples I've thrown at it.
>>>
>>> Contains some annoying machinery to persist these values in between
>>> frames, which is needed in theory even though I've never actually seen a
>>> sample that relies on it in practice. May or may not work.
>>>
>>> Since the distinction matters greatly for parsing the color matrix
>>> values, this includes a small helper function to guess the right profile
>>> from the RPU itself in case the user has forgotten to forward the dovi
>>> configuration record to the decoder. (Which in practice, only ffmpeg.c
>>> and ffplay do..)
>>>
>>> Notable omissions / deviations:
>>> - CRC32 verification. This is based on the MPEG2 CRC32 type, which does
>>> not seem to be implemented in lavu. (And I don't care enough to do so)
>>> - Linear interpolation support. Nothing documents this (beyond its
>>> existence) and no samples use it, so impossible to implement.
>>> - All of the extension metadata blocks, but these contain values that
>>> seem largely congruent with ST2094, HDR10, or other existing forms of
>>> side data, so I will defer parsing/attaching them to a future commit.
>>> - The patent describes a mechanism for predicting coefficients from
>>> previous RPUs, but the bit for the flag whether to use the
>>> prediction deltas or signal entirely new coefficients does not seem to
>>> be present in actual RPUs, so we ignore this subsystem entirely.
>>> - In the patent's spec, the NLQ subsystem also loops over
>>> num_nlq_pivots, but even in the patent the number is hard-coded to one
>>> iteration rather than signalled. So we only store one set of coefs.
>>>
>>> Heavily influenced by https://github.com/quietvoid/dovi_tool
>>> Documentation drawn from US Patent 10,701,399 B2 and ETSI GS CCM 001
>>>
>>> Signed-off-by: Niklas Haas <git@haasn.dev>
>>> ---
>>> configure | 2 +
>>> libavcodec/Makefile | 1 +
>>> libavcodec/dovi_rpu.c | 430 ++++++++++++++++++++++++++++++++++++++++++
>>> libavcodec/dovi_rpu.h | 71 +++++++
>>> 4 files changed, 504 insertions(+)
>>> create mode 100644 libavcodec/dovi_rpu.c
>>> create mode 100644 libavcodec/dovi_rpu.h
>>>
>>> diff --git a/configure b/configure
>>> index 0ccd3bda11..68658a847f 100755
>>> --- a/configure
>>> +++ b/configure
>>> @@ -2434,6 +2434,7 @@ CONFIG_EXTRA="
>>> cbs_vp9
>>> dirac_parse
>>> dnn
>>> + dovi_rpu
>>> dvprofile
>>> exif
>>> faandct
>>> @@ -2706,6 +2707,7 @@ cbs_mpeg2_select="cbs"
>>> cbs_vp9_select="cbs"
>>> dct_select="rdft"
>>> dirac_parse_select="golomb"
>>> +dovi_rpu_select="golomb"
>>> dnn_suggest="libtensorflow libopenvino"
>>> dnn_deps="avformat swscale"
>>> error_resilience_select="me_cmp"
>>> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
>>> index fb90ecea84..7364c7a91f 100644
>>> --- a/libavcodec/Makefile
>>> +++ b/libavcodec/Makefile
>>> @@ -77,6 +77,7 @@ OBJS-$(CONFIG_CBS_MPEG2) += cbs_mpeg2.o
>>> OBJS-$(CONFIG_CBS_VP9) += cbs_vp9.o
>>> OBJS-$(CONFIG_CRYSTALHD) += crystalhd.o
>>> OBJS-$(CONFIG_DCT) += dct.o dct32_fixed.o
>> dct32_float.o
>>> +OBJS-$(CONFIG_DOVI_RPU) += dovi_rpu.o
>>> OBJS-$(CONFIG_ERROR_RESILIENCE) += error_resilience.o
>>> OBJS-$(CONFIG_EXIF) += exif.o tiff_common.o
>>> OBJS-$(CONFIG_FAANDCT) += faandct.o
>>> diff --git a/libavcodec/dovi_rpu.c b/libavcodec/dovi_rpu.c
>>> new file mode 100644
>>> index 0000000000..fc2e1fb2a1
>>> --- /dev/null
>>> +++ b/libavcodec/dovi_rpu.c
>>> @@ -0,0 +1,430 @@
>>> +/*
>>> + * Dolby Vision RPU decoder
>>> + *
>>> + * Copyright (C) 2021 Jan Ekström
>>> + * Copyright (C) 2021 Niklas Haas
>>> + *
>>> + * 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/buffer.h"
>>> +
>>> +#include "dovi_rpu.h"
>>> +#include "golomb.h"
>>> +#include "get_bits.h"
>>> +
>>> +enum {
>>> + RPU_COEFF_FIXED = 0,
>>> + RPU_COEFF_FLOAT = 1,
>>> +};
>>> +
>>> +/**
>>> + * Private contents of vdr_ref.
>>> + */
>>> +typedef struct DOVIVdrRef {
>>> + AVDOVIDataMapping mapping;
>>> + AVDOVIColorMetadata color;
>>> +} DOVIVdrRef;
>>> +
>>> +void ff_dovi_ctx_unref(DOVIContext *s)
>>> +{
>>> + for (int i = 0; i < FF_ARRAY_ELEMS(s->vdr_ref); i++)
>>> + av_buffer_unref(&s->vdr_ref[i]);
>>> +
>>> + /* Preserve the user-provided fields explicitly, reset everything
>> else */
>>> + *s = (DOVIContext) {
>>> + .logctx = s->logctx,
>>> + .config = s->config,
>>> + };
>>> +}
>>> +
>>> +int ff_dovi_ctx_replace(DOVIContext *s, const DOVIContext *s0)
>>> +{
>>> + int ret;
>>> + s->logctx = s0->logctx;
>>> + s->config = s0->config;
>>> + s->mapping = s0->mapping;
>>> + s->color = s0->color;
>>> + for (int i = 0; i < DOVI_MAX_DM_ID; i++) {
>>> + if ((ret = av_buffer_replace(&s->vdr_ref[i], s0->vdr_ref[i])) <
>> 0)
>>> + goto fail;
>>> + }
>>> +
>>> + return 0;
>>> +
>>> +fail:
>>> + ff_dovi_ctx_unref(s);
>>> + return ret;
>>> +}
>>> +
>>> +int ff_dovi_attach_side_data(DOVIContext *s, AVFrame *frame)
>>> +{
>>> + AVFrameSideData *sd;
>>> + AVBufferRef *buf;
>>> + AVDOVIMetadata *dovi;
>>> + size_t dovi_size;
>>> +
>>> + if (!s->mapping || !s->color)
>>> + return 0; /* incomplete dovi metadata */
>>> +
>>> + dovi = av_dovi_metadata_alloc(&dovi_size);
>>> + if (!dovi)
>>> + return AVERROR(ENOMEM);
>>> +
>>> + buf = av_buffer_create((uint8_t *) dovi, dovi_size, NULL, NULL, 0);
>>> + if (!buf) {
>>> + av_free(dovi);
>>> + return AVERROR(ENOMEM);
>>> + }
>>> +
>>> + sd = av_frame_new_side_data_from_buf(frame,
>> AV_FRAME_DATA_DOVI_METADATA, buf);
>>> + if (!sd) {
>>> + av_buffer_unref(&buf);
>>> + return AVERROR(ENOMEM);
>>> + }
>>> +
>>> + memcpy(av_dovi_get_header(dovi), &s->header, sizeof(s->header));
>>> + memcpy(av_dovi_get_mapping(dovi), s->mapping, sizeof(*s->mapping));
>>> + memcpy(av_dovi_get_color(dovi), s->color, sizeof(*s->color));
>>
>> These are potentially problematic due to trailing padding in the
>> structures. This trailing padding might become valid fields in newer
>> versions of this structure. E.g. av_dovi_metadata_alloc() might
>> intentionally set the default values of these new fields to invalid
>> values. Yet lavc (with the old structure) sees them as padding, allowing
>> the compiler to trash these values on every write to these structures.
>> More likely though is that they will never be touched at all and
>> therefore retain their default (zero) initialization. Which might
>> conflict with the default value of these fields.
>> This can be fixed by only copying everything up to and including the
>> last field that we know of for every of these structures. This will
>> unfortunately increase the maintainence burden a bit.
>>
>
> This code is the primary source of any data in this struct. If it ever gets
> extended, this parser wanting to store more fields would be why, so
> initialization doesn't seem overly worrysome.
>
It is indeed reasonable to expect that any extension to any of these
structs would be necessitated by this code and patches to this code
making use of these new fields will be part of the same patchset adding
the new fields. But even then it is still possible to use a new lavu
with an old lavc in which case the above mentioned problem exists.
This scenario could be precluded by forbidding mixing libraries from
different snapshots.
- Andreas
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2022-01-03 5:42 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-22 15:14 [FFmpeg-devel] [PATCH v9 1/6] lavu/frame: Add Dolby Vision metadata side data type Niklas Haas
2021-12-22 15:14 ` [FFmpeg-devel] [PATCH v9 2/6] lavfi/showinfo: Support AV_FRAME_DATA_DOVI_METADATA Niklas Haas
2021-12-22 15:14 ` [FFmpeg-devel] [PATCH v9 3/6] ffprobe: " Niklas Haas
2021-12-22 15:14 ` [FFmpeg-devel] [PATCH v9 4/6] lavc: Implement Dolby Vision RPU parsing Niklas Haas
2022-01-02 4:18 ` Andreas Rheinhardt
2022-01-02 12:45 ` James Almer
2022-01-02 18:55 ` Hendrik Leppkes
2022-01-02 19:01 ` Hendrik Leppkes
2022-01-03 5:42 ` Andreas Rheinhardt
2021-12-22 15:14 ` [FFmpeg-devel] [PATCH v9 5/6] fate: Limit Dolby Vision RPU test frame count Niklas Haas
2021-12-22 15:14 ` [FFmpeg-devel] [PATCH v9 6/6] lavc/hevcdec: Parse DOVI RPU NALs Niklas Haas
2021-12-22 19:55 ` Andreas Rheinhardt
2022-01-02 4:31 ` Andreas Rheinhardt
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git