4 #include "ycbcr_input.h"
8 YCbCrInput::YCbCrInput(const ImageFormat &image_format,
9 const YCbCrFormat &ycbcr_format,
10 unsigned width, unsigned height)
11 : image_format(image_format),
12 ycbcr_format(ycbcr_format),
19 pitch[0] = pitch[1] = pitch[2] = width;
21 assert(width % ycbcr_format.chroma_subsampling_x == 0);
23 widths[1] = width / ycbcr_format.chroma_subsampling_x;
24 widths[2] = width / ycbcr_format.chroma_subsampling_x;
26 assert(height % ycbcr_format.chroma_subsampling_y == 0);
28 heights[1] = height / ycbcr_format.chroma_subsampling_y;
29 heights[2] = height / ycbcr_format.chroma_subsampling_y;
31 register_int("needs_mipmaps", &needs_mipmaps);
34 void YCbCrInput::finalize()
36 // Create PBOs to hold the textures holding the input image, and then the texture itself.
37 glGenBuffers(3, pbos);
39 glGenTextures(3, texture_num);
42 for (unsigned channel = 0; channel < 3; ++channel) {
43 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
45 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, pitch[channel] * heights[channel], NULL, GL_STREAM_DRAW);
47 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
50 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
52 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
54 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
56 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE8, widths[channel], heights[channel], 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL);
58 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
66 void YCbCrInput::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
68 for (unsigned channel = 0; channel < 3; ++channel) {
69 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
71 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
75 // Copy the pixel data into the PBO.
76 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
78 void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
79 memcpy(mapped_pbo, pixel_data[channel], pitch[channel] * heights[channel]);
80 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
83 // Re-upload the texture from the PBO.
84 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
86 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], heights[channel], GL_LUMINANCE, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
88 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
92 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
94 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
100 set_uniform_int(glsl_program_num, prefix, "tex_y", *sampler_num + 0);
101 set_uniform_int(glsl_program_num, prefix, "tex_cb", *sampler_num + 1);
102 set_uniform_int(glsl_program_num, prefix, "tex_cr", *sampler_num + 2);
105 needs_update = false;
108 std::string YCbCrInput::output_fragment_shader()
110 float coeff[3], offset[3], scale[3];
112 switch (ycbcr_format.luma_coefficients) {
121 // Rec. 709, page 19.
130 if (ycbcr_format.full_range) {
131 offset[0] = 0.0 / 255.0;
132 offset[1] = 128.0 / 255.0;
133 offset[2] = 128.0 / 255.0;
139 // Rec. 601, page 4; Rec. 709, page 19.
140 offset[0] = 16.0 / 255.0;
141 offset[1] = 128.0 / 255.0;
142 offset[2] = 128.0 / 255.0;
144 scale[0] = 255.0 / 219.0;
145 scale[1] = 255.0 / 224.0;
146 scale[2] = 255.0 / 224.0;
149 // Matrix to convert RGB to YCbCr. See e.g. Rec. 601.
150 Matrix3x3 rgb_to_ycbcr;
151 rgb_to_ycbcr[0] = coeff[0];
152 rgb_to_ycbcr[3] = coeff[1];
153 rgb_to_ycbcr[6] = coeff[2];
155 float cb_fac = (224.0 / 219.0) / (coeff[0] + coeff[1] + 1.0f - coeff[2]);
156 rgb_to_ycbcr[1] = -coeff[0] * cb_fac;
157 rgb_to_ycbcr[4] = -coeff[1] * cb_fac;
158 rgb_to_ycbcr[7] = (1.0f - coeff[2]) * cb_fac;
160 float cr_fac = (224.0 / 219.0) / (1.0f - coeff[0] + coeff[1] + coeff[2]);
161 rgb_to_ycbcr[2] = (1.0f - coeff[0]) * cr_fac;
162 rgb_to_ycbcr[5] = -coeff[1] * cr_fac;
163 rgb_to_ycbcr[8] = -coeff[2] * cr_fac;
165 // Inverting the matrix gives us what we need to go from YCbCr back to RGB.
166 Matrix3x3 ycbcr_to_rgb;
167 invert_3x3_matrix(rgb_to_ycbcr, ycbcr_to_rgb);
169 std::string frag_shader;
173 "const mat3 PREFIX(inv_ycbcr_matrix) = mat3(\n"
174 " %.8f, %.8f, %.8f,\n"
175 " %.8f, %.8f, %.8f,\n"
176 " %.8f, %.8f, %.8f);\n",
177 ycbcr_to_rgb[0], ycbcr_to_rgb[1], ycbcr_to_rgb[2],
178 ycbcr_to_rgb[3], ycbcr_to_rgb[4], ycbcr_to_rgb[5],
179 ycbcr_to_rgb[6], ycbcr_to_rgb[7], ycbcr_to_rgb[8]);
182 sprintf(buf, "const vec3 PREFIX(offset) = vec3(%.8f, %.8f, %.8f);\n",
183 offset[0], offset[1], offset[2]);
186 sprintf(buf, "const vec3 PREFIX(scale) = vec3(%.8f, %.8f, %.8f);\n",
187 scale[0], scale[1], scale[2]);
190 // OpenGL has texel center in (0.5, 0.5), but different formats have
191 // chroma in various other places. If luma samples are X, the chroma
192 // sample is *, and subsampling is 3x3, the situation with chroma
193 // center in (0.5, 0.5) looks approximately like this:
199 // If, on the other hand, chroma center is in (0.0, 0.5) (common
200 // for e.g. MPEG-4), the figure changes to:
206 // Obviously, the chroma plane here needs to be moved to the left,
207 // which means _adding_ 0.5 to the texture coordinates when sampling
209 float chroma_offset_x = (0.5f - ycbcr_format.chroma_x_position) / widths[1];
210 float chroma_offset_y = (0.5f - ycbcr_format.chroma_y_position) / heights[1];
211 sprintf(buf, "const vec2 PREFIX(chroma_offset) = vec2(%.8f, %.8f);\n",
212 chroma_offset_x, chroma_offset_y);
215 frag_shader += read_file("ycbcr_input.frag");