1 #include "chroma_subsampler.h"
5 #include <movit/effect_util.h>
6 #include <movit/resource_pool.h>
7 #include <movit/util.h>
12 ChromaSubsampler::ChromaSubsampler(ResourcePool *resource_pool)
13 : resource_pool(resource_pool)
15 vector<string> frag_shader_outputs;
17 // Set up stuff for NV12 conversion.
19 // Note: Due to the horizontally co-sited chroma/luma samples in H.264
20 // (chrome position is left for horizontal and center for vertical),
21 // we need to be a bit careful in our subsampling. A diagram will make
22 // this clearer, showing some luma and chroma samples:
37 // Clearly, the rightmost chroma sample here needs to be equivalent to
38 // b/4 + c/2 + d/4. (We could also implement more sophisticated filters,
39 // of course, but as long as the upsampling is not going to be equally
40 // sophisticated, it's probably not worth it.) If we sample once with
41 // no mipmapping, we get just c, ie., no actual filtering in the
42 // horizontal direction. (For the vertical direction, we can just
43 // sample in the middle to get the right filtering.) One could imagine
44 // we could use mipmapping (assuming we can create mipmaps cheaply),
45 // but then, what we'd get is this:
60 // which ends up sampling equally from a and b, which clearly isn't right. Instead,
61 // we need to do two (non-mipmapped) chroma samples, both hitting exactly in-between
64 // Sampling in-between b and c gives us the sample (b+c)/2, and similarly for c and d.
65 // Taking the average of these gives of (b+c)/4 + (c+d)/4 = b/4 + c/2 + d/4, which is
66 // exactly what we want.
68 // See also http://www.poynton.com/PDFs/Merging_RGB_and_422.pdf, pages 6–7.
71 string cbcr_vert_shader =
74 "in vec2 position; \n"
75 "in vec2 texcoord; \n"
76 "out vec2 tc0, tc1; \n"
77 "uniform vec2 foo_chroma_offset_0; \n"
78 "uniform vec2 foo_chroma_offset_1; \n"
82 " // The result of glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0) is: \n"
84 " // 2.000 0.000 0.000 -1.000 \n"
85 " // 0.000 2.000 0.000 -1.000 \n"
86 " // 0.000 0.000 -2.000 -1.000 \n"
87 " // 0.000 0.000 0.000 1.000 \n"
88 " gl_Position = vec4(2.0 * position.x - 1.0, 2.0 * position.y - 1.0, -1.0, 1.0); \n"
89 " vec2 flipped_tc = texcoord; \n"
90 " tc0 = flipped_tc + foo_chroma_offset_0; \n"
91 " tc1 = flipped_tc + foo_chroma_offset_1; \n"
93 string cbcr_frag_shader =
95 "in vec2 tc0, tc1; \n"
96 "uniform sampler2D cbcr_tex; \n"
97 "out vec4 FragColor; \n"
99 " FragColor = 0.5 * (texture(cbcr_tex, tc0) + texture(cbcr_tex, tc1)); \n"
101 cbcr_program_num = resource_pool->compile_glsl_program(cbcr_vert_shader, cbcr_frag_shader, frag_shader_outputs);
104 cbcr_texture_sampler_uniform = glGetUniformLocation(cbcr_program_num, "cbcr_tex");
106 cbcr_position_attribute_index = glGetAttribLocation(cbcr_program_num, "position");
108 cbcr_texcoord_attribute_index = glGetAttribLocation(cbcr_program_num, "texcoord");
111 // Same, for UYVY conversion.
112 string uyvy_vert_shader =
115 "in vec2 position; \n"
116 "in vec2 texcoord; \n"
117 "out vec2 y_tc0, y_tc1, cbcr_tc0, cbcr_tc1; \n"
118 "uniform vec2 foo_luma_offset_0; \n"
119 "uniform vec2 foo_luma_offset_1; \n"
120 "uniform vec2 foo_chroma_offset_0; \n"
121 "uniform vec2 foo_chroma_offset_1; \n"
125 " // The result of glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0) is: \n"
127 " // 2.000 0.000 0.000 -1.000 \n"
128 " // 0.000 2.000 0.000 -1.000 \n"
129 " // 0.000 0.000 -2.000 -1.000 \n"
130 " // 0.000 0.000 0.000 1.000 \n"
131 " gl_Position = vec4(2.0 * position.x - 1.0, 2.0 * position.y - 1.0, -1.0, 1.0); \n"
132 " vec2 flipped_tc = texcoord; \n"
133 " y_tc0 = flipped_tc + foo_luma_offset_0; \n"
134 " y_tc1 = flipped_tc + foo_luma_offset_1; \n"
135 " cbcr_tc0 = flipped_tc + foo_chroma_offset_0; \n"
136 " cbcr_tc1 = flipped_tc + foo_chroma_offset_1; \n"
138 string uyvy_frag_shader =
140 "in vec2 y_tc0, y_tc1, cbcr_tc0, cbcr_tc1; \n"
141 "uniform sampler2D y_tex, cbcr_tex; \n"
142 "out vec4 FragColor; \n"
144 " float y0 = texture(y_tex, y_tc0).r; \n"
145 " float y1 = texture(y_tex, y_tc1).r; \n"
146 " vec2 cbcr0 = texture(cbcr_tex, cbcr_tc0).rg; \n"
147 " vec2 cbcr1 = texture(cbcr_tex, cbcr_tc1).rg; \n"
148 " vec2 cbcr = 0.5 * (cbcr0 + cbcr1); \n"
149 " FragColor = vec4(cbcr.g, y0, cbcr.r, y1); \n"
152 uyvy_program_num = resource_pool->compile_glsl_program(uyvy_vert_shader, uyvy_frag_shader, frag_shader_outputs);
155 uyvy_y_texture_sampler_uniform = glGetUniformLocation(uyvy_program_num, "y_tex");
157 uyvy_cbcr_texture_sampler_uniform = glGetUniformLocation(uyvy_program_num, "cbcr_tex");
159 uyvy_position_attribute_index = glGetAttribLocation(uyvy_program_num, "position");
161 uyvy_texcoord_attribute_index = glGetAttribLocation(uyvy_program_num, "texcoord");
164 // Shared between the two.
170 vbo = generate_vbo(2, GL_FLOAT, sizeof(vertices), vertices);
174 ChromaSubsampler::~ChromaSubsampler()
176 resource_pool->release_glsl_program(cbcr_program_num);
178 resource_pool->release_glsl_program(uyvy_program_num);
180 glDeleteBuffers(1, &vbo);
184 void ChromaSubsampler::subsample_chroma(GLuint cbcr_tex, unsigned width, unsigned height, GLuint dst_tex)
187 glGenVertexArrays(1, &vao);
190 glBindVertexArray(vao);
194 GLuint fbo = resource_pool->create_fbo(dst_tex);
195 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
196 glViewport(0, 0, width/2, height/2);
199 glUseProgram(cbcr_program_num);
202 glActiveTexture(GL_TEXTURE0);
204 glBindTexture(GL_TEXTURE_2D, cbcr_tex);
206 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
208 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
210 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
213 float chroma_offset_0[] = { -1.0f / width, 0.0f };
214 float chroma_offset_1[] = { -0.0f / width, 0.0f };
215 set_uniform_vec2(cbcr_program_num, "foo", "chroma_offset_0", chroma_offset_0);
216 set_uniform_vec2(cbcr_program_num, "foo", "chroma_offset_1", chroma_offset_1);
218 glUniform1i(cbcr_texture_sampler_uniform, 0);
220 glBindBuffer(GL_ARRAY_BUFFER, vbo);
223 for (GLint attr_index : { cbcr_position_attribute_index, cbcr_texcoord_attribute_index }) {
224 glEnableVertexAttribArray(attr_index);
226 glVertexAttribPointer(attr_index, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
230 glDrawArrays(GL_TRIANGLES, 0, 3);
233 for (GLint attr_index : { cbcr_position_attribute_index, cbcr_texcoord_attribute_index }) {
234 glDisableVertexAttribArray(attr_index);
240 glBindFramebuffer(GL_FRAMEBUFFER, 0);
243 resource_pool->release_fbo(fbo);
244 glDeleteVertexArrays(1, &vao);
248 void ChromaSubsampler::create_uyvy(GLuint y_tex, GLuint cbcr_tex, unsigned width, unsigned height, GLuint dst_tex)
251 glGenVertexArrays(1, &vao);
254 glBindVertexArray(vao);
257 GLuint fbo = resource_pool->create_fbo(dst_tex);
258 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
259 glViewport(0, 0, width/2, height);
262 glUseProgram(uyvy_program_num);
265 glUniform1i(uyvy_y_texture_sampler_uniform, 0);
267 glUniform1i(uyvy_cbcr_texture_sampler_uniform, 1);
270 glActiveTexture(GL_TEXTURE0);
272 glBindTexture(GL_TEXTURE_2D, y_tex);
274 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
276 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
278 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
281 glActiveTexture(GL_TEXTURE1);
283 glBindTexture(GL_TEXTURE_2D, cbcr_tex);
285 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
287 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
289 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
292 float y_offset_0[] = { -0.5f / width, 0.0f };
293 float y_offset_1[] = { 0.5f / width, 0.0f };
294 float cbcr_offset0[] = { -1.0f / width, 0.0f };
295 float cbcr_offset1[] = { -0.0f / width, 0.0f };
296 set_uniform_vec2(uyvy_program_num, "foo", "luma_offset_0", y_offset_0);
297 set_uniform_vec2(uyvy_program_num, "foo", "luma_offset_1", y_offset_1);
298 set_uniform_vec2(uyvy_program_num, "foo", "chroma_offset_0", cbcr_offset0);
299 set_uniform_vec2(uyvy_program_num, "foo", "chroma_offset_1", cbcr_offset1);
301 glBindBuffer(GL_ARRAY_BUFFER, vbo);
304 for (GLint attr_index : { uyvy_position_attribute_index, uyvy_texcoord_attribute_index }) {
305 if (attr_index == -1) continue;
306 glEnableVertexAttribArray(attr_index);
308 glVertexAttribPointer(attr_index, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
312 glDrawArrays(GL_TRIANGLES, 0, 3);
315 for (GLint attr_index : { uyvy_position_attribute_index, uyvy_texcoord_attribute_index }) {
316 if (attr_index == -1) continue;
317 glDisableVertexAttribArray(attr_index);
321 glActiveTexture(GL_TEXTURE0);
325 glBindFramebuffer(GL_FRAMEBUFFER, 0);
328 resource_pool->release_fbo(fbo);
329 glDeleteVertexArrays(1, &vao);