Skip to content

Commit

Permalink
support conversions with BT.601 chromicities
Browse files Browse the repository at this point in the history
Added experimental support for conversions with BT.601 primaries. The
command-line interface will most likely change.
  • Loading branch information
MartinPulec committed Sep 27, 2024
1 parent 4c17aa9 commit cf6acad
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
34 changes: 24 additions & 10 deletions src/color.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <stdio.h> // for fprintf, stderr
#include <stdlib.h> // for abort

#include "host.h" // for ADD_TO_PARAM
#include "types.h" // for depth

#define D(kr, kb) (2. * ((kr) + KG(kr, kb)))
Expand Down Expand Up @@ -91,8 +92,11 @@
SCALED(B_CB(depth, kr, kb)), \
}

#define COEFFS_601(depth) COEFFS(depth, KR_601, KB_601)
#define COEFFS_709(depth) COEFFS(depth, KR_709, KB_709)

ADD_TO_PARAM("color-601", "* color-601\n"
" Use BT.601 color primaries.\n");
/**
* @brief returns color coefficient for RGB<-YCbCr conversion
*
Expand All @@ -109,21 +113,31 @@
const struct color_coeffs *
get_color_coeffs(int ycbcr_bit_depth)
{
static const struct color_coeffs col_cfs_8 = COEFFS_709(DEPTH8);
static const struct color_coeffs col_cfs_10 = COEFFS_709(DEPTH10);
static const struct color_coeffs col_cfs_12 = COEFFS_709(DEPTH12);
static const struct color_coeffs col_cfs_16 = COEFFS_709(DEPTH16);
static _Atomic int primaries_index = -1;
if (primaries_index == -1) {
primaries_index =
get_commandline_param("color-601") != NULL ? 0 : 1;
}

static const struct {
struct color_coeffs col_cfs[2];
} coeffs[] = {
{ { COEFFS_601(DEPTH8), COEFFS_709(DEPTH8) } },
{ { COEFFS_601(DEPTH10), COEFFS_709(DEPTH10) } },
{ { COEFFS_601(DEPTH12), COEFFS_709(DEPTH12) } },
{ { COEFFS_601(DEPTH16), COEFFS_709(DEPTH16) } }
};
switch ((enum depth) ycbcr_bit_depth) {
case DEPTH8:
return &col_cfs_8;
return &coeffs[0].col_cfs[primaries_index];
case DEPTH10:
return &col_cfs_10;
return &coeffs[1].col_cfs[primaries_index];
case DEPTH12:
return &col_cfs_12;
return &coeffs[2].col_cfs[primaries_index];
case DEPTH16:
return &col_cfs_16;
return &coeffs[3].col_cfs[primaries_index];
}
fprintf(stderr, "%s: Wrong depth %d!\n", __func__,
ycbcr_bit_depth);

fprintf(stderr, "%s: Wrong depth %d!\n", __func__, ycbcr_bit_depth);
abort();
}
10 changes: 8 additions & 2 deletions src/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ static_assert(sizeof(comp_type_t) * 8 >= COMP_BASE + 18, "comp_type_t not wide e
(224. * (1 << ((out_depth) - 8)) / ((1 << (out_depth)) - 1))
#endif // !defined YCBCR_FULL

#define KR_601 .299
#define KB_601 .114
#define KR_709 .212639
#define KB_709 .072192
#define KR_2020 .262700
Expand Down Expand Up @@ -152,14 +154,18 @@ static_assert(sizeof(comp_type_t) * 8 >= COMP_BASE + 18, "comp_type_t not wide e
extern "C" {
#endif

// new API
struct color_coeffs {
// shorts are used the compiler can use 2-byte words in the vecotred
// instruction, which is faster (and the values fit)
short y_r, y_g, y_b;
short cb_r, cb_g, cb_b;
short cr_r, cr_g, cr_b;

// the shorts below doesn't seem to be necessary - it seems like the
// compiler doesn't vectorise those conversions (in contrary to the
// above coeffs)
short r_cr, g_cb, g_cr;
int b_cb; // b_cb is 34712 so doesn't fit to 16-bit short
int b_cb; // is 34712 for 709 so doesn't fit to 16-bit short
};
const struct color_coeffs *get_color_coeffs(int ycbcr_bit_depth);
#ifdef __cplusplus
Expand Down
6 changes: 5 additions & 1 deletion src/video_display/gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,11 @@ static GLuint gl_substitute_compile_link(const char *vprogram, const char *fprog
LOG(LOG_LEVEL_WARNING) << MOD_NAME "Wrong chromicities index " << color << "\n";
}
}
double cs_coeffs[2*4] = { 0, 0, KR_709, KB_709, KR_2020, KB_2020, KR_P3, KB_P3 };
if (get_commandline_param("color-601") != nullptr) {
index = 0;
}
double cs_coeffs[2 * 4] = { KR_601, KB_601, KR_709, KB_709,
KR_2020, KB_2020, KR_P3, KB_P3 };
double kr = cs_coeffs[2 * index];
double kb = cs_coeffs[2 * index + 1];
const char *placeholders[] = { "Y_SCALED_PLACEHOLDER", "R_CR_PLACEHOLDER", "G_CB_PLACEHOLDER", "G_CR_PLACEHOLDER", "B_CB_PLACEHOLDER" };
Expand Down

0 comments on commit cf6acad

Please sign in to comment.