diff mbox series

[FFmpeg-devel,2/3] avcodec/hevc_sei: parse and export Film Grain Characteristics SEI messages

Message ID 20210803001231.2346-2-jamrial@gmail.com
State Accepted
Commit 82be9f277781afa7f90a2d93bfda818c1dc84551
Headers show
Series [FFmpeg-devel,1/3] cbs_h265: add support for Film Grain Characteristics SEI message | expand

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

James Almer Aug. 3, 2021, 12:12 a.m. UTC
Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/hevc_sei.c | 44 ++++++++++++++++++++++++++++++++++
 libavcodec/hevc_sei.h | 22 +++++++++++++++++
 libavcodec/hevcdec.c  | 56 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 122 insertions(+)
diff mbox series

Patch

diff --git a/libavcodec/hevc_sei.c b/libavcodec/hevc_sei.c
index e6ae777852..2c326bf1f7 100644
--- a/libavcodec/hevc_sei.c
+++ b/libavcodec/hevc_sei.c
@@ -395,6 +395,48 @@  static int decode_nal_sei_timecode(HEVCSEITimeCode *s, GetBitContext *gb)
     return 0;
 }
 
+static int decode_film_grain_characteristics(HEVCSEIFilmGrainCharacteristics *h,
+                                             GetBitContext *gb)
+{
+    h->present = !get_bits1(gb); // film_grain_characteristics_cancel_flag
+
+    if (h->present) {
+        memset(h, 0, sizeof(*h));
+        h->model_id = get_bits(gb, 2);
+        h->separate_colour_description_present_flag = get_bits1(gb);
+        if (h->separate_colour_description_present_flag) {
+            h->bit_depth_luma = get_bits(gb, 3) + 8;
+            h->bit_depth_chroma = get_bits(gb, 3) + 8;
+            h->full_range = get_bits1(gb);
+            h->color_primaries = get_bits(gb, 8);
+            h->transfer_characteristics = get_bits(gb, 8);
+            h->matrix_coeffs = get_bits(gb, 8);
+        }
+        h->blending_mode_id = get_bits(gb, 2);
+        h->log2_scale_factor = get_bits(gb, 4);
+        for (int c = 0; c < 3; c++)
+            h->comp_model_present_flag[c] = get_bits1(gb);
+        for (int c = 0; c < 3; c++) {
+            if (h->comp_model_present_flag[c]) {
+                h->num_intensity_intervals[c] = get_bits(gb, 8) + 1;
+                h->num_model_values[c] = get_bits(gb, 3) + 1;
+                if (h->num_model_values[c] > 6)
+                    return AVERROR_INVALIDDATA;
+                for (int i = 0; i < h->num_intensity_intervals[c]; i++) {
+                    h->intensity_interval_lower_bound[c][i] = get_bits(gb, 8);
+                    h->intensity_interval_upper_bound[c][i] = get_bits(gb, 8);
+                    for (int j = 0; j < h->num_model_values[c]; j++)
+                        h->comp_model_value[c][i][j] = get_se_golomb_long(gb);
+                }
+            }
+        }
+        h->persistence_flag = get_bits1(gb);
+
+        h->present = 1;
+    }
+
+    return 0;
+}
 
 static int decode_nal_sei_prefix(GetBitContext *gb, void *logctx, HEVCSEI *s,
                                  const HEVCParamSets *ps, int type, int size)
@@ -422,6 +464,8 @@  static int decode_nal_sei_prefix(GetBitContext *gb, void *logctx, HEVCSEI *s,
         return decode_nal_sei_alternative_transfer(&s->alternative_transfer, gb);
     case SEI_TYPE_TIME_CODE:
         return decode_nal_sei_timecode(&s->timecode, gb);
+    case SEI_TYPE_FILM_GRAIN_CHARACTERISTICS:
+        return decode_film_grain_characteristics(&s->film_grain_characteristics, gb);
     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 4b6bfddeb9..ac8c61c1f1 100644
--- a/libavcodec/hevc_sei.h
+++ b/libavcodec/hevc_sei.h
@@ -109,6 +109,27 @@  typedef struct HEVCSEITimeCode {
     int32_t  time_offset_value[3];
 } HEVCSEITimeCode;
 
+typedef struct HEVCSEIFilmGrainCharacteristics {
+    int present;
+    int model_id;
+    int separate_colour_description_present_flag;
+    int bit_depth_luma;
+    int bit_depth_chroma;
+    int full_range;
+    int color_primaries;
+    int transfer_characteristics;
+    int matrix_coeffs;
+    int blending_mode_id;
+    int log2_scale_factor;
+    int comp_model_present_flag[3];
+    uint16_t num_intensity_intervals[3];
+    uint8_t num_model_values[3];
+    uint8_t intensity_interval_lower_bound[3][256];
+    uint8_t intensity_interval_upper_bound[3][256];
+    int16_t comp_model_value[3][256][6];
+    int persistence_flag;
+} HEVCSEIFilmGrainCharacteristics;
+
 typedef struct HEVCSEI {
     HEVCSEIPictureHash picture_hash;
     HEVCSEIFramePacking frame_packing;
@@ -122,6 +143,7 @@  typedef struct HEVCSEI {
     int active_seq_parameter_set_id;
     HEVCSEIAlternativeTransfer alternative_transfer;
     HEVCSEITimeCode timecode;
+    HEVCSEIFilmGrainCharacteristics film_grain_characteristics;
 } HEVCSEI;
 
 struct HEVCParamSets;
diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
index 2d35b0b182..eafb883429 100644
--- a/libavcodec/hevcdec.c
+++ b/libavcodec/hevcdec.c
@@ -26,6 +26,7 @@ 
 #include "libavutil/attributes.h"
 #include "libavutil/common.h"
 #include "libavutil/display.h"
+#include "libavutil/film_grain_params.h"
 #include "libavutil/internal.h"
 #include "libavutil/mastering_display_metadata.h"
 #include "libavutil/md5.h"
@@ -2880,6 +2881,61 @@  static int set_side_data(HEVCContext *s)
         s->sei.timecode.num_clock_ts = 0;
     }
 
+    if (s->sei.film_grain_characteristics.present &&
+        (s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN)) {
+        HEVCSEIFilmGrainCharacteristics *fgc = &s->sei.film_grain_characteristics;
+        AVFilmGrainParams *fgp = av_film_grain_params_create_side_data(out);
+        if (!fgp)
+            return AVERROR(ENOMEM);
+
+        fgp->type = AV_FILM_GRAIN_PARAMS_H274;
+
+        fgp->codec.h274.model_id = fgc->model_id;
+        if (fgc->separate_colour_description_present_flag) {
+            fgp->codec.h274.bit_depth_luma = fgc->bit_depth_luma;
+            fgp->codec.h274.bit_depth_chroma = fgc->bit_depth_chroma;
+            fgp->codec.h274.color_range = fgc->full_range + 1;
+            fgp->codec.h274.color_primaries = fgc->color_primaries;
+            fgp->codec.h274.color_trc = fgc->transfer_characteristics;
+            fgp->codec.h274.color_space = fgc->matrix_coeffs;
+        } else {
+            const HEVCSPS *sps = s->ps.sps;
+            const VUI *vui = &sps->vui;
+            fgp->codec.h274.bit_depth_luma = sps->bit_depth;
+            fgp->codec.h274.bit_depth_chroma = sps->bit_depth_chroma;
+            if (vui->video_signal_type_present_flag)
+                fgp->codec.h274.color_range = vui->video_full_range_flag + 1;
+            else
+                fgp->codec.h274.color_range = AVCOL_RANGE_UNSPECIFIED;
+            if (vui->colour_description_present_flag) {
+                fgp->codec.h274.color_primaries = vui->colour_primaries;
+                fgp->codec.h274.color_trc = vui->transfer_characteristic;
+                fgp->codec.h274.color_space = vui->matrix_coeffs;
+            } else {
+                fgp->codec.h274.color_primaries = AVCOL_PRI_UNSPECIFIED;
+                fgp->codec.h274.color_trc = AVCOL_TRC_UNSPECIFIED;
+                fgp->codec.h274.color_space = AVCOL_SPC_UNSPECIFIED;
+            }
+        }
+        fgp->codec.h274.blending_mode_id = fgc->blending_mode_id;
+        fgp->codec.h274.log2_scale_factor = fgc->log2_scale_factor;
+
+        memcpy(&fgp->codec.h274.component_model_present, &fgc->comp_model_present_flag,
+               sizeof(fgp->codec.h274.component_model_present));
+        memcpy(&fgp->codec.h274.num_intensity_intervals, &fgc->num_intensity_intervals,
+               sizeof(fgp->codec.h274.num_intensity_intervals));
+        memcpy(&fgp->codec.h274.num_model_values, &fgc->num_model_values,
+               sizeof(fgp->codec.h274.num_model_values));
+        memcpy(&fgp->codec.h274.intensity_interval_lower_bound, &fgc->intensity_interval_lower_bound,
+               sizeof(fgp->codec.h274.intensity_interval_lower_bound));
+        memcpy(&fgp->codec.h274.intensity_interval_upper_bound, &fgc->intensity_interval_upper_bound,
+               sizeof(fgp->codec.h274.intensity_interval_upper_bound));
+        memcpy(&fgp->codec.h274.comp_model_value, &fgc->comp_model_value,
+               sizeof(fgp->codec.h274.comp_model_value));
+
+        fgc->present = fgc->persistence_flag;
+    }
+
     if (s->sei.dynamic_hdr_plus.info) {
         AVBufferRef *info_ref = av_buffer_ref(s->sei.dynamic_hdr_plus.info);
         if (!info_ref)