diff --git a/libavcodec/h2645_sei.c b/libavcodec/h2645_sei.c
index 1793c07285..72b976f92e 100644
--- a/libavcodec/h2645_sei.c
+++ b/libavcodec/h2645_sei.c
@@ -429,7 +429,9 @@ static int is_frame_packing_type_valid(SEIFpaType type, enum AVCodecID codec_id)
 
 int ff_h2645_sei_to_frame(AVFrame *frame, H2645SEI *sei,
                           enum AVCodecID codec_id,
-                          AVCodecContext *avctx)
+                          AVCodecContext *avctx, const H2645VUI *vui,
+                          unsigned bit_depth_luma, unsigned bit_depth_chroma,
+                          int seed)
 {
     H2645SEIFramePacking *fp = &sei->frame_packing;
 
@@ -544,6 +546,69 @@ int ff_h2645_sei_to_frame(AVFrame *frame, H2645SEI *sei,
         }
     }
 
+    if (sei->film_grain_characteristics.present) {
+        H2645SEIFilmGrainCharacteristics *fgc = &sei->film_grain_characteristics;
+        AVFilmGrainParams *fgp = av_film_grain_params_create_side_data(frame);
+        AVFilmGrainH274Params *h274;
+
+        if (!fgp)
+            return AVERROR(ENOMEM);
+
+        fgp->type = AV_FILM_GRAIN_PARAMS_H274;
+        h274      = &fgp->codec.h274;
+
+        fgp->seed = seed;
+
+        h274->model_id = fgc->model_id;
+        if (fgc->separate_colour_description_present_flag) {
+            h274->bit_depth_luma   = fgc->bit_depth_luma;
+            h274->bit_depth_chroma = fgc->bit_depth_chroma;
+            h274->color_range      = fgc->full_range + 1;
+            h274->color_primaries  = fgc->color_primaries;
+            h274->color_trc        = fgc->transfer_characteristics;
+            h274->color_space      = fgc->matrix_coeffs;
+        } else {
+            h274->bit_depth_luma   = bit_depth_luma;
+            h274->bit_depth_chroma = bit_depth_chroma;
+            if (vui->video_signal_type_present_flag)
+                h274->color_range = vui->video_full_range_flag + 1;
+            else
+                h274->color_range = AVCOL_RANGE_UNSPECIFIED;
+            if (vui->colour_description_present_flag) {
+                h274->color_primaries = vui->colour_primaries;
+                h274->color_trc       = vui->transfer_characteristics;
+                h274->color_space     = vui->matrix_coeffs;
+            } else {
+                h274->color_primaries = AVCOL_PRI_UNSPECIFIED;
+                h274->color_trc       = AVCOL_TRC_UNSPECIFIED;
+                h274->color_space     = AVCOL_SPC_UNSPECIFIED;
+            }
+        }
+        h274->blending_mode_id  = fgc->blending_mode_id;
+        h274->log2_scale_factor = fgc->log2_scale_factor;
+
+        memcpy(&h274->component_model_present, &fgc->comp_model_present_flag,
+               sizeof(h274->component_model_present));
+        memcpy(&h274->num_intensity_intervals, &fgc->num_intensity_intervals,
+               sizeof(h274->num_intensity_intervals));
+        memcpy(&h274->num_model_values, &fgc->num_model_values,
+               sizeof(h274->num_model_values));
+        memcpy(&h274->intensity_interval_lower_bound, &fgc->intensity_interval_lower_bound,
+               sizeof(h274->intensity_interval_lower_bound));
+        memcpy(&h274->intensity_interval_upper_bound, &fgc->intensity_interval_upper_bound,
+               sizeof(h274->intensity_interval_upper_bound));
+        memcpy(&h274->comp_model_value, &fgc->comp_model_value,
+               sizeof(h274->comp_model_value));
+
+        if (IS_H264(codec_id))
+            fgc->present = !!fgc->repetition_period;
+        else
+            fgc->present = fgc->persistence_flag;
+
+        if (avctx)
+            avctx->properties |= FF_CODEC_PROPERTY_FILM_GRAIN;
+    }
+
     return 0;
 }
 
diff --git a/libavcodec/h2645_sei.h b/libavcodec/h2645_sei.h
index eb00107abb..f3ee9af524 100644
--- a/libavcodec/h2645_sei.h
+++ b/libavcodec/h2645_sei.h
@@ -28,6 +28,7 @@
 #include "bytestream.h"
 #include "codec_id.h"
 #include "get_bits.h"
+#include "h2645_vui.h"
 #include "sei.h"
 
 typedef struct H2645SEIA53Caption {
@@ -136,6 +137,8 @@ void ff_h2645_sei_reset(H2645SEI *s);
 
 int ff_h2645_sei_to_frame(AVFrame *frame, H2645SEI *sei,
                           enum AVCodecID codec_id,
-                          AVCodecContext *avctx);
+                          AVCodecContext *avctx, const H2645VUI *vui,
+                          unsigned bit_depth_luma, unsigned bit_depth_chroma,
+                          int seed);
 
 #endif /* AVCODEC_H2645_SEI_H */
diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c
index ba873254ce..51d8d944be 100644
--- a/libavcodec/h264_slice.c
+++ b/libavcodec/h264_slice.c
@@ -1225,65 +1225,12 @@ static int h264_export_frame_props(H264Context *h)
         }
     }
 
-    ret = ff_h2645_sei_to_frame(out, &h->sei.common, AV_CODEC_ID_H264, h->avctx);
+    ret = ff_h2645_sei_to_frame(out, &h->sei.common, AV_CODEC_ID_H264, h->avctx,
+                                &sps->vui, sps->bit_depth_luma, sps->bit_depth_chroma,
+                                cur->poc + (h->poc_offset << 5));
     if (ret < 0)
         return ret;
 
-    if (h->sei.common.film_grain_characteristics.present) {
-        H2645SEIFilmGrainCharacteristics *fgc = &h->sei.common.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->seed = cur->poc + (h->poc_offset << 5);
-
-        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 {
-            fgp->codec.h274.bit_depth_luma = sps->bit_depth_luma;
-            fgp->codec.h274.bit_depth_chroma = sps->bit_depth_chroma;
-            if (sps->vui.video_signal_type_present_flag)
-                fgp->codec.h274.color_range = sps->vui.video_full_range_flag + 1;
-            else
-                fgp->codec.h274.color_range = AVCOL_RANGE_UNSPECIFIED;
-            if (sps->vui.colour_description_present_flag) {
-                fgp->codec.h274.color_primaries = sps->vui.colour_primaries;
-                fgp->codec.h274.color_trc       = sps->vui.transfer_characteristics;
-                fgp->codec.h274.color_space     = sps->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->repetition_period;
-
-        h->avctx->properties |= FF_CODEC_PROPERTY_FILM_GRAIN;
-    }
-
     if (h->sei.picture_timing.timecode_cnt > 0) {
         uint32_t *tc_sd;
         char tcbuf[AV_TIMECODE_STR_SIZE];
diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
index 7147944bc6..298edb782b 100644
--- a/libavcodec/hevcdec.c
+++ b/libavcodec/hevcdec.c
@@ -2799,7 +2799,10 @@ static int set_side_data(HEVCContext *s)
                metadata->MaxCLL, metadata->MaxFALL);
     }
 
-    ret = ff_h2645_sei_to_frame(out, &s->sei.common, AV_CODEC_ID_HEVC, NULL);
+    ret = ff_h2645_sei_to_frame(out, &s->sei.common, AV_CODEC_ID_HEVC, NULL,
+                                &s->ps.sps->vui.common,
+                                s->ps.sps->bit_depth, s->ps.sps->bit_depth_chroma,
+                                s->ref->poc /* no poc_offset in HEVC */);
     if (ret < 0)
         return ret;
 
@@ -2829,61 +2832,6 @@ static int set_side_data(HEVCContext *s)
         s->sei.timecode.num_clock_ts = 0;
     }
 
-    if (s->sei.common.film_grain_characteristics.present) {
-        H2645SEIFilmGrainCharacteristics *fgc = &s->sei.common.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->seed = s->ref->poc; /* no poc_offset in HEVC */
-
-        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->common.video_signal_type_present_flag)
-                fgp->codec.h274.color_range = vui->common.video_full_range_flag + 1;
-            else
-                fgp->codec.h274.color_range = AVCOL_RANGE_UNSPECIFIED;
-            if (vui->common.colour_description_present_flag) {
-                fgp->codec.h274.color_primaries = vui->common.colour_primaries;
-                fgp->codec.h274.color_trc       = vui->common.transfer_characteristics;
-                fgp->codec.h274.color_space     = vui->common.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.common.dynamic_hdr_plus.info) {
         AVBufferRef *info_ref = av_buffer_ref(s->sei.common.dynamic_hdr_plus.info);
         if (!info_ref)
