6 #include "effect_util.h"
7 #include "resource_pool.h"
10 #include "ycbcr_422interleaved_input.h"
12 using namespace Eigen;
17 YCbCr422InterleavedInput::YCbCr422InterleavedInput(const ImageFormat &image_format,
18 const YCbCrFormat &ycbcr_format,
19 unsigned width, unsigned height)
20 : image_format(image_format),
21 ycbcr_format(ycbcr_format),
27 texture_num[0] = texture_num[1] = 0;
29 assert(ycbcr_format.chroma_subsampling_x == 2);
30 assert(ycbcr_format.chroma_subsampling_y == 1);
31 assert(width % ycbcr_format.chroma_subsampling_x == 0);
33 widths[CHANNEL_LUMA] = width;
34 widths[CHANNEL_CHROMA] = width / ycbcr_format.chroma_subsampling_x;
35 pitches[CHANNEL_LUMA] = width;
36 pitches[CHANNEL_CHROMA] = width / ycbcr_format.chroma_subsampling_x;
40 register_uniform_sampler2d("tex_y", &uniform_tex_y);
41 register_uniform_sampler2d("tex_cbcr", &uniform_tex_cbcr);
44 YCbCr422InterleavedInput::~YCbCr422InterleavedInput()
46 for (unsigned channel = 0; channel < 2; ++channel) {
47 if (texture_num[channel] != 0) {
48 resource_pool->release_2d_texture(texture_num[channel]);
53 void YCbCr422InterleavedInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
55 for (unsigned channel = 0; channel < 2; ++channel) {
56 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
59 if (texture_num[channel] == 0) {
60 // (Re-)upload the texture.
61 GLuint format, internal_format;
62 if (channel == CHANNEL_LUMA) {
64 internal_format = GL_RG8;
66 assert(channel == CHANNEL_CHROMA);
68 internal_format = GL_RGBA8;
71 texture_num[channel] = resource_pool->create_2d_texture(internal_format, widths[channel], height);
72 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
74 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
76 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
78 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
80 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitches[channel]);
82 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], height, format, GL_UNSIGNED_BYTE, pixel_data);
84 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
86 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
88 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
91 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
96 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
100 uniform_tex_y = *sampler_num + 0;
101 uniform_tex_cbcr = *sampler_num + 1;
106 string YCbCr422InterleavedInput::output_fragment_shader()
109 Matrix3d ycbcr_to_rgb;
110 compute_ycbcr_matrix(ycbcr_format, offset, &ycbcr_to_rgb);
114 frag_shader = output_glsl_mat3("PREFIX(inv_ycbcr_matrix)", ycbcr_to_rgb);
115 frag_shader += output_glsl_vec3("PREFIX(offset)", offset[0], offset[1], offset[2]);
117 float cb_offset_x = compute_chroma_offset(
118 ycbcr_format.cb_x_position, ycbcr_format.chroma_subsampling_x, widths[CHANNEL_CHROMA]);
119 float cr_offset_x = compute_chroma_offset(
120 ycbcr_format.cr_x_position, ycbcr_format.chroma_subsampling_x, widths[CHANNEL_CHROMA]);
121 frag_shader += output_glsl_float("PREFIX(cb_offset_x)", cb_offset_x);
122 frag_shader += output_glsl_float("PREFIX(cr_offset_x)", cr_offset_x);
125 sprintf(buf, "#define CB_CR_OFFSETS_EQUAL %d\n",
126 (fabs(ycbcr_format.cb_x_position - ycbcr_format.cr_x_position) < 1e-6));
129 frag_shader += read_file("ycbcr_422interleaved_input.frag");
133 void YCbCr422InterleavedInput::invalidate_pixel_data()
135 for (unsigned channel = 0; channel < 2; ++channel) {
136 if (texture_num[channel] != 0) {
137 resource_pool->release_2d_texture(texture_num[channel]);
138 texture_num[channel] = 0;
143 bool YCbCr422InterleavedInput::set_int(const std::string& key, int value)
145 if (key == "needs_mipmaps") {
146 // We currently do not support this.
149 return Effect::set_int(key, value);