+ priv->sei_needed = 0;
+
+ // Only look for the metadata on I/IDR frame on the output. We
+ // may force an IDR frame on the output where the medadata gets
+ // changed on the input frame.
+ if ((priv->sei & SEI_MASTERING_DISPLAY) &&
+ (pic->type == PICTURE_TYPE_I || pic->type == PICTURE_TYPE_IDR)) {
+ AVFrameSideData *sd =
+ av_frame_get_side_data(pic->input_image,
+ AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
+
+ if (sd) {
+ AVMasteringDisplayMetadata *mdm =
+ (AVMasteringDisplayMetadata *)sd->data;
+
+ // SEI is needed when both the primaries and luminance are set
+ if (mdm->has_primaries && mdm->has_luminance) {
+ H265RawSEIMasteringDisplayColourVolume *mdcv =
+ &priv->sei_mastering_display;
+ const int mapping[3] = {1, 2, 0};
+ const int chroma_den = 50000;
+ const int luma_den = 10000;
+
+ for (i = 0; i < 3; i++) {
+ const int j = mapping[i];
+ mdcv->display_primaries_x[i] =
+ FFMIN(lrint(chroma_den *
+ av_q2d(mdm->display_primaries[j][0])),
+ chroma_den);
+ mdcv->display_primaries_y[i] =
+ FFMIN(lrint(chroma_den *
+ av_q2d(mdm->display_primaries[j][1])),
+ chroma_den);
+ }
+
+ mdcv->white_point_x =
+ FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[0])),
+ chroma_den);
+ mdcv->white_point_y =
+ FFMIN(lrint(chroma_den * av_q2d(mdm->white_point[1])),
+ chroma_den);
+
+ mdcv->max_display_mastering_luminance =
+ lrint(luma_den * av_q2d(mdm->max_luminance));
+ mdcv->min_display_mastering_luminance =
+ FFMIN(lrint(luma_den * av_q2d(mdm->min_luminance)),
+ mdcv->max_display_mastering_luminance);
+
+ priv->sei_needed |= SEI_MASTERING_DISPLAY;
+ }
+ }
+ }
+
+ if ((priv->sei & SEI_CONTENT_LIGHT_LEVEL) &&
+ (pic->type == PICTURE_TYPE_I || pic->type == PICTURE_TYPE_IDR)) {
+ AVFrameSideData *sd =
+ av_frame_get_side_data(pic->input_image,
+ AV_FRAME_DATA_CONTENT_LIGHT_LEVEL);
+
+ if (sd) {
+ AVContentLightMetadata *clm =
+ (AVContentLightMetadata *)sd->data;
+ H265RawSEIContentLightLevelInfo *clli =
+ &priv->sei_content_light_level;
+
+ clli->max_content_light_level = FFMIN(clm->MaxCLL, 65535);
+ clli->max_pic_average_light_level = FFMIN(clm->MaxFALL, 65535);
+
+ priv->sei_needed |= SEI_CONTENT_LIGHT_LEVEL;
+ }
+ }
+