]> git.sesse.net Git - movit/blob - ycbcr_conversion_effect.cpp
Small fix in EffectChainTester constructor.
[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, GLenum type)
19         : ycbcr_format(ycbcr_format), type(type)
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         double scale_factor;
41         compute_ycbcr_matrix(ycbcr_format, uniform_offset, &ycbcr_to_rgb, type, &scale_factor);
42
43         uniform_ycbcr_matrix = ycbcr_to_rgb.inverse();
44
45         if (ycbcr_format.full_range) {
46                 // The card will clamp for us later.
47                 uniform_clamp_range = false;
48         } else {
49                 uniform_clamp_range = true;
50
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.
68                         // BT.2020, page 6.
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;
75                 } else {
76                         assert(false);
77                 }
78                 uniform_ycbcr_min[0] /= scale_factor;
79                 uniform_ycbcr_min[1] /= scale_factor;
80                 uniform_ycbcr_min[2] /= scale_factor;
81         }
82 }
83
84 }  // namespace movit