Support 10- and 12-bit Y'CbCr output.
[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                 if (ycbcr_format.num_levels == 256) {  // 8-bit.
51                         // These limits come from BT.601 page 8, or BT.709, page 5.
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                 } else if (ycbcr_format.num_levels == 1024) {  // 10-bit.
59                         // BT.709, page 5, or BT.2020, page 6.
60                         uniform_ycbcr_min[0] = 64.0 / 1023.0;
61                         uniform_ycbcr_min[1] = 64.0 / 1023.0;
62                         uniform_ycbcr_min[2] = 64.0 / 1023.0;
63                         uniform_ycbcr_max[0] = 940.0 / 1023.0;
64                         uniform_ycbcr_max[1] = 960.0 / 1023.0;
65                         uniform_ycbcr_max[2] = 960.0 / 1023.0;
66                 } else if (ycbcr_format.num_levels == 4096) {  // 12-bit.
67                         // BT.2020, page 6.
68                         uniform_ycbcr_min[0] = 256.0 / 4095.0;
69                         uniform_ycbcr_min[1] = 256.0 / 4095.0;
70                         uniform_ycbcr_min[2] = 256.0 / 4095.0;
71                         uniform_ycbcr_max[0] = 3760.0 / 4095.0;
72                         uniform_ycbcr_max[1] = 3840.0 / 4095.0;
73                         uniform_ycbcr_max[2] = 3840.0 / 4095.0;
74                 } else {
75                         assert(false);
76                 }
77         }
78 }
79
80 }  // namespace movit