diff mbox series

[FFmpeg-devel,2/4] avcodec: add support for hevc ambient viewing environment SEI message

Message ID 1648423513-21261-2-git-send-email-lance.lmwang@gmail.com
State New
Headers show
Series [FFmpeg-devel,1/4] avutil: add ambient viewing environment metadata side data | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Limin Wang March 27, 2022, 11:25 p.m. UTC
From: Limin Wang <lance.lmwang@gmail.com>

Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
---
 libavcodec/hevc_sei.c      | 19 +++++++++++++++++++
 libavcodec/hevc_sei.h      |  8 ++++++++
 libavcodec/hevcdec.c       | 11 +++++++++++
 tests/ref/fate/hevc-dv-rpu |  6 ++++++
 4 files changed, 44 insertions(+)
diff mbox series

Patch

diff --git a/libavcodec/hevc_sei.c b/libavcodec/hevc_sei.c
index ec3036f..d94df4e 100644
--- a/libavcodec/hevc_sei.c
+++ b/libavcodec/hevc_sei.c
@@ -497,6 +497,23 @@  static int decode_film_grain_characteristics(HEVCSEIFilmGrainCharacteristics *h,
     return 0;
 }
 
+static int decode_ambient_viewing_env(HEVCSEIAmbientViewingEnvironment *s, GetBitContext *gb, int size)
+{
+    if (size < 8)
+        return AVERROR_INVALIDDATA;
+
+    s->ambient_illuminance  = get_bits(gb, 32);
+    s->ambient_light_x      = get_bits(gb, 16);
+    s->ambient_light_y      = get_bits(gb, 16);
+    size -= 8;
+
+    s->present = 1;
+
+    skip_bits_long(gb, 8 * size);
+    return  0;
+}
+
+
 static int decode_nal_sei_prefix(GetBitContext *gb, void *logctx, HEVCSEI *s,
                                  const HEVCParamSets *ps, int type, int size)
 {
@@ -525,6 +542,8 @@  static int decode_nal_sei_prefix(GetBitContext *gb, void *logctx, HEVCSEI *s,
         return decode_nal_sei_timecode(&s->timecode, gb);
     case SEI_TYPE_FILM_GRAIN_CHARACTERISTICS:
         return decode_film_grain_characteristics(&s->film_grain_characteristics, gb);
+    case SEI_TYPE_AMBIENT_VIEWING_ENVIRONMENT:
+        return decode_ambient_viewing_env(&s->ambient_viewing_env, gb, size);
     default:
         av_log(logctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
         skip_bits_long(gb, 8 * size);
diff --git a/libavcodec/hevc_sei.h b/libavcodec/hevc_sei.h
index f198402..c7623f5 100644
--- a/libavcodec/hevc_sei.h
+++ b/libavcodec/hevc_sei.h
@@ -134,6 +134,13 @@  typedef struct HEVCSEIFilmGrainCharacteristics {
     int persistence_flag;
 } HEVCSEIFilmGrainCharacteristics;
 
+typedef struct HEVCSEIAmbientViewingEnvironment {
+    int present;
+    uint32_t ambient_illuminance;
+    uint16_t ambient_light_x;
+    uint16_t ambient_light_y;
+} HEVCSEIAmbientViewingEnvironment;
+
 typedef struct HEVCSEI {
     HEVCSEIPictureHash picture_hash;
     HEVCSEIFramePacking frame_packing;
@@ -149,6 +156,7 @@  typedef struct HEVCSEI {
     HEVCSEIAlternativeTransfer alternative_transfer;
     HEVCSEITimeCode timecode;
     HEVCSEIFilmGrainCharacteristics film_grain_characteristics;
+    HEVCSEIAmbientViewingEnvironment ambient_viewing_env;
 } HEVCSEI;
 
 struct HEVCParamSets;
diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
index 09c07ac..9be366e 100644
--- a/libavcodec/hevcdec.c
+++ b/libavcodec/hevcdec.c
@@ -36,6 +36,7 @@ 
 #include "libavutil/pixdesc.h"
 #include "libavutil/stereo3d.h"
 #include "libavutil/timecode.h"
+#include "libavutil/ambient_viewing_env_metadata.h"
 
 #include "bswapdsp.h"
 #include "bytestream.h"
@@ -2998,6 +2999,15 @@  static int set_side_data(HEVCContext *s)
         }
     }
 
+    if (s->sei.ambient_viewing_env.present > 0) {
+        AVAmbientViewingEnvMetadata *metadata = av_ambient_viewing_env_metadata_create_side_data(out);
+        if (!metadata)
+            return AVERROR(ENOMEM);
+        metadata->ambient_illuminance = s->sei.ambient_viewing_env.ambient_illuminance;
+        metadata->ambient_light_x = s->sei.ambient_viewing_env.ambient_light_x;
+        metadata->ambient_light_y = s->sei.ambient_viewing_env.ambient_light_y;
+    }
+
     return 0;
 }
 
@@ -3800,6 +3810,7 @@  static int hevc_update_thread_context(AVCodecContext *dst,
     s->sei.mastering_display    = s0->sei.mastering_display;
     s->sei.content_light        = s0->sei.content_light;
     s->sei.alternative_transfer = s0->sei.alternative_transfer;
+    s->sei.ambient_viewing_env  = s0->sei.ambient_viewing_env;
 
     ret = export_stream_params_from_sei(s);
     if (ret < 0)
diff --git a/tests/ref/fate/hevc-dv-rpu b/tests/ref/fate/hevc-dv-rpu
index 1980ab1..6879f71 100644
--- a/tests/ref/fate/hevc-dv-rpu
+++ b/tests/ref/fate/hevc-dv-rpu
@@ -117,6 +117,9 @@  source_min_pq=0
 source_max_pq=3079
 source_diagonal=42
 [/SIDE_DATA]
+[SIDE_DATA]
+side_data_type=Ambient Viewing Environment
+[/SIDE_DATA]
 [/FRAME]
 [FRAME]
 [SIDE_DATA]
@@ -234,4 +237,7 @@  source_min_pq=0
 source_max_pq=3079
 source_diagonal=42
 [/SIDE_DATA]
+[SIDE_DATA]
+side_data_type=Ambient Viewing Environment
+[/SIDE_DATA]
 [/FRAME]