8 #include "effect_util.h"
9 #include "resource_pool.h"
12 #include "ycbcr_input.h"
14 using namespace Eigen;
19 YCbCrInput::YCbCrInput(const ImageFormat &image_format,
20 const YCbCrFormat &ycbcr_format,
21 unsigned width, unsigned height,
22 YCbCrInputSplitting ycbcr_input_splitting)
23 : image_format(image_format),
24 ycbcr_format(ycbcr_format),
25 ycbcr_input_splitting(ycbcr_input_splitting),
30 pbos[0] = pbos[1] = pbos[2] = 0;
31 texture_num[0] = texture_num[1] = texture_num[2] = 0;
36 pixel_data[0] = pixel_data[1] = pixel_data[2] = NULL;
37 owns_texture[0] = owns_texture[1] = owns_texture[2] = false;
39 register_uniform_sampler2d("tex_y", &uniform_tex_y);
41 if (ycbcr_input_splitting == YCBCR_INPUT_INTERLEAVED) {
43 assert(ycbcr_format.chroma_subsampling_x == 1);
44 assert(ycbcr_format.chroma_subsampling_y == 1);
45 } else if (ycbcr_input_splitting == YCBCR_INPUT_SPLIT_Y_AND_CBCR) {
47 register_uniform_sampler2d("tex_cbcr", &uniform_tex_cb);
49 assert(ycbcr_input_splitting == YCBCR_INPUT_PLANAR);
51 register_uniform_sampler2d("tex_cb", &uniform_tex_cb);
52 register_uniform_sampler2d("tex_cr", &uniform_tex_cr);
56 YCbCrInput::~YCbCrInput()
58 for (unsigned channel = 0; channel < num_channels; ++channel) {
59 possibly_release_texture(channel);
63 void YCbCrInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
65 for (unsigned channel = 0; channel < num_channels; ++channel) {
66 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
69 if (texture_num[channel] == 0 && (pbos[channel] != 0 || pixel_data[channel] != NULL)) {
70 GLenum format, internal_format;
71 if (channel == 0 && ycbcr_input_splitting == YCBCR_INPUT_INTERLEAVED) {
73 internal_format = GL_RGB8;
74 } else if (channel == 1 && ycbcr_input_splitting == YCBCR_INPUT_SPLIT_Y_AND_CBCR) {
76 internal_format = GL_RG8;
79 internal_format = GL_R8;
82 // (Re-)upload the texture.
83 texture_num[channel] = resource_pool->create_2d_texture(internal_format, widths[channel], heights[channel]);
84 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
86 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
88 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
90 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
92 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
94 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], heights[channel], format, GL_UNSIGNED_BYTE, pixel_data[channel]);
96 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
98 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
100 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
102 owns_texture[channel] = true;
104 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
109 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
113 uniform_tex_y = *sampler_num + 0;
114 uniform_tex_cb = *sampler_num + 1;
115 if (ycbcr_input_splitting == YCBCR_INPUT_PLANAR) {
116 uniform_tex_cr = *sampler_num + 2;
119 *sampler_num += num_channels;
122 string YCbCrInput::output_fragment_shader()
125 Matrix3d ycbcr_to_rgb;
126 compute_ycbcr_matrix(ycbcr_format, offset, &ycbcr_to_rgb);
130 frag_shader = output_glsl_mat3("PREFIX(inv_ycbcr_matrix)", ycbcr_to_rgb);
131 frag_shader += output_glsl_vec3("PREFIX(offset)", offset[0], offset[1], offset[2]);
133 float cb_offset_x = compute_chroma_offset(
134 ycbcr_format.cb_x_position, ycbcr_format.chroma_subsampling_x, widths[1]);
135 float cb_offset_y = compute_chroma_offset(
136 ycbcr_format.cb_y_position, ycbcr_format.chroma_subsampling_y, heights[1]);
137 frag_shader += output_glsl_vec2("PREFIX(cb_offset)", cb_offset_x, cb_offset_y);
139 float cr_offset_x = compute_chroma_offset(
140 ycbcr_format.cr_x_position, ycbcr_format.chroma_subsampling_x, widths[2]);
141 float cr_offset_y = compute_chroma_offset(
142 ycbcr_format.cr_y_position, ycbcr_format.chroma_subsampling_y, heights[2]);
143 frag_shader += output_glsl_vec2("PREFIX(cr_offset)", cr_offset_x, cr_offset_y);
145 if (ycbcr_input_splitting == YCBCR_INPUT_INTERLEAVED) {
146 frag_shader += "#define Y_CB_CR_SAME_TEXTURE 1\n";
147 } else if (ycbcr_input_splitting == YCBCR_INPUT_SPLIT_Y_AND_CBCR) {
149 snprintf(buf, sizeof(buf), "#define Y_CB_CR_SAME_TEXTURE 0\n#define CB_CR_SAME_TEXTURE 1\n#define CB_CR_OFFSETS_EQUAL %d\n",
150 (fabs(ycbcr_format.cb_x_position - ycbcr_format.cr_x_position) < 1e-6));
153 frag_shader += "#define Y_CB_CR_SAME_TEXTURE 0\n#define CB_CR_SAME_TEXTURE 0\n";
156 frag_shader += read_file("ycbcr_input.frag");
160 void YCbCrInput::invalidate_pixel_data()
162 for (unsigned channel = 0; channel < 3; ++channel) {
163 possibly_release_texture(channel);
167 bool YCbCrInput::set_int(const std::string& key, int value)
169 if (key == "needs_mipmaps") {
170 // We currently do not support this.
173 return Effect::set_int(key, value);
176 void YCbCrInput::possibly_release_texture(unsigned channel)
178 if (texture_num[channel] != 0 && owns_texture[channel]) {
179 resource_pool->release_2d_texture(texture_num[channel]);
180 texture_num[channel] = 0;
181 owns_texture[channel] = false;