[FFmpeg-cvslog] cbs_h264: add support for Film Grain Characteristics SEI messages

James Almer git at videolan.org
Fri Jul 23 17:09:52 EEST 2021


ffmpeg | branch: master | James Almer <jamrial at gmail.com> | Fri Jul 23 11:05:04 2021 -0300| [41d1dba4d281aafc4c67aa24ddb1798b25f1e27f] | committer: James Almer

cbs_h264: add support for Film Grain Characteristics SEI messages

Signed-off-by: James Almer <jamrial at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=41d1dba4d281aafc4c67aa24ddb1798b25f1e27f
---

 libavcodec/cbs_h264.h                 | 21 +++++++++
 libavcodec/cbs_h2645.c                |  6 +++
 libavcodec/cbs_h264_syntax_template.c | 83 +++++++++++++++++++++++++++++++++++
 3 files changed, 110 insertions(+)

diff --git a/libavcodec/cbs_h264.h b/libavcodec/cbs_h264.h
index 5a8641a333..ca9b688c05 100644
--- a/libavcodec/cbs_h264.h
+++ b/libavcodec/cbs_h264.h
@@ -272,6 +272,27 @@ typedef struct H264RawSEIRecoveryPoint {
     uint8_t changing_slice_group_idc;
 } H264RawSEIRecoveryPoint;
 
+typedef struct H264RawFilmGrainCharacteristics {
+    uint8_t      film_grain_characteristics_cancel_flag;
+    uint8_t      film_grain_model_id;
+    uint8_t      separate_colour_description_present_flag;
+    uint8_t      film_grain_bit_depth_luma_minus8;
+    uint8_t      film_grain_bit_depth_chroma_minus8;
+    uint8_t      film_grain_full_range_flag;
+    uint8_t      film_grain_colour_primaries;
+    uint8_t      film_grain_transfer_characteristics;
+    uint8_t      film_grain_matrix_coefficients;
+    uint8_t      blending_mode_id;
+    uint8_t      log2_scale_factor;
+    uint8_t      comp_model_present_flag[3];
+    uint8_t      num_intensity_intervals_minus1[3];
+    uint8_t      num_model_values_minus1[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];
+    uint8_t      film_grain_characteristics_repetition_period;
+} H264RawFilmGrainCharacteristics;
+
 typedef struct H264RawSEIDisplayOrientation {
     uint8_t display_orientation_cancel_flag;
     uint8_t hor_flip;
diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c
index 6005d46e0d..61c1e13cf7 100644
--- a/libavcodec/cbs_h2645.c
+++ b/libavcodec/cbs_h2645.c
@@ -1570,6 +1570,12 @@ static const SEIMessageTypeDescriptor cbs_sei_h264_types[] = {
         sizeof(H264RawSEIRecoveryPoint),
         SEI_MESSAGE_RW(h264, sei_recovery_point),
     },
+    {
+        SEI_TYPE_FILM_GRAIN_CHARACTERISTICS,
+        1, 0,
+        sizeof(H264RawFilmGrainCharacteristics),
+        SEI_MESSAGE_RW(h264, film_grain_characteristics),
+    },
     {
         SEI_TYPE_DISPLAY_ORIENTATION,
         1, 0,
diff --git a/libavcodec/cbs_h264_syntax_template.c b/libavcodec/cbs_h264_syntax_template.c
index 9587f33985..0f8bba4a0d 100644
--- a/libavcodec/cbs_h264_syntax_template.c
+++ b/libavcodec/cbs_h264_syntax_template.c
@@ -719,6 +719,89 @@ static int FUNC(sei_recovery_point)(CodedBitstreamContext *ctx, RWContext *rw,
     return 0;
 }
 
+static int FUNC(film_grain_characteristics)(CodedBitstreamContext *ctx, RWContext *rw,
+                                            H264RawFilmGrainCharacteristics *current,
+                                            SEIMessageState *state)
+{
+    CodedBitstreamH264Context *h264 = ctx->priv_data;
+    const H264RawSPS *sps;
+    int err, c, i, j;
+
+    HEADER("Film Grain Characteristics");
+
+    sps = h264->active_sps;
+    if (!sps) {
+        // If there is exactly one possible SPS but it is not yet active
+        // then just assume that it should be the active one.
+        int i, k = -1;
+        for (i = 0; i < H264_MAX_SPS_COUNT; i++) {
+            if (h264->sps[i]) {
+                if (k >= 0) {
+                    k = -1;
+                    break;
+                }
+                k = i;
+            }
+        }
+        if (k >= 0)
+            sps = h264->sps[k];
+    }
+
+    flag(film_grain_characteristics_cancel_flag);
+    if (!current->film_grain_characteristics_cancel_flag) {
+        int filmGrainBitDepth[3];
+
+        u(2, film_grain_model_id, 0, 1);
+        flag(separate_colour_description_present_flag);
+        if (current->separate_colour_description_present_flag) {
+            ub(3, film_grain_bit_depth_luma_minus8);
+            ub(3, film_grain_bit_depth_chroma_minus8);
+            flag(film_grain_full_range_flag);
+            ub(8, film_grain_colour_primaries);
+            ub(8, film_grain_transfer_characteristics);
+            ub(8, film_grain_matrix_coefficients);
+        } else {
+            if (!sps) {
+                av_log(ctx->log_ctx, AV_LOG_ERROR,
+                       "No active SPS for film_grain_characteristics.\n");
+                return AVERROR_INVALIDDATA;
+            }
+            infer(film_grain_bit_depth_luma_minus8, sps->bit_depth_luma_minus8);
+            infer(film_grain_bit_depth_chroma_minus8, sps->bit_depth_chroma_minus8);
+            infer(film_grain_full_range_flag, sps->vui.video_full_range_flag);
+            infer(film_grain_colour_primaries, sps->vui.colour_primaries);
+            infer(film_grain_transfer_characteristics, sps->vui.transfer_characteristics);
+            infer(film_grain_matrix_coefficients, sps->vui.matrix_coefficients);
+        }
+
+        filmGrainBitDepth[0] = current->film_grain_bit_depth_luma_minus8 + 8;
+        filmGrainBitDepth[1] =
+        filmGrainBitDepth[2] = current->film_grain_bit_depth_chroma_minus8 + 8;
+
+        u(2, blending_mode_id, 0, 1);
+        ub(4, log2_scale_factor);
+        for (c = 0; c < 3; c++)
+            flags(comp_model_present_flag[c], 1, c);
+        for (c = 0; c < 3; c++) {
+            if (current->comp_model_present_flag[c]) {
+                ubs(8, num_intensity_intervals_minus1[c], 1, c);
+                us(3, num_model_values_minus1[c], 0, 5, 1, c);
+                for (i = 0; i <= current->num_intensity_intervals_minus1[c]; i++) {
+                    ubs(8, intensity_interval_lower_bound[c][i], 2, c, i);
+                    ubs(8, intensity_interval_upper_bound[c][i], 2, c, i);
+                    for (j = 0; j <= current->num_model_values_minus1[c]; j++)
+                        ses(comp_model_value[c][i][j],      0 - current->film_grain_model_id * (1 << (filmGrainBitDepth[c] - 1)),
+                            ((1 << filmGrainBitDepth[c]) - 1) - current->film_grain_model_id * (1 << (filmGrainBitDepth[c] - 1)),
+                            3, c, i, j);
+                }
+            }
+        }
+        ue(film_grain_characteristics_repetition_period, 0, 16384);
+    }
+
+    return 0;
+}
+
 static int FUNC(sei_display_orientation)(CodedBitstreamContext *ctx, RWContext *rw,
                                          H264RawSEIDisplayOrientation *current,
                                          SEIMessageState *sei)



More information about the ffmpeg-cvslog mailing list