Skip to content

Commit

Permalink
extending support for BT.2020 colorspace
Browse files Browse the repository at this point in the history
  • Loading branch information
ddennedy committed Dec 28, 2024
1 parent 0eb5994 commit 2b9f250
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/modules/avformat/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ int mlt_set_luma_transfer(struct SwsContext *context,
int src_range = src_full_range ? 1 : 0;
int dst_range = dst_full_range ? 1 : 0;

sws_getColorspaceDetails(context,
(int **) &src_coefficients,
&src_range,
(int **) &dst_coefficients,
&dst_range,
&brightness,
&contrast,
&saturation);
switch (src_colorspace) {
case 170:
case 470:
Expand Down
43 changes: 43 additions & 0 deletions src/modules/avformat/consumer_avformat.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ static void color_trc_from_colorspace(mlt_properties properties)
case 170:
mlt_properties_set_int(properties, "color_trc", AVCOL_TRC_SMPTE170M);
break;
case 2020:
mlt_properties_set_int(properties, "color_trc", AVCOL_TRC_BT2020_10);
break;
default:
break;
}
Expand Down Expand Up @@ -377,6 +380,39 @@ static void color_primaries_from_colorspace(mlt_properties properties)
case 170:
mlt_properties_set_int(properties, "color_primaries", AVCOL_PRI_SMPTE170M);
break;
case 2020:
mlt_properties_set_int(properties, "color_primaries", AVCOL_PRI_BT2020);
break;
default:
break;
}
}

static void av_colorspace_from_colorspace(mlt_properties properties, int colorspace)
{
// Default color_space from colorspace.
switch (colorspace) {
case 0: // sRGB
mlt_properties_set_int(properties, "colorspace", AVCOL_SPC_RGB);
break;
case 601:
mlt_properties_set_int(properties, "colorspace", AVCOL_SPC_BT470BG);
break;
case 709:
mlt_properties_set_int(properties, "colorspace", AVCOL_SPC_BT709);
break;
case 240:
mlt_properties_set_int(properties, "colorspace", AVCOL_SPC_SMPTE240M);
break;
case 170:
mlt_properties_set_int(properties, "colorspace", AVCOL_SPC_SMPTE170M);
break;
case 2020:
mlt_properties_set_int(properties, "colorspace", AVCOL_SPC_BT2020_NCL);
break;
case 2021:
mlt_properties_set_int(properties, "colorspace", AVCOL_SPC_BT2020_CL);
break;
default:
break;
}
Expand Down Expand Up @@ -928,6 +964,7 @@ static AVStream *add_video_stream(mlt_consumer consumer,
}
int colorspace = mlt_properties_get_int(properties, "colorspace");
mlt_properties_set(properties, "colorspace", NULL);
av_colorspace_from_colorspace(properties, colorspace);
apply_properties(c, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM);
mlt_properties_set_int(properties, "colorspace", colorspace);

Expand Down Expand Up @@ -1858,6 +1895,12 @@ static void *consumer_thread(void *arg)
} else if (strstr(pix_fmt_name, "rgb") || strstr(pix_fmt_name, "bgr")) {
mlt_properties_set(properties, "mlt_image_format", "rgb");
img_fmt = mlt_image_rgb;
} else if (strstr(pix_fmt_name, "yuv420p10le")) {
mlt_properties_set(properties, "mlt_image_format", "yuv420p10");
img_fmt = mlt_image_yuv420p10;
} else if (strstr(pix_fmt_name, "yuv444p10le")) {
mlt_properties_set(properties, "mlt_image_format", "yuv444p10");
img_fmt = mlt_image_yuv444p10;
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/modules/avformat/producer_avformat.c
Original file line number Diff line number Diff line change
Expand Up @@ -2867,6 +2867,12 @@ static int video_codec_init(producer_avformat self, int index, mlt_properties pr
case AVCOL_SPC_BT709:
self->yuv_colorspace = 709;
break;
case AVCOL_SPC_BT2020_NCL:
self->yuv_colorspace = 2020;
break;
case AVCOL_SPC_BT2020_CL:
self->yuv_colorspace = 2021;
break;
default:
// This is a heuristic Charles Poynton suggests in "Digital Video and HDTV"
self->yuv_colorspace = self->video_codec->width * self->video_codec->height > 750000
Expand All @@ -2893,6 +2899,9 @@ static int video_codec_init(producer_avformat self, int index, mlt_properties pr
case AVCOL_PRI_SMPTE240M:
self->color_primaries = 601525;
break;
case AVCOL_PRI_BT2020:
self->color_primaries = 2020;
break;
case AVCOL_PRI_BT709:
case AVCOL_PRI_UNSPECIFIED:
default:
Expand Down
14 changes: 12 additions & 2 deletions src/modules/movit/filter_movit_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ enum AVColorTransferCharacteristic {
AVCOL_TRC_IEC61966_2_1 = 13, ///< IEC 61966-2-1 (sRGB or sYCC)
AVCOL_TRC_BT2020_10 = 14, ///< ITU-R BT2020 for 10 bit system
AVCOL_TRC_BT2020_12 = 15, ///< ITU-R BT2020 for 12 bit system
AVCOL_TRC_SMPTE2084 = 16, ///< SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems
AVCOL_TRC_SMPTEST2084 = AVCOL_TRC_SMPTE2084,
AVCOL_TRC_SMPTE428 = 17, ///< SMPTE ST 428-1
AVCOL_TRC_SMPTEST428_1 = AVCOL_TRC_SMPTE428,
AVCOL_TRC_ARIB_STD_B67 = 18, ///< ARIB STD-B67, known as "Hybrid log-gamma"
AVCOL_TRC_NB, ///< Not part of ABI
};

Expand Down Expand Up @@ -151,10 +156,10 @@ static GammaCurve getGammaCurve(mlt_properties properties)
} else if (!strcmp(color_trc, "linear")) {
mlt_properties_set_int(properties, "color_trc", AVCOL_TRC_LINEAR);
return GAMMA_LINEAR;
} else if (!strcmp(color_trc, "bt2020_10bit")) {
} else if (!strcmp(color_trc, "bt2020-10")) {
mlt_properties_set_int(properties, "color_trc", AVCOL_TRC_BT2020_10);
return GAMMA_REC_2020_10_BIT;
} else if (!strcmp(color_trc, "bt2020_12bit")) {
} else if (!strcmp(color_trc, "bt2020-12")) {
mlt_properties_set_int(properties, "color_trc", AVCOL_TRC_BT2020_12);
return GAMMA_REC_2020_12_BIT;
}
Expand All @@ -172,6 +177,8 @@ static void get_format_from_properties(mlt_properties properties,
case 601:
ycbcr_format->luma_coefficients = YCBCR_REC_601;
break;
case 2020:
ycbcr_format->luma_coefficients = YCBCR_REC_2020;
case 709:
default:
ycbcr_format->luma_coefficients = YCBCR_REC_709;
Expand All @@ -186,6 +193,9 @@ static void get_format_from_properties(mlt_properties properties,
case 601525:
image_format->color_space = COLORSPACE_REC_601_525;
break;
case 2020:
image_format->color_space = COLORSPACE_REC_2020;
break;
case 709:
default:
image_format->color_space = COLORSPACE_REC_709;
Expand Down

0 comments on commit 2b9f250

Please sign in to comment.