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),
14 needs_pbo_recreate(false),
20 pitch[0] = pitch[1] = pitch[2] = width;
22 assert(width % ycbcr_format.chroma_subsampling_x == 0);
24 widths[1] = width / ycbcr_format.chroma_subsampling_x;
25 widths[2] = width / ycbcr_format.chroma_subsampling_x;
27 assert(height % ycbcr_format.chroma_subsampling_y == 0);
29 heights[1] = height / ycbcr_format.chroma_subsampling_y;
30 heights[2] = height / ycbcr_format.chroma_subsampling_y;
32 register_int("needs_mipmaps", &needs_mipmaps);
35 void YCbCrInput::finalize()
37 // Create PBOs to hold the textures holding the input image, and then the texture itself.
38 glGenBuffers(3, pbos);
40 glGenTextures(3, texture_num);
43 for (unsigned channel = 0; channel < 3; ++channel) {
44 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
46 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, pitch[channel] * heights[channel], NULL, GL_STREAM_DRAW);
48 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
51 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
53 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
55 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
57 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE8, widths[channel], heights[channel], 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL);
59 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
67 void YCbCrInput::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
69 for (unsigned channel = 0; channel < 3; ++channel) {
70 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
72 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
75 if (needs_update || needs_pbo_recreate) {
76 // Copy the pixel data into the PBO.
77 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
80 if (needs_pbo_recreate) {
81 // The pitch has changed; we need to reallocate this PBO.
82 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, pitch[channel] * heights[channel], NULL, GL_STREAM_DRAW);
86 void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
87 memcpy(mapped_pbo, pixel_data[channel], pitch[channel] * heights[channel]);
89 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
92 // Re-upload the texture from the PBO.
93 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
95 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], heights[channel], GL_LUMINANCE, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
97 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
99 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
101 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
103 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
109 set_uniform_int(glsl_program_num, prefix, "tex_y", *sampler_num + 0);
110 set_uniform_int(glsl_program_num, prefix, "tex_cb", *sampler_num + 1);
111 set_uniform_int(glsl_program_num, prefix, "tex_cr", *sampler_num + 2);
114 needs_update = false;
115 needs_pbo_recreate = false;
118 std::string YCbCrInput::output_fragment_shader()
120 float coeff[3], offset[3], scale[3];
122 switch (ycbcr_format.luma_coefficients) {
131 // Rec. 709, page 19.
140 if (ycbcr_format.full_range) {
141 offset[0] = 0.0 / 255.0;
142 offset[1] = 128.0 / 255.0;
143 offset[2] = 128.0 / 255.0;
149 // Rec. 601, page 4; Rec. 709, page 19.
150 offset[0] = 16.0 / 255.0;
151 offset[1] = 128.0 / 255.0;
152 offset[2] = 128.0 / 255.0;
154 scale[0] = 255.0 / 219.0;
155 scale[1] = 255.0 / 224.0;
156 scale[2] = 255.0 / 224.0;
159 // Matrix to convert RGB to YCbCr. See e.g. Rec. 601.
160 Matrix3x3 rgb_to_ycbcr;
161 rgb_to_ycbcr[0] = coeff[0];
162 rgb_to_ycbcr[3] = coeff[1];
163 rgb_to_ycbcr[6] = coeff[2];
165 float cb_fac = (224.0 / 219.0) / (coeff[0] + coeff[1] + 1.0f - coeff[2]);
166 rgb_to_ycbcr[1] = -coeff[0] * cb_fac;
167 rgb_to_ycbcr[4] = -coeff[1] * cb_fac;
168 rgb_to_ycbcr[7] = (1.0f - coeff[2]) * cb_fac;
170 float cr_fac = (224.0 / 219.0) / (1.0f - coeff[0] + coeff[1] + coeff[2]);
171 rgb_to_ycbcr[2] = (1.0f - coeff[0]) * cr_fac;
172 rgb_to_ycbcr[5] = -coeff[1] * cr_fac;
173 rgb_to_ycbcr[8] = -coeff[2] * cr_fac;
175 // Inverting the matrix gives us what we need to go from YCbCr back to RGB.
176 Matrix3x3 ycbcr_to_rgb;
177 invert_3x3_matrix(rgb_to_ycbcr, ycbcr_to_rgb);
179 std::string frag_shader;
183 "const mat3 PREFIX(inv_ycbcr_matrix) = mat3(\n"
184 " %.8f, %.8f, %.8f,\n"
185 " %.8f, %.8f, %.8f,\n"
186 " %.8f, %.8f, %.8f);\n",
187 ycbcr_to_rgb[0], ycbcr_to_rgb[1], ycbcr_to_rgb[2],
188 ycbcr_to_rgb[3], ycbcr_to_rgb[4], ycbcr_to_rgb[5],
189 ycbcr_to_rgb[6], ycbcr_to_rgb[7], ycbcr_to_rgb[8]);
192 sprintf(buf, "const vec3 PREFIX(offset) = vec3(%.8f, %.8f, %.8f);\n",
193 offset[0], offset[1], offset[2]);
196 sprintf(buf, "const vec3 PREFIX(scale) = vec3(%.8f, %.8f, %.8f);\n",
197 scale[0], scale[1], scale[2]);
200 // OpenGL has texel center in (0.5, 0.5), but different formats have
201 // chroma in various other places. If luma samples are X, the chroma
202 // sample is *, and subsampling is 3x3, the situation with chroma
203 // center in (0.5, 0.5) looks approximately like this:
209 // If, on the other hand, chroma center is in (0.0, 0.5) (common
210 // for e.g. MPEG-4), the figure changes to:
216 // Obviously, the chroma plane here needs to be moved to the left,
217 // which means _adding_ 0.5 to the texture coordinates when sampling
219 float chroma_offset_x = (0.5f - ycbcr_format.chroma_x_position) / widths[1];
220 float chroma_offset_y = (0.5f - ycbcr_format.chroma_y_position) / heights[1];
221 sprintf(buf, "const vec2 PREFIX(chroma_offset) = vec2(%.8f, %.8f);\n",
222 chroma_offset_x, chroma_offset_y);
225 frag_shader += read_file("ycbcr_input.frag");