6 #include "ycbcr_input.h"
10 using namespace Eigen;
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),
18 needs_pbo_recreate(false),
24 pbos[0] = pbos[1] = pbos[2] = 0;
25 texture_num[0] = texture_num[1] = texture_num[2] = 0;
27 pitch[0] = pitch[1] = pitch[2] = width;
29 assert(width % ycbcr_format.chroma_subsampling_x == 0);
31 widths[1] = width / ycbcr_format.chroma_subsampling_x;
32 widths[2] = width / ycbcr_format.chroma_subsampling_x;
34 assert(height % ycbcr_format.chroma_subsampling_y == 0);
36 heights[1] = height / ycbcr_format.chroma_subsampling_y;
37 heights[2] = height / ycbcr_format.chroma_subsampling_y;
39 register_int("needs_mipmaps", &needs_mipmaps);
42 YCbCrInput::~YCbCrInput()
45 glDeleteBuffers(3, pbos);
48 if (texture_num[0] != 0) {
49 glDeleteTextures(3, texture_num);
54 void YCbCrInput::finalize()
56 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
59 // Create PBOs to hold the textures holding the input image, and then the texture itself.
60 glGenBuffers(3, pbos);
62 glGenTextures(3, texture_num);
65 for (unsigned channel = 0; channel < 3; ++channel) {
66 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
68 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, pitch[channel] * heights[channel], NULL, GL_STREAM_DRAW);
70 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
73 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
75 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
77 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
79 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE8, widths[channel], heights[channel], 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL);
81 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
89 void YCbCrInput::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
91 for (unsigned channel = 0; channel < 3; ++channel) {
92 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
94 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
97 if (needs_update || needs_pbo_recreate) {
98 // Copy the pixel data into the PBO.
99 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
102 if (needs_pbo_recreate) {
103 // The pitch has changed; we need to reallocate this PBO.
104 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, pitch[channel] * heights[channel], NULL, GL_STREAM_DRAW);
108 void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
109 memcpy(mapped_pbo, pixel_data[channel], pitch[channel] * heights[channel]);
111 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
114 // Re-upload the texture from the PBO.
115 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
117 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], heights[channel], GL_LUMINANCE, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
119 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
121 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
123 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
125 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
131 set_uniform_int(glsl_program_num, prefix, "tex_y", *sampler_num + 0);
132 set_uniform_int(glsl_program_num, prefix, "tex_cb", *sampler_num + 1);
133 set_uniform_int(glsl_program_num, prefix, "tex_cr", *sampler_num + 2);
136 needs_update = false;
137 needs_pbo_recreate = false;
140 std::string YCbCrInput::output_fragment_shader()
142 float coeff[3], offset[3], scale[3];
144 switch (ycbcr_format.luma_coefficients) {
153 // Rec. 709, page 19.
162 if (ycbcr_format.full_range) {
163 offset[0] = 0.0 / 255.0;
164 offset[1] = 128.0 / 255.0;
165 offset[2] = 128.0 / 255.0;
171 // Rec. 601, page 4; Rec. 709, page 19.
172 offset[0] = 16.0 / 255.0;
173 offset[1] = 128.0 / 255.0;
174 offset[2] = 128.0 / 255.0;
176 scale[0] = 255.0 / 219.0;
177 scale[1] = 255.0 / 224.0;
178 scale[2] = 255.0 / 224.0;
181 // Matrix to convert RGB to YCbCr. See e.g. Rec. 601.
182 Matrix3d rgb_to_ycbcr;
183 rgb_to_ycbcr(0,0) = coeff[0];
184 rgb_to_ycbcr(0,1) = coeff[1];
185 rgb_to_ycbcr(0,2) = coeff[2];
187 float cb_fac = (224.0 / 219.0) / (coeff[0] + coeff[1] + 1.0f - coeff[2]);
188 rgb_to_ycbcr(1,0) = -coeff[0] * cb_fac;
189 rgb_to_ycbcr(1,1) = -coeff[1] * cb_fac;
190 rgb_to_ycbcr(1,2) = (1.0f - coeff[2]) * cb_fac;
192 float cr_fac = (224.0 / 219.0) / (1.0f - coeff[0] + coeff[1] + coeff[2]);
193 rgb_to_ycbcr(2,0) = (1.0f - coeff[0]) * cr_fac;
194 rgb_to_ycbcr(2,1) = -coeff[1] * cr_fac;
195 rgb_to_ycbcr(2,2) = -coeff[2] * cr_fac;
197 // Inverting the matrix gives us what we need to go from YCbCr back to RGB.
198 Matrix3d ycbcr_to_rgb = rgb_to_ycbcr.inverse();
200 std::string frag_shader;
202 frag_shader = output_glsl_mat3("PREFIX(inv_ycbcr_matrix)", ycbcr_to_rgb);
205 sprintf(buf, "const vec3 PREFIX(offset) = vec3(%.8f, %.8f, %.8f);\n",
206 offset[0], offset[1], offset[2]);
209 sprintf(buf, "const vec3 PREFIX(scale) = vec3(%.8f, %.8f, %.8f);\n",
210 scale[0], scale[1], scale[2]);
213 // OpenGL has texel center in (0.5, 0.5), but different formats have
214 // chroma in various other places. If luma samples are X, the chroma
215 // sample is *, and subsampling is 3x3, the situation with chroma
216 // center in (0.5, 0.5) looks approximately like this:
222 // If, on the other hand, chroma center is in (0.0, 0.5) (common
223 // for e.g. MPEG-4), the figure changes to:
229 // Obviously, the chroma plane here needs to be moved to the left,
230 // which means _adding_ 0.5 to the texture coordinates when sampling
232 float chroma_offset_x = (0.5f - ycbcr_format.chroma_x_position) / widths[1];
233 float chroma_offset_y = (0.5f - ycbcr_format.chroma_y_position) / heights[1];
234 sprintf(buf, "const vec2 PREFIX(chroma_offset) = vec2(%.8f, %.8f);\n",
235 chroma_offset_x, chroma_offset_y);
238 frag_shader += read_file("ycbcr_input.frag");