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;
41 YCbCr422InterleavedInput::~YCbCr422InterleavedInput()
43 for (unsigned channel = 0; channel < 2; ++channel) {
44 if (texture_num[channel] != 0) {
45 resource_pool->release_2d_texture(texture_num[channel]);
50 void YCbCr422InterleavedInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
52 for (unsigned channel = 0; channel < 2; ++channel) {
53 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
56 if (texture_num[channel] == 0) {
57 // (Re-)upload the texture.
58 GLuint format, internal_format;
59 if (channel == CHANNEL_LUMA) {
61 internal_format = GL_RG8;
63 assert(channel == CHANNEL_CHROMA);
65 internal_format = GL_RGBA8;
68 texture_num[channel] = resource_pool->create_2d_texture(internal_format, widths[channel], height);
69 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
71 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
73 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
75 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
77 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitches[channel]);
79 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], height, format, GL_UNSIGNED_BYTE, pixel_data);
81 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
83 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
85 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
88 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
93 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
97 set_uniform_int(glsl_program_num, prefix, "tex_y", *sampler_num + 0);
98 set_uniform_int(glsl_program_num, prefix, "tex_cbcr", *sampler_num + 1);
103 string YCbCr422InterleavedInput::output_fragment_shader()
106 Matrix3d ycbcr_to_rgb;
107 compute_ycbcr_matrix(ycbcr_format, offset, &ycbcr_to_rgb);
111 frag_shader = output_glsl_mat3("PREFIX(inv_ycbcr_matrix)", ycbcr_to_rgb);
112 frag_shader += output_glsl_vec3("PREFIX(offset)", offset[0], offset[1], offset[2]);
114 float cb_offset_x = compute_chroma_offset(
115 ycbcr_format.cb_x_position, ycbcr_format.chroma_subsampling_x, widths[CHANNEL_CHROMA]);
116 float cr_offset_x = compute_chroma_offset(
117 ycbcr_format.cr_x_position, ycbcr_format.chroma_subsampling_x, widths[CHANNEL_CHROMA]);
118 frag_shader += output_glsl_float("PREFIX(cb_offset_x)", cb_offset_x);
119 frag_shader += output_glsl_float("PREFIX(cr_offset_x)", cr_offset_x);
122 sprintf(buf, "#define CB_CR_OFFSETS_EQUAL %d\n",
123 (fabs(ycbcr_format.cb_x_position - ycbcr_format.cr_x_position) < 1e-6));
126 frag_shader += read_file("ycbcr_422interleaved_input.frag");
130 void YCbCr422InterleavedInput::invalidate_pixel_data()
132 for (unsigned channel = 0; channel < 2; ++channel) {
133 if (texture_num[channel] != 0) {
134 resource_pool->release_2d_texture(texture_num[channel]);
135 texture_num[channel] = 0;
140 bool YCbCr422InterleavedInput::set_int(const std::string& key, int value)
142 if (key == "needs_mipmaps") {
143 // We currently do not support this.
146 return Effect::set_int(key, value);