diff mbox

[FFmpeg-devel,2/3] lavc: Add Content Light Level side metadata found in HEVC

Message ID 20170331080023.10432-2-robux4@gmail.com
State Superseded
Headers show

Commit Message

Steve Lhomme March 31, 2017, 8 a.m. UTC
These data are necessary when transmitting HDR over HDMI.

--
update the previous patch:
- rename CEA 861.3 to CTA-861.3
- update with MaxCLL/MaxFALL changes in avutil
---
 libavcodec/avcodec.h  |  7 +++++++
 libavcodec/avpacket.c |  1 +
 libavcodec/hevc_sei.c | 15 +++++++++++++++
 libavcodec/hevcdec.c  | 18 ++++++++++++++++++
 libavcodec/hevcdec.h  |  5 +++++
 libavcodec/utils.c    |  1 +
 libavcodec/version.h  |  2 +-
 7 files changed, 48 insertions(+), 1 deletion(-)
diff mbox

Patch

diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 60f7acefbd..6ded74b512 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -1583,6 +1583,13 @@  enum AVPacketSideDataType {
      * to the AVSphericalMapping structure.
      */
     AV_PKT_DATA_SPHERICAL,
+    
+    /**
+     * Content light level (based on CTA-861.3). This metadata should be
+     * associated with a video stream and containts data in the form of the
+     * AVContentLightMetadata struct.
+     */
+    AV_PKT_DATA_CONTENT_LIGHT_LEVEL,
 };
 
 #define AV_PKT_DATA_QUALITY_FACTOR AV_PKT_DATA_QUALITY_STATS //DEPRECATED
diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c
index 8420d8b258..2c28a26a9a 100644
--- a/libavcodec/avpacket.c
+++ b/libavcodec/avpacket.c
@@ -374,6 +374,7 @@  const char *av_packet_side_data_name(enum AVPacketSideDataType type)
     case AV_PKT_DATA_METADATA_UPDATE:            return "Metadata Update";
     case AV_PKT_DATA_MPEGTS_STREAM_ID:           return "MPEGTS Stream ID";
     case AV_PKT_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
+    case AV_PKT_DATA_CONTENT_LIGHT_LEVEL:        return "Content light level metadata";
     case AV_PKT_DATA_SPHERICAL:                  return "Spherical Mapping";
     }
     return NULL;
diff --git a/libavcodec/hevc_sei.c b/libavcodec/hevc_sei.c
index 8fc026cf1e..bb299d5a9f 100644
--- a/libavcodec/hevc_sei.c
+++ b/libavcodec/hevc_sei.c
@@ -102,6 +102,19 @@  static int decode_nal_sei_mastering_display_info(HEVCContext *s)
     return 0;
 }
 
+static int decode_nal_sei_content_light_info(HEVCContext *s)
+{
+    GetBitContext *gb = &s->HEVClc->gb;
+    // Max and average light levels
+    s->max_content_light_level     = get_bits_long(gb, 16);
+    s->max_pic_average_light_level = get_bits_long(gb, 16);
+    // As this SEI message comes before the first frame that references it,
+    // initialize the flag to 2 and decrement on IRAP access unit so it
+    // persists for the coded video sequence (e.g., between two IRAPs)
+    s-> sei_content_light_present = 2;
+    return  0;
+}
+
 static int decode_nal_sei_frame_packing_arrangement(HEVCContext *s)
 {
     GetBitContext *gb = &s->HEVClc->gb;
@@ -304,6 +317,8 @@  static int decode_nal_sei_prefix(HEVCContext *s, int type, int size)
         }
     case SEI_TYPE_MASTERING_DISPLAY_INFO:
         return decode_nal_sei_mastering_display_info(s);
+    case SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO:
+        return decode_nal_sei_content_light_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);
diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
index ef21595c44..61426979ae 100644
--- a/libavcodec/hevcdec.c
+++ b/libavcodec/hevcdec.c
@@ -2644,6 +2644,24 @@  static int set_side_data(HEVCContext *s)
                "min_luminance=%f, max_luminance=%f\n",
                av_q2d(metadata->min_luminance), av_q2d(metadata->max_luminance));
     }
+    // Decrement the mastering display flag when IRAP frame has no_rasl_output_flag=1
+    // so the side data persists for the entire coded video sequence.
+    if (s->sei_content_light_present > 0 &&
+        IS_IRAP(s) && s->no_rasl_output_flag) {
+        s->sei_content_light_present--;
+    }
+    if (s->sei_content_light_present) {
+        AVContentLightMetadata *metadata =
+            av_content_light_metadata_create_side_data(out);
+        if (!metadata)
+            return AVERROR(ENOMEM);
+        metadata->MaxCLL  = s->max_content_light_level;
+        metadata->MaxFALL = s->max_pic_average_light_level;
+
+        av_log(s->avctx, AV_LOG_DEBUG, "Content Light Level Metadata:\n");
+        av_log(s->avctx, AV_LOG_DEBUG, "MaxCLL=%d, MaxFALL=%d\n",
+               metadata->MaxCLL, metadata->MaxFALL);
+    }
 
     if (s->a53_caption) {
         AVFrameSideData* sd = av_frame_new_side_data(out,
diff --git a/libavcodec/hevcdec.h b/libavcodec/hevcdec.h
index 0c7881286c..e5879a5c2b 100644
--- a/libavcodec/hevcdec.h
+++ b/libavcodec/hevcdec.h
@@ -596,6 +596,11 @@  typedef struct HEVCContext {
     uint32_t max_mastering_luminance;
     uint32_t min_mastering_luminance;
 
+    /* content light level */
+    int sei_content_light_present;
+    uint16_t max_content_light_level;
+    uint16_t max_pic_average_light_level;
+
 } HEVCContext;
 
 int ff_hevc_decode_nal_sei(HEVCContext *s);
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 3e8677d0f4..8a47a32663 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -771,6 +771,7 @@  int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
         { AV_PKT_DATA_STEREO3D,                   AV_FRAME_DATA_STEREO3D },
         { AV_PKT_DATA_AUDIO_SERVICE_TYPE,         AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
         { AV_PKT_DATA_MASTERING_DISPLAY_METADATA, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA },
+        { AV_PKT_DATA_CONTENT_LIGHT_LEVEL,        AV_FRAME_DATA_CONTENT_LIGHT_LEVEL },
     };
 
     if (pkt) {
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 8dea5cb97b..5dbbc703ab 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -29,7 +29,7 @@ 
 
 #define LIBAVCODEC_VERSION_MAJOR  57
 #define LIBAVCODEC_VERSION_MINOR  86
-#define LIBAVCODEC_VERSION_MICRO 103
+#define LIBAVCODEC_VERSION_MICRO 104
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
                                                LIBAVCODEC_VERSION_MINOR, \