]> git.sesse.net Git - movit/blob - ycbcr_conversion_effect.cpp
Allow adjusting the output Y'CbCr coefficients after finalize.
[movit] / ycbcr_conversion_effect.cpp
1 #include <epoxy/gl.h>
2 #include <assert.h>
3 #include <stdio.h>
4 #include <algorithm>
5 #include <Eigen/Core>
6 #include <Eigen/LU>
7
8 #include "ycbcr_conversion_effect.h"
9 #include "effect_util.h"
10 #include "util.h"
11 #include "ycbcr.h"
12
13 using namespace std;
14 using namespace Eigen;
15
16 namespace movit {
17
18 YCbCrConversionEffect::YCbCrConversionEffect(const YCbCrFormat &ycbcr_format)
19         : ycbcr_format(ycbcr_format)
20 {
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);
24
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);
28 }
29
30 string YCbCrConversionEffect::output_fragment_shader()
31 {
32         return read_file("ycbcr_conversion_effect.frag");
33 }
34
35 void YCbCrConversionEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
36 {
37         Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
38
39         Matrix3d ycbcr_to_rgb;
40         compute_ycbcr_matrix(ycbcr_format, uniform_offset, &ycbcr_to_rgb);
41
42         uniform_ycbcr_matrix = ycbcr_to_rgb.inverse();
43
44         if (ycbcr_format.full_range) {
45                 // The card will clamp for us later.
46                 uniform_clamp_range = false;
47         } else {
48                 uniform_clamp_range = true;
49
50                 // These limits come from BT.601 page 8, or BT.701, page 5.
51                 // TODO: Use num_levels. Currently we support 8-bit levels only.
52                 uniform_ycbcr_min[0] = 16.0 / 255.0;
53                 uniform_ycbcr_min[1] = 16.0 / 255.0;
54                 uniform_ycbcr_min[2] = 16.0 / 255.0;
55                 uniform_ycbcr_max[0] = 235.0 / 255.0;
56                 uniform_ycbcr_max[1] = 240.0 / 255.0;
57                 uniform_ycbcr_max[2] = 240.0 / 255.0;
58         }
59 }
60
61 }  // namespace movit