8 #include "ycbcr_conversion_effect.h"
9 #include "effect_util.h"
14 using namespace Eigen;
18 YCbCrConversionEffect::YCbCrConversionEffect(const YCbCrFormat &ycbcr_format, GLenum type)
19 : ycbcr_format(ycbcr_format), type(type)
21 register_uniform_mat3("ycbcr_matrix", &uniform_ycbcr_matrix);
22 register_uniform_vec3("offset", uniform_offset);
23 register_uniform_bool("clamp_range", &uniform_clamp_range);
25 // Only used when clamp_range is true.
26 register_uniform_vec3("ycbcr_min", uniform_ycbcr_min);
27 register_uniform_vec3("ycbcr_max", uniform_ycbcr_max);
30 string YCbCrConversionEffect::output_fragment_shader()
32 return read_file("ycbcr_conversion_effect.frag");
35 void YCbCrConversionEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
37 Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
39 Matrix3d ycbcr_to_rgb;
41 compute_ycbcr_matrix(ycbcr_format, uniform_offset, &ycbcr_to_rgb, type, &scale_factor);
43 uniform_ycbcr_matrix = ycbcr_to_rgb.inverse();
45 if (ycbcr_format.full_range) {
46 // The card will clamp for us later.
47 uniform_clamp_range = false;
49 uniform_clamp_range = true;
51 if (ycbcr_format.num_levels == 0 || ycbcr_format.num_levels == 256) { // 8-bit.
52 // These limits come from BT.601 page 8, or BT.709, page 5.
53 uniform_ycbcr_min[0] = 16.0 / 255.0;
54 uniform_ycbcr_min[1] = 16.0 / 255.0;
55 uniform_ycbcr_min[2] = 16.0 / 255.0;
56 uniform_ycbcr_max[0] = 235.0 / 255.0;
57 uniform_ycbcr_max[1] = 240.0 / 255.0;
58 uniform_ycbcr_max[2] = 240.0 / 255.0;
59 } else if (ycbcr_format.num_levels == 1024) { // 10-bit.
60 // BT.709, page 5, or BT.2020, page 6.
61 uniform_ycbcr_min[0] = 64.0 / 1023.0;
62 uniform_ycbcr_min[1] = 64.0 / 1023.0;
63 uniform_ycbcr_min[2] = 64.0 / 1023.0;
64 uniform_ycbcr_max[0] = 940.0 / 1023.0;
65 uniform_ycbcr_max[1] = 960.0 / 1023.0;
66 uniform_ycbcr_max[2] = 960.0 / 1023.0;
67 } else if (ycbcr_format.num_levels == 4096) { // 12-bit.
69 uniform_ycbcr_min[0] = 256.0 / 4095.0;
70 uniform_ycbcr_min[1] = 256.0 / 4095.0;
71 uniform_ycbcr_min[2] = 256.0 / 4095.0;
72 uniform_ycbcr_max[0] = 3760.0 / 4095.0;
73 uniform_ycbcr_max[1] = 3840.0 / 4095.0;
74 uniform_ycbcr_max[2] = 3840.0 / 4095.0;
78 uniform_ycbcr_min[0] /= scale_factor;
79 uniform_ycbcr_min[1] /= scale_factor;
80 uniform_ycbcr_min[2] /= scale_factor;