Move to 'using namespace std;' in all .cpp files.
[movit] / ycbcr_input.cpp
1 #include <Eigen/Core>
2 #include <Eigen/LU>
3 #include <GL/glew.h>
4 #include <assert.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 #include "effect_util.h"
9 #include "resource_pool.h"
10 #include "util.h"
11 #include "ycbcr_input.h"
12
13 using namespace Eigen;
14 using namespace std;
15
16 namespace {
17
18 // OpenGL has texel center in (0.5, 0.5), but different formats have
19 // chroma in various other places. If luma samples are X, the chroma
20 // sample is *, and subsampling is 3x3, the situation with chroma
21 // center in (0.5, 0.5) looks approximately like this:
22 //
23 //   X   X
24 //     *   
25 //   X   X
26 //
27 // If, on the other hand, chroma center is in (0.0, 0.5) (common
28 // for e.g. MPEG-4), the figure changes to:
29 //
30 //   X   X
31 //   *      
32 //   X   X
33 //
34 // In other words, (0.0, 0.0) means that the chroma sample is exactly
35 // co-sited on top of the top-left luma sample. Note, however, that
36 // this is _not_ 0.5 texels to the left, since the OpenGL's texel center
37 // is in (0.5, 0.5); it is in (0.25, 0.25). In a sense, the four luma samples
38 // define a square where chroma position (0.0, 0.0) is in texel position
39 // (0.25, 0.25) and chroma position (1.0, 1.0) is in texel position (0.75, 0.75)
40 // (the outer border shows the borders of the texel itself, ie. from
41 // (0, 0) to (1, 1)):
42 //
43 //  ---------
44 // |         |
45 // |  X---X  |
46 // |  | * |  |
47 // |  X---X  |
48 // |         |
49 //  ---------
50 //
51 // Also note that if we have no subsampling, the square will have zero
52 // area and the chroma position does not matter at all.
53 float compute_chroma_offset(float pos, unsigned subsampling_factor, unsigned resolution)
54 {
55         float local_chroma_pos = (0.5 + pos * (subsampling_factor - 1)) / subsampling_factor;
56         return (0.5 - local_chroma_pos) / resolution;
57 }
58
59 }  // namespace
60
61 YCbCrInput::YCbCrInput(const ImageFormat &image_format,
62                        const YCbCrFormat &ycbcr_format,
63                        unsigned width, unsigned height)
64         : image_format(image_format),
65           ycbcr_format(ycbcr_format),
66           finalized(false),
67           needs_mipmaps(false),
68           width(width),
69           height(height),
70           resource_pool(NULL)
71 {
72         pbos[0] = pbos[1] = pbos[2] = 0;
73         texture_num[0] = texture_num[1] = texture_num[2] = 0;
74
75         assert(width % ycbcr_format.chroma_subsampling_x == 0);
76         pitch[0] = widths[0] = width;
77         pitch[1] = widths[1] = width / ycbcr_format.chroma_subsampling_x;
78         pitch[2] = widths[2] = width / ycbcr_format.chroma_subsampling_x;
79
80         assert(height % ycbcr_format.chroma_subsampling_y == 0);
81         heights[0] = height;
82         heights[1] = height / ycbcr_format.chroma_subsampling_y;
83         heights[2] = height / ycbcr_format.chroma_subsampling_y;
84
85         pixel_data[0] = pixel_data[1] = pixel_data[2] = NULL;
86
87         register_int("needs_mipmaps", &needs_mipmaps);
88 }
89
90 YCbCrInput::~YCbCrInput()
91 {
92         for (unsigned channel = 0; channel < 3; ++channel) {
93                 if (texture_num[channel] != 0) {
94                         resource_pool->release_2d_texture(texture_num[channel]);
95                 }
96         }
97 }
98
99 void YCbCrInput::finalize()
100 {
101         finalized = true;
102 }
103         
104 void YCbCrInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
105 {
106         for (unsigned channel = 0; channel < 3; ++channel) {
107                 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
108                 check_error();
109
110                 if (texture_num[channel] == 0) {
111                         // (Re-)upload the texture.
112                         texture_num[channel] = resource_pool->create_2d_texture(GL_LUMINANCE8, widths[channel], heights[channel]);
113                         glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
114                         check_error();
115                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
116                         check_error();
117                         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
118                         check_error();
119                         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
120                         check_error();
121                         glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
122                         check_error();
123                         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], heights[channel], GL_LUMINANCE, GL_UNSIGNED_BYTE, pixel_data[channel]);
124                         check_error();
125                         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
126                         check_error();
127                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
128                         check_error();
129                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
130                         check_error();
131                 } else {
132                         glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
133                         check_error();
134                 }
135         }
136
137         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
138         check_error();
139
140         // Bind samplers.
141         set_uniform_int(glsl_program_num, prefix, "tex_y", *sampler_num + 0);
142         set_uniform_int(glsl_program_num, prefix, "tex_cb", *sampler_num + 1);
143         set_uniform_int(glsl_program_num, prefix, "tex_cr", *sampler_num + 2);
144
145         *sampler_num += 3;
146 }
147
148 string YCbCrInput::output_fragment_shader()
149 {
150         float coeff[3], offset[3], scale[3];
151
152         switch (ycbcr_format.luma_coefficients) {
153         case YCBCR_REC_601:
154                 // Rec. 601, page 2.
155                 coeff[0] = 0.299;
156                 coeff[1] = 0.587;
157                 coeff[2] = 0.114;
158                 break;
159
160         case YCBCR_REC_709:
161                 // Rec. 709, page 19.
162                 coeff[0] = 0.2126;
163                 coeff[1] = 0.7152;
164                 coeff[2] = 0.0722;
165                 break;
166
167         case YCBCR_REC_2020:
168                 // Rec. 2020, page 4.
169                 coeff[0] = 0.2627;
170                 coeff[1] = 0.6780;
171                 coeff[2] = 0.0593;
172                 break;
173
174         default:
175                 assert(false);
176         }
177
178         if (ycbcr_format.full_range) {
179                 offset[0] = 0.0 / 255.0;
180                 offset[1] = 128.0 / 255.0;
181                 offset[2] = 128.0 / 255.0;
182
183                 scale[0] = 1.0;
184                 scale[1] = 1.0;
185                 scale[2] = 1.0;
186         } else {
187                 // Rec. 601, page 4; Rec. 709, page 19; Rec. 2020, page 4.
188                 offset[0] = 16.0 / 255.0;
189                 offset[1] = 128.0 / 255.0;
190                 offset[2] = 128.0 / 255.0;
191
192                 scale[0] = 255.0 / 219.0;
193                 scale[1] = 255.0 / 224.0;
194                 scale[2] = 255.0 / 224.0;
195         }
196
197         // Matrix to convert RGB to YCbCr. See e.g. Rec. 601.
198         Matrix3d rgb_to_ycbcr;
199         rgb_to_ycbcr(0,0) = coeff[0];
200         rgb_to_ycbcr(0,1) = coeff[1];
201         rgb_to_ycbcr(0,2) = coeff[2];
202
203         float cb_fac = (224.0 / 219.0) / (coeff[0] + coeff[1] + 1.0f - coeff[2]);
204         rgb_to_ycbcr(1,0) = -coeff[0] * cb_fac;
205         rgb_to_ycbcr(1,1) = -coeff[1] * cb_fac;
206         rgb_to_ycbcr(1,2) = (1.0f - coeff[2]) * cb_fac;
207
208         float cr_fac = (224.0 / 219.0) / (1.0f - coeff[0] + coeff[1] + coeff[2]);
209         rgb_to_ycbcr(2,0) = (1.0f - coeff[0]) * cr_fac;
210         rgb_to_ycbcr(2,1) = -coeff[1] * cr_fac;
211         rgb_to_ycbcr(2,2) = -coeff[2] * cr_fac;
212
213         // Inverting the matrix gives us what we need to go from YCbCr back to RGB.
214         Matrix3d ycbcr_to_rgb = rgb_to_ycbcr.inverse();
215
216         string frag_shader;
217
218         frag_shader = output_glsl_mat3("PREFIX(inv_ycbcr_matrix)", ycbcr_to_rgb);
219
220         char buf[256];
221         sprintf(buf, "const vec3 PREFIX(offset) = vec3(%.8f, %.8f, %.8f);\n",
222                 offset[0], offset[1], offset[2]);
223         frag_shader += buf;
224
225         sprintf(buf, "const vec3 PREFIX(scale) = vec3(%.8f, %.8f, %.8f);\n",
226                 scale[0], scale[1], scale[2]);
227         frag_shader += buf;
228
229         float cb_offset_x = compute_chroma_offset(
230                 ycbcr_format.cb_x_position, ycbcr_format.chroma_subsampling_x, widths[1]);
231         float cb_offset_y = compute_chroma_offset(
232                 ycbcr_format.cb_y_position, ycbcr_format.chroma_subsampling_y, heights[1]);
233         sprintf(buf, "const vec2 PREFIX(cb_offset) = vec2(%.8f, %.8f);\n",
234                 cb_offset_x, cb_offset_y);
235         frag_shader += buf;
236
237         float cr_offset_x = compute_chroma_offset(
238                 ycbcr_format.cr_x_position, ycbcr_format.chroma_subsampling_x, widths[2]);
239         float cr_offset_y = compute_chroma_offset(
240                 ycbcr_format.cr_y_position, ycbcr_format.chroma_subsampling_y, heights[2]);
241         sprintf(buf, "const vec2 PREFIX(cr_offset) = vec2(%.8f, %.8f);\n",
242                 cr_offset_x, cr_offset_y);
243         frag_shader += buf;
244
245         frag_shader += read_file("ycbcr_input.frag");
246         return frag_shader;
247 }
248
249 void YCbCrInput::invalidate_pixel_data()
250 {
251         for (unsigned channel = 0; channel < 3; ++channel) {
252                 if (texture_num[channel] != 0) {
253                         resource_pool->release_2d_texture(texture_num[channel]);
254                         texture_num[channel] = 0;
255                 }
256         }
257 }