namespace movit {
-YCbCrConversionEffect::YCbCrConversionEffect(const YCbCrFormat &ycbcr_format)
- : ycbcr_format(ycbcr_format)
+YCbCrConversionEffect::YCbCrConversionEffect(const YCbCrFormat &ycbcr_format, GLenum type)
+ : ycbcr_format(ycbcr_format), type(type)
{
+ register_uniform_mat3("ycbcr_matrix", &uniform_ycbcr_matrix);
+ register_uniform_vec3("offset", uniform_offset);
+ register_uniform_bool("clamp_range", &uniform_clamp_range);
+
+ // Only used when clamp_range is true.
+ register_uniform_vec3("ycbcr_min", uniform_ycbcr_min);
+ register_uniform_vec3("ycbcr_max", uniform_ycbcr_max);
}
string YCbCrConversionEffect::output_fragment_shader()
{
- float offset[3];
+ return read_file("ycbcr_conversion_effect.frag");
+}
+
+void YCbCrConversionEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
+{
+ Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
+
Matrix3d ycbcr_to_rgb;
- compute_ycbcr_matrix(ycbcr_format, offset, &ycbcr_to_rgb);
+ double scale_factor;
+ compute_ycbcr_matrix(ycbcr_format, uniform_offset, &ycbcr_to_rgb, type, &scale_factor);
- string frag_shader = output_glsl_mat3("PREFIX(ycbcr_matrix)", ycbcr_to_rgb.inverse());
- frag_shader += output_glsl_vec3("PREFIX(offset)", offset[0], offset[1], offset[2]);
+ uniform_ycbcr_matrix = ycbcr_to_rgb.inverse();
if (ycbcr_format.full_range) {
// The card will clamp for us later.
- frag_shader += "#define YCBCR_CLAMP_RANGE 0\n";
+ uniform_clamp_range = false;
} else {
- frag_shader += "#define YCBCR_CLAMP_RANGE 1\n";
+ uniform_clamp_range = true;
- // These limits come from BT.601 page 8, or BT.701, page 5.
- // TODO: Use num_levels. Currently we support 8-bit levels only.
- frag_shader += output_glsl_vec3("PREFIX(ycbcr_min)", 16.0 / 255.0, 16.0 / 255.0, 16.0 / 255.0);
- frag_shader += output_glsl_vec3("PREFIX(ycbcr_max)", 235.0 / 255.0, 240.0 / 255.0, 240.0 / 255.0);
+ if (ycbcr_format.num_levels == 0 || ycbcr_format.num_levels == 256) { // 8-bit.
+ // These limits come from BT.601 page 8, or BT.709, page 5.
+ uniform_ycbcr_min[0] = 16.0 / 255.0;
+ uniform_ycbcr_min[1] = 16.0 / 255.0;
+ uniform_ycbcr_min[2] = 16.0 / 255.0;
+ uniform_ycbcr_max[0] = 235.0 / 255.0;
+ uniform_ycbcr_max[1] = 240.0 / 255.0;
+ uniform_ycbcr_max[2] = 240.0 / 255.0;
+ } else if (ycbcr_format.num_levels == 1024) { // 10-bit.
+ // BT.709, page 5, or BT.2020, page 6.
+ uniform_ycbcr_min[0] = 64.0 / 1023.0;
+ uniform_ycbcr_min[1] = 64.0 / 1023.0;
+ uniform_ycbcr_min[2] = 64.0 / 1023.0;
+ uniform_ycbcr_max[0] = 940.0 / 1023.0;
+ uniform_ycbcr_max[1] = 960.0 / 1023.0;
+ uniform_ycbcr_max[2] = 960.0 / 1023.0;
+ } else if (ycbcr_format.num_levels == 4096) { // 12-bit.
+ // BT.2020, page 6.
+ uniform_ycbcr_min[0] = 256.0 / 4095.0;
+ uniform_ycbcr_min[1] = 256.0 / 4095.0;
+ uniform_ycbcr_min[2] = 256.0 / 4095.0;
+ uniform_ycbcr_max[0] = 3760.0 / 4095.0;
+ uniform_ycbcr_max[1] = 3840.0 / 4095.0;
+ uniform_ycbcr_max[2] = 3840.0 / 4095.0;
+ } else {
+ assert(false);
+ }
+ uniform_ycbcr_min[0] /= scale_factor;
+ uniform_ycbcr_min[1] /= scale_factor;
+ uniform_ycbcr_min[2] /= scale_factor;
}
-
- return frag_shader + read_file("ycbcr_conversion_effect.frag");
}
} // namespace movit