Skip to content

Commit

Permalink
csputils: replace mp_colorspace with pl_color_space
Browse files Browse the repository at this point in the history
  • Loading branch information
kasper93 authored and Dudemanguy committed Jan 22, 2024
1 parent 9dd1a13 commit 66e451f
Show file tree
Hide file tree
Showing 44 changed files with 609 additions and 980 deletions.
21 changes: 12 additions & 9 deletions demux/demux_mkv.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include <libavcodec/avcodec.h>
#include <libavcodec/version.h>

#include <libplacebo/utils/libav.h>

#include "config.h"

#if HAVE_ZLIB
Expand Down Expand Up @@ -108,7 +110,8 @@ typedef struct mkv_track {
double v_frate;
uint32_t colorspace;
int stereo_mode;
struct mp_colorspace color;
struct pl_color_repr repr;
struct pl_color_space color;
uint32_t v_crop_top, v_crop_left, v_crop_right, v_crop_bottom;
float v_projection_pose_roll;
bool v_projection_pose_roll_set;
Expand Down Expand Up @@ -573,24 +576,24 @@ static void parse_trackcolour(struct demuxer *demuxer, struct mkv_track *track,
// 23001-8:2013/DCOR1, which is the same order used by libavutil/pixfmt.h,
// so we can just re-use our avcol_ conversion functions.
if (colour->n_matrix_coefficients) {
track->color.space = avcol_spc_to_mp_csp(colour->matrix_coefficients);
track->repr.sys = pl_system_from_av(colour->matrix_coefficients);
MP_DBG(demuxer, "| + Matrix: %s\n",
m_opt_choice_str(mp_csp_names, track->color.space));
m_opt_choice_str(pl_csp_names, track->repr.sys));
}
if (colour->n_primaries) {
track->color.primaries = avcol_pri_to_mp_csp_prim(colour->primaries);
track->color.primaries = pl_primaries_from_av(colour->primaries);
MP_DBG(demuxer, "| + Primaries: %s\n",
m_opt_choice_str(mp_csp_prim_names, track->color.primaries));
m_opt_choice_str(pl_csp_prim_names, track->color.primaries));
}
if (colour->n_transfer_characteristics) {
track->color.gamma = avcol_trc_to_mp_csp_trc(colour->transfer_characteristics);
track->color.transfer = pl_transfer_from_av(colour->transfer_characteristics);
MP_DBG(demuxer, "| + Gamma: %s\n",
m_opt_choice_str(mp_csp_trc_names, track->color.gamma));
m_opt_choice_str(pl_csp_trc_names, track->color.transfer));
}
if (colour->n_range) {
track->color.levels = avcol_range_to_mp_csp_levels(colour->range);
track->repr.levels = pl_levels_from_av(colour->range);
MP_DBG(demuxer, "| + Levels: %s\n",
m_opt_choice_str(mp_csp_levels_names, track->color.levels));
m_opt_choice_str(pl_csp_levels_names, track->repr.levels));
}
if (colour->n_max_cll) {
track->color.hdr.max_cll = colour->max_cll;
Expand Down
5 changes: 3 additions & 2 deletions demux/stheader.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ struct mp_codec_params {
int disp_w, disp_h; // display size
int rotate; // intended display rotation, in degrees, [0, 359]
int stereo_mode; // mp_stereo3d_mode (0 if none/unknown)
struct mp_colorspace color; // colorspace info where available
struct mp_rect crop; // crop to be applied
struct pl_color_space color; // colorspace info where available
struct pl_color_repr repr; // color representaion info where available
struct mp_rect crop; // crop to be applied

// STREAM_VIDEO + STREAM_AUDIO
int bits_per_coded_sample;
Expand Down
3 changes: 2 additions & 1 deletion filters/f_decoder_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,8 @@ static void fix_image_params(struct priv *p,
m.rotate = (m.rotate + opts->video_rotate) % 360;
}

mp_colorspace_merge(&m.color, &c->color);
pl_color_space_merge(&m.color, &c->color);
pl_color_repr_merge(&m.repr, &c->repr);

// Guess missing colorspace fields from metadata. This guarantees all
// fields are at least set to legal values afterwards.
Expand Down
10 changes: 5 additions & 5 deletions player/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -2341,16 +2341,16 @@ static int property_imgparams(const struct mp_image_params *p, int action, void
{"sar", SUB_PROP_FLOAT(p->w / (double)p->h)},
{"sar-name", SUB_PROP_STR(sar_name), .unavailable = !sar_name},
{"colormatrix",
SUB_PROP_STR(m_opt_choice_str(mp_csp_names, p->color.space))},
SUB_PROP_STR(m_opt_choice_str(pl_csp_names, p->repr.sys))},
{"colorlevels",
SUB_PROP_STR(m_opt_choice_str(mp_csp_levels_names, p->color.levels))},
SUB_PROP_STR(m_opt_choice_str(pl_csp_levels_names, p->repr.levels))},
{"primaries",
SUB_PROP_STR(m_opt_choice_str(mp_csp_prim_names, p->color.primaries))},
SUB_PROP_STR(m_opt_choice_str(pl_csp_prim_names, p->color.primaries))},
{"gamma",
SUB_PROP_STR(m_opt_choice_str(mp_csp_trc_names, p->color.gamma))},
SUB_PROP_STR(m_opt_choice_str(pl_csp_trc_names, p->color.transfer))},
{"sig-peak", SUB_PROP_FLOAT(p->color.hdr.max_luma / MP_REF_WHITE)},
{"light",
SUB_PROP_STR(m_opt_choice_str(mp_csp_light_names, p->color.light))},
SUB_PROP_STR(m_opt_choice_str(mp_csp_light_names, p->light))},
{"chroma-location",
SUB_PROP_STR(m_opt_choice_str(mp_chroma_names, p->chroma_location))},
{"stereo-in",
Expand Down
4 changes: 2 additions & 2 deletions sub/draw_bmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ static bool reinit_to_video(struct mp_draw_sub_cache *p)
mp_get_regular_imgfmt(&vfdesc, mp_repack_get_format_dst(p->video_to_f32));
assert(vfdesc.num_planes); // must have succeeded

if (params->color.space == MP_CSP_RGB && vfdesc.num_planes >= 3) {
if (params->repr.sys == PL_COLOR_SYSTEM_RGB && vfdesc.num_planes >= 3) {
use_shortcut = true;

if (vfdesc.component_type == MP_COMPONENT_TYPE_UINT &&
Expand Down Expand Up @@ -724,7 +724,7 @@ static bool reinit_to_video(struct mp_draw_sub_cache *p)
p->alpha_overlay->stride[0] = p->video_overlay->stride[aplane];

// Full range gray always has the same range as alpha.
p->alpha_overlay->params.color.levels = MP_CSP_LEVELS_PC;
p->alpha_overlay->params.repr.levels = PL_COLOR_LEVELS_FULL;
mp_image_params_guess_csp(&p->alpha_overlay->params);

p->calpha_overlay =
Expand Down
66 changes: 33 additions & 33 deletions sub/sd_ass.c
Original file line number Diff line number Diff line change
Expand Up @@ -888,27 +888,27 @@ static void mangle_colors(struct sd *sd, struct sub_bitmaps *parts)
{
struct mp_subtitle_opts *opts = sd->opts;
struct sd_ass_priv *ctx = sd->priv;
enum mp_csp csp = 0;
enum mp_csp_levels levels = 0;
enum pl_color_system csp = 0;
enum pl_color_levels levels = 0;
if (opts->ass_vsfilter_color_compat == 0) // "no"
return;
bool force_601 = opts->ass_vsfilter_color_compat == 3;
ASS_Track *track = ctx->ass_track;
static const int ass_csp[] = {
[YCBCR_BT601_TV] = MP_CSP_BT_601,
[YCBCR_BT601_PC] = MP_CSP_BT_601,
[YCBCR_BT709_TV] = MP_CSP_BT_709,
[YCBCR_BT709_PC] = MP_CSP_BT_709,
[YCBCR_SMPTE240M_TV] = MP_CSP_SMPTE_240M,
[YCBCR_SMPTE240M_PC] = MP_CSP_SMPTE_240M,
[YCBCR_BT601_TV] = PL_COLOR_SYSTEM_BT_601,
[YCBCR_BT601_PC] = PL_COLOR_SYSTEM_BT_601,
[YCBCR_BT709_TV] = PL_COLOR_SYSTEM_BT_709,
[YCBCR_BT709_PC] = PL_COLOR_SYSTEM_BT_709,
[YCBCR_SMPTE240M_TV] = PL_COLOR_SYSTEM_SMPTE_240M,
[YCBCR_SMPTE240M_PC] = PL_COLOR_SYSTEM_SMPTE_240M,
};
static const int ass_levels[] = {
[YCBCR_BT601_TV] = MP_CSP_LEVELS_TV,
[YCBCR_BT601_PC] = MP_CSP_LEVELS_PC,
[YCBCR_BT709_TV] = MP_CSP_LEVELS_TV,
[YCBCR_BT709_PC] = MP_CSP_LEVELS_PC,
[YCBCR_SMPTE240M_TV] = MP_CSP_LEVELS_TV,
[YCBCR_SMPTE240M_PC] = MP_CSP_LEVELS_PC,
[YCBCR_BT601_TV] = PL_COLOR_LEVELS_LIMITED,
[YCBCR_BT601_PC] = PL_COLOR_LEVELS_FULL,
[YCBCR_BT709_TV] = PL_COLOR_LEVELS_LIMITED,
[YCBCR_BT709_PC] = PL_COLOR_LEVELS_FULL,
[YCBCR_SMPTE240M_TV] = PL_COLOR_LEVELS_LIMITED,
[YCBCR_SMPTE240M_PC] = PL_COLOR_LEVELS_FULL,
};
int trackcsp = track->YCbCrMatrix;
if (force_601)
Expand All @@ -921,8 +921,8 @@ static void mangle_colors(struct sd *sd, struct sub_bitmaps *parts)
if (trackcsp < sizeof(ass_levels) / sizeof(ass_levels[0]))
levels = ass_levels[trackcsp];
if (trackcsp == YCBCR_DEFAULT) {
csp = MP_CSP_BT_601;
levels = MP_CSP_LEVELS_TV;
csp = PL_COLOR_SYSTEM_BT_601;
levels = PL_COLOR_LEVELS_LIMITED;
}
// Unknown colorspace (either YCBCR_UNKNOWN, or a valid value unknown to us)
if (!csp || !levels)
Expand All @@ -931,42 +931,42 @@ static void mangle_colors(struct sd *sd, struct sub_bitmaps *parts)
struct mp_image_params params = ctx->video_params;

if (force_601) {
params.color = (struct mp_colorspace){
.space = MP_CSP_BT_709,
.levels = MP_CSP_LEVELS_TV,
params.repr = (struct pl_color_repr){
.sys = PL_COLOR_SYSTEM_BT_709,
.levels = PL_COLOR_LEVELS_LIMITED,
};
}

if ((csp == params.color.space && levels == params.color.levels) ||
params.color.space == MP_CSP_RGB) // Even VSFilter doesn't mangle on RGB video
if ((csp == params.repr.sys && levels == params.repr.levels) ||
params.repr.sys == PL_COLOR_SYSTEM_RGB) // Even VSFilter doesn't mangle on RGB video
return;

bool basic_conv = params.color.space == MP_CSP_BT_709 &&
params.color.levels == MP_CSP_LEVELS_TV &&
csp == MP_CSP_BT_601 &&
levels == MP_CSP_LEVELS_TV;
bool basic_conv = params.repr.sys == PL_COLOR_SYSTEM_BT_709 &&
params.repr.levels == PL_COLOR_LEVELS_LIMITED &&
csp == PL_COLOR_SYSTEM_BT_601 &&
levels == PL_COLOR_LEVELS_LIMITED;

// With "basic", only do as much as needed for basic compatibility.
if (opts->ass_vsfilter_color_compat == 1 && !basic_conv)
return;

if (params.color.space != ctx->last_params.color.space ||
params.color.levels != ctx->last_params.color.levels)
if (params.repr.sys != ctx->last_params.repr.sys ||
params.repr.levels != ctx->last_params.repr.levels)
{
int msgl = basic_conv ? MSGL_V : MSGL_WARN;
ctx->last_params = params;
MP_MSG(sd, msgl, "mangling colors like vsfilter: "
"RGB -> %s %s -> %s %s -> RGB\n",
m_opt_choice_str(mp_csp_names, csp),
m_opt_choice_str(mp_csp_levels_names, levels),
m_opt_choice_str(mp_csp_names, params.color.space),
m_opt_choice_str(mp_csp_names, params.color.levels));
m_opt_choice_str(pl_csp_names, csp),
m_opt_choice_str(pl_csp_levels_names, levels),
m_opt_choice_str(pl_csp_names, params.repr.sys),
m_opt_choice_str(pl_csp_names, params.repr.levels));
}

// Conversion that VSFilter would use
struct mp_csp_params vs_params = MP_CSP_PARAMS_DEFAULTS;
vs_params.color.space = csp;
vs_params.color.levels = levels;
vs_params.repr.sys = csp;
vs_params.repr.levels = levels;
struct mp_cmat vs_yuv2rgb, vs_rgb2yuv;
mp_get_csp_matrix(&vs_params, &vs_yuv2rgb);
mp_invert_cmat(&vs_rgb2yuv, &vs_yuv2rgb);
Expand Down
2 changes: 1 addition & 1 deletion test/img_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ int main(int argc, char *argv[])

int fcsp = mp_imgfmt_get_forced_csp(mpfmt);
if (fcsp)
fprintf(f, "fcsp=%s ", m_opt_choice_str(mp_csp_names, fcsp));
fprintf(f, "fcsp=%s ", m_opt_choice_str(pl_csp_names, fcsp));
fprintf(f, "ctype=%s\n", comp_type(mp_imgfmt_get_component_type(mpfmt)));

struct mp_imgfmt_desc d = mp_imgfmt_get_desc(mpfmt);
Expand Down
32 changes: 17 additions & 15 deletions test/repack.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ static int try_repack(FILE *f, int imgfmt, int flags, int not_if_fmt)
return b;
}

static void check_float_repack(int imgfmt, enum mp_csp csp,
enum mp_csp_levels levels)
static void check_float_repack(int imgfmt, enum pl_color_system csp,
enum pl_color_levels levels)
{
imgfmt = UNFUCK(imgfmt);

Expand All @@ -349,12 +349,12 @@ static void check_float_repack(int imgfmt, enum mp_csp csp,
struct mp_image *src = mp_image_alloc(imgfmt, w, 1);
assert(src);

src->params.color.space = csp;
src->params.color.levels = levels;
src->params.repr.sys = csp;
src->params.repr.levels = levels;
mp_image_params_guess_csp(&src->params);
// mpv may not allow all combinations
assert(src->params.color.space == csp);
assert(src->params.color.levels == levels);
assert(src->params.repr.sys == csp);
assert(src->params.repr.levels == levels);

for (int p = 0; p < src->num_planes; p++) {
int val = 0;
Expand Down Expand Up @@ -384,6 +384,8 @@ static void check_float_repack(int imgfmt, enum mp_csp csp,

z_f->params.color = r_f->params.color = z_i->params.color =
r_i->params.color = src->params.color;
z_f->params.repr = r_f->params.repr = z_i->params.repr =
r_i->params.repr = src->params.repr;

// The idea is to use zimg to cross-check conversion.
struct mp_sws_context *s = mp_sws_alloc(NULL);
Expand Down Expand Up @@ -503,15 +505,15 @@ int main(int argc, char *argv[])
assert_text_files_equal(refdir, outdir, "repack.txt",
"This can fail if FFmpeg/libswscale adds or removes pixfmts.");

check_float_repack(-AV_PIX_FMT_GBRAP, MP_CSP_RGB, MP_CSP_LEVELS_PC);
check_float_repack(-AV_PIX_FMT_GBRAP10, MP_CSP_RGB, MP_CSP_LEVELS_PC);
check_float_repack(-AV_PIX_FMT_GBRAP16, MP_CSP_RGB, MP_CSP_LEVELS_PC);
check_float_repack(-AV_PIX_FMT_YUVA444P, MP_CSP_BT_709, MP_CSP_LEVELS_PC);
check_float_repack(-AV_PIX_FMT_YUVA444P, MP_CSP_BT_709, MP_CSP_LEVELS_TV);
check_float_repack(-AV_PIX_FMT_YUVA444P10, MP_CSP_BT_709, MP_CSP_LEVELS_PC);
check_float_repack(-AV_PIX_FMT_YUVA444P10, MP_CSP_BT_709, MP_CSP_LEVELS_TV);
check_float_repack(-AV_PIX_FMT_YUVA444P16, MP_CSP_BT_709, MP_CSP_LEVELS_PC);
check_float_repack(-AV_PIX_FMT_YUVA444P16, MP_CSP_BT_709, MP_CSP_LEVELS_TV);
check_float_repack(-AV_PIX_FMT_GBRAP, PL_COLOR_SYSTEM_RGB, PL_COLOR_LEVELS_FULL);
check_float_repack(-AV_PIX_FMT_GBRAP10, PL_COLOR_SYSTEM_RGB, PL_COLOR_LEVELS_FULL);
check_float_repack(-AV_PIX_FMT_GBRAP16, PL_COLOR_SYSTEM_RGB, PL_COLOR_LEVELS_FULL);
check_float_repack(-AV_PIX_FMT_YUVA444P, PL_COLOR_SYSTEM_BT_709, PL_COLOR_LEVELS_FULL);
check_float_repack(-AV_PIX_FMT_YUVA444P, PL_COLOR_SYSTEM_BT_709, PL_COLOR_LEVELS_LIMITED);
check_float_repack(-AV_PIX_FMT_YUVA444P10, PL_COLOR_SYSTEM_BT_709, PL_COLOR_LEVELS_FULL);
check_float_repack(-AV_PIX_FMT_YUVA444P10, PL_COLOR_SYSTEM_BT_709, PL_COLOR_LEVELS_LIMITED);
check_float_repack(-AV_PIX_FMT_YUVA444P16, PL_COLOR_SYSTEM_BT_709, PL_COLOR_LEVELS_FULL);
check_float_repack(-AV_PIX_FMT_YUVA444P16, PL_COLOR_SYSTEM_BT_709, PL_COLOR_LEVELS_LIMITED);

// Determine the list of possible draw_bmp input formats. Do this here
// because it mostly depends on repack and imgformat stuff.
Expand Down
4 changes: 2 additions & 2 deletions test/scale_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ static struct mp_image *gen_repack_test_img(int w, int h, int bytes, bool rgb,
struct mp_regular_imgfmt planar_desc = {
.component_type = MP_COMPONENT_TYPE_UINT,
.component_size = bytes,
.forced_csp = rgb ? MP_CSP_RGB : 0,
.forced_csp = rgb ? PL_COLOR_SYSTEM_RGB : 0,
.num_planes = alpha ? 4 : 3,
.planes = {
{1, {rgb ? 2 : 1}},
Expand Down Expand Up @@ -129,7 +129,7 @@ void repack_test_run(struct scale_test *stest)
if (!mp_get_regular_imgfmt(&rdesc, ofmt))
continue;
}
if (rdesc.num_planes > 1 || rdesc.forced_csp != MP_CSP_RGB)
if (rdesc.num_planes > 1 || rdesc.forced_csp != PL_COLOR_SYSTEM_RGB)
continue;

struct mp_image *test_img = NULL;
Expand Down
Loading

0 comments on commit 66e451f

Please sign in to comment.