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