8 #include "effect_util.h"
10 #include "ycbcr_input.h"
12 using namespace Eigen;
16 // OpenGL has texel center in (0.5, 0.5), but different formats have
17 // chroma in various other places. If luma samples are X, the chroma
18 // sample is *, and subsampling is 3x3, the situation with chroma
19 // center in (0.5, 0.5) looks approximately like this:
25 // If, on the other hand, chroma center is in (0.0, 0.5) (common
26 // for e.g. MPEG-4), the figure changes to:
32 // In other words, (0.0, 0.0) means that the chroma sample is exactly
33 // co-sited on top of the top-left luma sample. Note, however, that
34 // this is _not_ 0.5 texels to the left, since the OpenGL's texel center
35 // is in (0.5, 0.5); it is in (0.25, 0.25). In a sense, the four luma samples
36 // define a square where chroma position (0.0, 0.0) is in texel position
37 // (0.25, 0.25) and chroma position (1.0, 1.0) is in texel position (0.75, 0.75)
38 // (the outer border shows the borders of the texel itself, ie. from
49 // Also note that if we have no subsampling, the square will have zero
50 // area and the chroma position does not matter at all.
51 float compute_chroma_offset(float pos, unsigned subsampling_factor, unsigned resolution)
53 float local_chroma_pos = (0.5 + pos * (subsampling_factor - 1)) / subsampling_factor;
54 return (0.5 - local_chroma_pos) / resolution;
59 YCbCrInput::YCbCrInput(const ImageFormat &image_format,
60 const YCbCrFormat &ycbcr_format,
61 unsigned width, unsigned height)
62 : image_format(image_format),
63 ycbcr_format(ycbcr_format),
65 needs_pbo_recreate(false),
71 pbos[0] = pbos[1] = pbos[2] = 0;
72 texture_num[0] = texture_num[1] = texture_num[2] = 0;
74 assert(width % ycbcr_format.chroma_subsampling_x == 0);
75 pitch[0] = widths[0] = width;
76 pitch[1] = widths[1] = width / ycbcr_format.chroma_subsampling_x;
77 pitch[2] = widths[2] = width / ycbcr_format.chroma_subsampling_x;
79 assert(height % ycbcr_format.chroma_subsampling_y == 0);
81 heights[1] = height / ycbcr_format.chroma_subsampling_y;
82 heights[2] = height / ycbcr_format.chroma_subsampling_y;
84 pixel_data[0] = pixel_data[1] = pixel_data[2] = NULL;
86 register_int("needs_mipmaps", &needs_mipmaps);
89 YCbCrInput::~YCbCrInput()
92 glDeleteBuffers(3, pbos);
95 if (texture_num[0] != 0) {
96 glDeleteTextures(3, texture_num);
101 void YCbCrInput::finalize()
103 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
106 // Create PBOs to hold the textures holding the input image, and then the texture itself.
107 glGenBuffers(3, pbos);
109 glGenTextures(3, texture_num);
112 for (unsigned channel = 0; channel < 3; ++channel) {
113 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
115 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, pitch[channel] * heights[channel], NULL, GL_STREAM_DRAW);
117 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
120 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
122 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
124 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
126 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE8, widths[channel], heights[channel], 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL);
128 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
136 void YCbCrInput::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
138 for (unsigned channel = 0; channel < 3; ++channel) {
139 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
141 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
144 if (needs_update || needs_pbo_recreate) {
145 // Copy the pixel data into the PBO.
146 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
149 if (needs_pbo_recreate) {
150 // The pitch has changed; we need to reallocate this PBO.
151 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, pitch[channel] * heights[channel], NULL, GL_STREAM_DRAW);
155 void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
156 memcpy(mapped_pbo, pixel_data[channel], pitch[channel] * heights[channel]);
158 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
161 // Re-upload the texture from the PBO.
162 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
164 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], heights[channel], GL_LUMINANCE, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
166 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
168 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
170 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
172 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
178 set_uniform_int(glsl_program_num, prefix, "tex_y", *sampler_num + 0);
179 set_uniform_int(glsl_program_num, prefix, "tex_cb", *sampler_num + 1);
180 set_uniform_int(glsl_program_num, prefix, "tex_cr", *sampler_num + 2);
183 needs_update = false;
184 needs_pbo_recreate = false;
187 std::string YCbCrInput::output_fragment_shader()
189 float coeff[3], offset[3], scale[3];
191 switch (ycbcr_format.luma_coefficients) {
200 // Rec. 709, page 19.
209 if (ycbcr_format.full_range) {
210 offset[0] = 0.0 / 255.0;
211 offset[1] = 128.0 / 255.0;
212 offset[2] = 128.0 / 255.0;
218 // Rec. 601, page 4; Rec. 709, page 19.
219 offset[0] = 16.0 / 255.0;
220 offset[1] = 128.0 / 255.0;
221 offset[2] = 128.0 / 255.0;
223 scale[0] = 255.0 / 219.0;
224 scale[1] = 255.0 / 224.0;
225 scale[2] = 255.0 / 224.0;
228 // Matrix to convert RGB to YCbCr. See e.g. Rec. 601.
229 Matrix3d rgb_to_ycbcr;
230 rgb_to_ycbcr(0,0) = coeff[0];
231 rgb_to_ycbcr(0,1) = coeff[1];
232 rgb_to_ycbcr(0,2) = coeff[2];
234 float cb_fac = (224.0 / 219.0) / (coeff[0] + coeff[1] + 1.0f - coeff[2]);
235 rgb_to_ycbcr(1,0) = -coeff[0] * cb_fac;
236 rgb_to_ycbcr(1,1) = -coeff[1] * cb_fac;
237 rgb_to_ycbcr(1,2) = (1.0f - coeff[2]) * cb_fac;
239 float cr_fac = (224.0 / 219.0) / (1.0f - coeff[0] + coeff[1] + coeff[2]);
240 rgb_to_ycbcr(2,0) = (1.0f - coeff[0]) * cr_fac;
241 rgb_to_ycbcr(2,1) = -coeff[1] * cr_fac;
242 rgb_to_ycbcr(2,2) = -coeff[2] * cr_fac;
244 // Inverting the matrix gives us what we need to go from YCbCr back to RGB.
245 Matrix3d ycbcr_to_rgb = rgb_to_ycbcr.inverse();
247 std::string frag_shader;
249 frag_shader = output_glsl_mat3("PREFIX(inv_ycbcr_matrix)", ycbcr_to_rgb);
252 sprintf(buf, "const vec3 PREFIX(offset) = vec3(%.8f, %.8f, %.8f);\n",
253 offset[0], offset[1], offset[2]);
256 sprintf(buf, "const vec3 PREFIX(scale) = vec3(%.8f, %.8f, %.8f);\n",
257 scale[0], scale[1], scale[2]);
260 float cb_offset_x = compute_chroma_offset(
261 ycbcr_format.cb_x_position, ycbcr_format.chroma_subsampling_x, widths[1]);
262 float cb_offset_y = compute_chroma_offset(
263 ycbcr_format.cb_y_position, ycbcr_format.chroma_subsampling_y, heights[1]);
264 sprintf(buf, "const vec2 PREFIX(cb_offset) = vec2(%.8f, %.8f);\n",
265 cb_offset_x, cb_offset_y);
268 float cr_offset_x = compute_chroma_offset(
269 ycbcr_format.cr_x_position, ycbcr_format.chroma_subsampling_x, widths[2]);
270 float cr_offset_y = compute_chroma_offset(
271 ycbcr_format.cr_y_position, ycbcr_format.chroma_subsampling_y, heights[2]);
272 sprintf(buf, "const vec2 PREFIX(cr_offset) = vec2(%.8f, %.8f);\n",
273 cr_offset_x, cr_offset_y);
276 frag_shader += read_file("ycbcr_input.frag");