8 #include "effect_util.h"
9 #include "resource_pool.h"
11 #include "ycbcr_input.h"
13 using namespace Eigen;
20 // OpenGL has texel center in (0.5, 0.5), but different formats have
21 // chroma in various other places. If luma samples are X, the chroma
22 // sample is *, and subsampling is 3x3, the situation with chroma
23 // center in (0.5, 0.5) looks approximately like this:
29 // If, on the other hand, chroma center is in (0.0, 0.5) (common
30 // for e.g. MPEG-4), the figure changes to:
36 // In other words, (0.0, 0.0) means that the chroma sample is exactly
37 // co-sited on top of the top-left luma sample. Note, however, that
38 // this is _not_ 0.5 texels to the left, since the OpenGL's texel center
39 // is in (0.5, 0.5); it is in (0.25, 0.25). In a sense, the four luma samples
40 // define a square where chroma position (0.0, 0.0) is in texel position
41 // (0.25, 0.25) and chroma position (1.0, 1.0) is in texel position (0.75, 0.75)
42 // (the outer border shows the borders of the texel itself, ie. from
53 // Also note that if we have no subsampling, the square will have zero
54 // area and the chroma position does not matter at all.
55 float compute_chroma_offset(float pos, unsigned subsampling_factor, unsigned resolution)
57 float local_chroma_pos = (0.5 + pos * (subsampling_factor - 1)) / subsampling_factor;
58 return (0.5 - local_chroma_pos) / resolution;
63 YCbCrInput::YCbCrInput(const ImageFormat &image_format,
64 const YCbCrFormat &ycbcr_format,
65 unsigned width, unsigned height)
66 : image_format(image_format),
67 ycbcr_format(ycbcr_format),
73 pbos[0] = pbos[1] = pbos[2] = 0;
74 texture_num[0] = texture_num[1] = texture_num[2] = 0;
76 assert(width % ycbcr_format.chroma_subsampling_x == 0);
77 pitch[0] = widths[0] = width;
78 pitch[1] = widths[1] = width / ycbcr_format.chroma_subsampling_x;
79 pitch[2] = widths[2] = width / ycbcr_format.chroma_subsampling_x;
81 assert(height % ycbcr_format.chroma_subsampling_y == 0);
83 heights[1] = height / ycbcr_format.chroma_subsampling_y;
84 heights[2] = height / ycbcr_format.chroma_subsampling_y;
86 pixel_data[0] = pixel_data[1] = pixel_data[2] = NULL;
88 register_int("needs_mipmaps", &needs_mipmaps);
91 YCbCrInput::~YCbCrInput()
93 for (unsigned channel = 0; channel < 3; ++channel) {
94 if (texture_num[channel] != 0) {
95 resource_pool->release_2d_texture(texture_num[channel]);
100 void YCbCrInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
102 for (unsigned channel = 0; channel < 3; ++channel) {
103 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
106 if (texture_num[channel] == 0) {
107 // (Re-)upload the texture.
108 texture_num[channel] = resource_pool->create_2d_texture(GL_R8, widths[channel], heights[channel]);
109 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
113 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
115 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
117 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
119 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], heights[channel], GL_RED, GL_UNSIGNED_BYTE, pixel_data[channel]);
121 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
123 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
128 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
133 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
137 set_uniform_int(glsl_program_num, prefix, "tex_y", *sampler_num + 0);
138 set_uniform_int(glsl_program_num, prefix, "tex_cb", *sampler_num + 1);
139 set_uniform_int(glsl_program_num, prefix, "tex_cr", *sampler_num + 2);
144 string YCbCrInput::output_fragment_shader()
146 float coeff[3], offset[3], scale[3];
148 switch (ycbcr_format.luma_coefficients) {
157 // Rec. 709, page 19.
164 // Rec. 2020, page 4.
174 if (ycbcr_format.full_range) {
175 offset[0] = 0.0 / 255.0;
176 offset[1] = 128.0 / 255.0;
177 offset[2] = 128.0 / 255.0;
183 // Rec. 601, page 4; Rec. 709, page 19; Rec. 2020, page 4.
184 offset[0] = 16.0 / 255.0;
185 offset[1] = 128.0 / 255.0;
186 offset[2] = 128.0 / 255.0;
188 scale[0] = 255.0 / 219.0;
189 scale[1] = 255.0 / 224.0;
190 scale[2] = 255.0 / 224.0;
193 // Matrix to convert RGB to YCbCr. See e.g. Rec. 601.
194 Matrix3d rgb_to_ycbcr;
195 rgb_to_ycbcr(0,0) = coeff[0];
196 rgb_to_ycbcr(0,1) = coeff[1];
197 rgb_to_ycbcr(0,2) = coeff[2];
199 float cb_fac = (224.0 / 219.0) / (coeff[0] + coeff[1] + 1.0f - coeff[2]);
200 rgb_to_ycbcr(1,0) = -coeff[0] * cb_fac;
201 rgb_to_ycbcr(1,1) = -coeff[1] * cb_fac;
202 rgb_to_ycbcr(1,2) = (1.0f - coeff[2]) * cb_fac;
204 float cr_fac = (224.0 / 219.0) / (1.0f - coeff[0] + coeff[1] + coeff[2]);
205 rgb_to_ycbcr(2,0) = (1.0f - coeff[0]) * cr_fac;
206 rgb_to_ycbcr(2,1) = -coeff[1] * cr_fac;
207 rgb_to_ycbcr(2,2) = -coeff[2] * cr_fac;
209 // Inverting the matrix gives us what we need to go from YCbCr back to RGB.
210 Matrix3d ycbcr_to_rgb = rgb_to_ycbcr.inverse();
214 frag_shader = output_glsl_mat3("PREFIX(inv_ycbcr_matrix)", ycbcr_to_rgb);
217 sprintf(buf, "const vec3 PREFIX(offset) = vec3(%.8f, %.8f, %.8f);\n",
218 offset[0], offset[1], offset[2]);
221 sprintf(buf, "const vec3 PREFIX(scale) = vec3(%.8f, %.8f, %.8f);\n",
222 scale[0], scale[1], scale[2]);
225 float cb_offset_x = compute_chroma_offset(
226 ycbcr_format.cb_x_position, ycbcr_format.chroma_subsampling_x, widths[1]);
227 float cb_offset_y = compute_chroma_offset(
228 ycbcr_format.cb_y_position, ycbcr_format.chroma_subsampling_y, heights[1]);
229 sprintf(buf, "const vec2 PREFIX(cb_offset) = vec2(%.8f, %.8f);\n",
230 cb_offset_x, cb_offset_y);
233 float cr_offset_x = compute_chroma_offset(
234 ycbcr_format.cr_x_position, ycbcr_format.chroma_subsampling_x, widths[2]);
235 float cr_offset_y = compute_chroma_offset(
236 ycbcr_format.cr_y_position, ycbcr_format.chroma_subsampling_y, heights[2]);
237 sprintf(buf, "const vec2 PREFIX(cr_offset) = vec2(%.8f, %.8f);\n",
238 cr_offset_x, cr_offset_y);
241 frag_shader += read_file("ycbcr_input.frag");
245 void YCbCrInput::invalidate_pixel_data()
247 for (unsigned channel = 0; channel < 3; ++channel) {
248 if (texture_num[channel] != 0) {
249 resource_pool->release_2d_texture(texture_num[channel]);
250 texture_num[channel] = 0;