[FFmpeg-devel] [PATCH] lavc/hevc Parse SEI_TYPE_MASTERING_DISPLAY_INFO and propagate contents into the AVMasteringDisplayMetadata side data.
Neil Birkbeck
neil.birkbeck at gmail.com
Thu Jan 21 23:26:39 CET 2016
Add support for parsing SEI_TYPE_MASTERING_DISPLAY_INFO and propagate contents into
the AVMasteringDisplayMetadata side data. Primaries are ordered in RGB order and
the values are converted to the natural ranges ([0,1] for CEI 1931 Chroma coords,
and cd/m^2 for luma).
Signed-off-by: Neil Birkbeck <neil.birkbeck at gmail.com>
---
libavcodec/hevc.c | 38 ++++++++++++++++++++++++++++++++++++++
libavcodec/hevc.h | 7 +++++++
libavcodec/hevc_sei.c | 22 ++++++++++++++++++++++
3 files changed, 67 insertions(+)
diff --git a/libavcodec/hevc.c b/libavcodec/hevc.c
index c245d3b..98cc6f7 100644
--- a/libavcodec/hevc.c
+++ b/libavcodec/hevc.c
@@ -28,6 +28,7 @@
#include "libavutil/common.h"
#include "libavutil/display.h"
#include "libavutil/internal.h"
+#include "libavutil/mastering_display_metadata.h"
#include "libavutil/md5.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
@@ -2580,6 +2581,43 @@ static int set_side_data(HEVCContext *s)
s->sei_hflip, s->sei_vflip);
}
+ if (s->sei_mastering_display_info_present) {
+ // HEVC uses a g,b,r ordering, which we convert to a more natural r,g,b
+ const int mapping[3] = {2, 0, 1};
+ const float chroma_scale = 50000.f;
+ const float luma_scale = 10000.f;
+ int i;
+ AVMasteringDisplayMetadata *metadata =
+ av_mastering_display_metadata_create_side_data(out);
+ if (!metadata)
+ return AVERROR(ENOMEM);
+
+ for (i = 0; i < 3; i++) {
+ const int j = mapping[i];
+ metadata->display_primaries[i][0] =
+ s->display_primaries[j][0] / chroma_scale;
+ metadata->display_primaries[i][1] =
+ s->display_primaries[j][1] / chroma_scale;
+ }
+ metadata->white_point[0] = s->white_point[0] / chroma_scale;
+ metadata->white_point[1] = s->white_point[1] / chroma_scale;
+ metadata->max_luminance = s->max_mastering_luminance / luma_scale;
+ metadata->min_luminance = s->min_mastering_luminance / luma_scale;
+ metadata->has_luminance = 1;
+ metadata->has_primaries = 1;
+
+ av_log(s->avctx, AV_LOG_DEBUG, "Mastering Display Metadata:\n");
+ av_log(s->avctx, AV_LOG_DEBUG,
+ "r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f)\n",
+ metadata->display_primaries[0][0], metadata->display_primaries[0][1],
+ metadata->display_primaries[1][0], metadata->display_primaries[1][1],
+ metadata->display_primaries[2][0], metadata->display_primaries[2][1],
+ metadata->white_point[0], metadata->white_point[1]);
+ av_log(s->avctx, AV_LOG_DEBUG,
+ "min_luminance=%f, max_luminance=%f\n",
+ metadata->min_luminance, metadata->max_luminance);
+ }
+
if (s->a53_caption) {
AVFrameSideData* sd = av_frame_new_side_data(out,
AV_FRAME_DATA_A53_CC,
diff --git a/libavcodec/hevc.h b/libavcodec/hevc.h
index 9d72555..c64b94d 100644
--- a/libavcodec/hevc.h
+++ b/libavcodec/hevc.h
@@ -936,6 +936,13 @@ typedef struct HEVCContext {
int sei_anticlockwise_rotation;
int sei_hflip, sei_vflip;
+ /** mastering display */
+ int sei_mastering_display_info_present;
+ int display_primaries[3][2];
+ int white_point[2];
+ int max_mastering_luminance;
+ int min_mastering_luminance;
+
int picture_struct;
uint8_t* a53_caption;
diff --git a/libavcodec/hevc_sei.c b/libavcodec/hevc_sei.c
index 07856f2..095c658 100644
--- a/libavcodec/hevc_sei.c
+++ b/libavcodec/hevc_sei.c
@@ -78,6 +78,26 @@ static int decode_nal_sei_decoded_picture_hash(HEVCContext *s)
return 0;
}
+static int decode_nal_sei_mastering_display_info(HEVCContext *s)
+{
+ GetBitContext *gb = &s->HEVClc->gb;
+ int i;
+ // Mastering primaries
+ for (i = 0; i < 3; i++) {
+ s->display_primaries[i][0] = get_bits(gb, 16);
+ s->display_primaries[i][1] = get_bits(gb, 16);
+ }
+ // White point (x, y)
+ s->white_point[0] = get_bits(gb, 16);
+ s->white_point[1] = get_bits(gb, 16);
+
+ // Max and min luminance of mastering display
+ s->max_mastering_luminance = get_bits(gb, 32);
+ s->min_mastering_luminance = get_bits(gb, 32);
+ s->sei_mastering_display_info_present = 1;
+ return 0;
+}
+
static int decode_nal_sei_frame_packing_arrangement(HEVCContext *s)
{
GetBitContext *gb = &s->HEVClc->gb;
@@ -278,6 +298,8 @@ static int decode_nal_sei_prefix(HEVCContext *s, int type, int size)
skip_bits(gb, 8 * size);
return ret;
}
+ case SEI_TYPE_MASTERING_DISPLAY_INFO:
+ return decode_nal_sei_mastering_display_info(s);
case SEI_TYPE_ACTIVE_PARAMETER_SETS:
active_parameter_sets(s);
av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
--
2.7.0.rc3.207.g0ac5344
More information about the ffmpeg-devel
mailing list