]> git.sesse.net Git - nageru/blob - chroma_subsampler.cpp
Add UYVY support to ChromaSubsampler.
[nageru] / chroma_subsampler.cpp
1 #include "chroma_subsampler.h"
2
3 #include <vector>
4
5 #include <movit/effect_util.h>
6 #include <movit/resource_pool.h>
7 #include <movit/util.h>
8
9 using namespace movit;
10 using namespace std;
11
12 ChromaSubsampler::ChromaSubsampler(ResourcePool *resource_pool)
13         : resource_pool(resource_pool)
14 {
15         vector<string> frag_shader_outputs;
16
17         // Set up stuff for NV12 conversion.
18         //
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:
23         //
24         //     a   b   c   d
25         //   +---+---+---+---+
26         //   |   |   |   |   |
27         //   | Y | Y | Y | Y |
28         //   |   |   |   |   |
29         //   +---+---+---+---+
30         //
31         // +-------+-------+
32         // |       |       |
33         // |   C   |   C   |
34         // |       |       |
35         // +-------+-------+
36         //
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:
46         //
47         //    (a+b)/2 (c+d)/2
48         //   +-------+-------+
49         //   |       |       |
50         //   |   Y   |   Y   |
51         //   |       |       |
52         //   +-------+-------+
53         //
54         // +-------+-------+
55         // |       |       |
56         // |   C   |   C   |
57         // |       |       |
58         // +-------+-------+
59         //
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
62         // source pixels.
63         //
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.
67         //
68         // See also http://www.poynton.com/PDFs/Merging_RGB_and_422.pdf, pages 6–7.
69
70         // Cb/Cr shader.
71         string cbcr_vert_shader =
72                 "#version 130 \n"
73                 " \n"
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"
79                 " \n"
80                 "void main() \n"
81                 "{ \n"
82                 "    // The result of glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0) is: \n"
83                 "    // \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"
92                 "} \n";
93         string cbcr_frag_shader =
94                 "#version 130 \n"
95                 "in vec2 tc0, tc1; \n"
96                 "uniform sampler2D cbcr_tex; \n"
97                 "out vec4 FragColor; \n"
98                 "void main() { \n"
99                 "    FragColor = 0.5 * (texture(cbcr_tex, tc0) + texture(cbcr_tex, tc1)); \n"
100                 "} \n";
101         cbcr_program_num = resource_pool->compile_glsl_program(cbcr_vert_shader, cbcr_frag_shader, frag_shader_outputs);
102         check_error();
103
104         cbcr_texture_sampler_uniform = glGetUniformLocation(cbcr_program_num, "cbcr_tex");
105         check_error();
106         cbcr_position_attribute_index = glGetAttribLocation(cbcr_program_num, "position");
107         check_error();
108         cbcr_texcoord_attribute_index = glGetAttribLocation(cbcr_program_num, "texcoord");
109         check_error();
110
111         // Same, for UYVY conversion.
112         string uyvy_vert_shader =
113                 "#version 130 \n"
114                 " \n"
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"
122                 " \n"
123                 "void main() \n"
124                 "{ \n"
125                 "    // The result of glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0) is: \n"
126                 "    // \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"
137                 "} \n";
138         string uyvy_frag_shader =
139                 "#version 130 \n"
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"
143                 "void main() { \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"  // FIXME: swap y0 and y1?
150                 "} \n";
151
152         uyvy_program_num = resource_pool->compile_glsl_program(uyvy_vert_shader, uyvy_frag_shader, frag_shader_outputs);
153         check_error();
154
155         uyvy_y_texture_sampler_uniform = glGetUniformLocation(uyvy_program_num, "y_tex");
156         check_error();
157         uyvy_cbcr_texture_sampler_uniform = glGetUniformLocation(uyvy_program_num, "cbcr_tex");
158         check_error();
159         uyvy_position_attribute_index = glGetAttribLocation(uyvy_program_num, "position");
160         check_error();
161         uyvy_texcoord_attribute_index = glGetAttribLocation(uyvy_program_num, "texcoord");
162         check_error();
163
164         // Shared between the two.
165         float vertices[] = {
166                 0.0f, 2.0f,
167                 0.0f, 0.0f,
168                 2.0f, 0.0f
169         };
170         vbo = generate_vbo(2, GL_FLOAT, sizeof(vertices), vertices);
171         check_error();
172 }
173
174 ChromaSubsampler::~ChromaSubsampler()
175 {
176         resource_pool->release_glsl_program(cbcr_program_num);
177         check_error();
178         resource_pool->release_glsl_program(uyvy_program_num);
179         check_error();
180         glDeleteBuffers(1, &vbo);
181         check_error();
182 }
183
184 void ChromaSubsampler::subsample_chroma(GLuint cbcr_tex, unsigned width, unsigned height, GLuint dst_tex)
185 {
186         GLuint vao;
187         glGenVertexArrays(1, &vao);
188         check_error();
189
190         glBindVertexArray(vao);
191         check_error();
192
193         // Extract Cb/Cr.
194         GLuint fbo = resource_pool->create_fbo(dst_tex);
195         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
196         glViewport(0, 0, width/2, height/2);
197         check_error();
198
199         glUseProgram(cbcr_program_num);
200         check_error();
201
202         glActiveTexture(GL_TEXTURE0);
203         check_error();
204         glBindTexture(GL_TEXTURE_2D, cbcr_tex);
205         check_error();
206         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
207         check_error();
208         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
209         check_error();
210         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
211         check_error();
212
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);
217
218         glUniform1i(cbcr_texture_sampler_uniform, 0);
219
220         glBindBuffer(GL_ARRAY_BUFFER, vbo);
221         check_error();
222
223         for (GLint attr_index : { cbcr_position_attribute_index, cbcr_texcoord_attribute_index }) {
224                 glEnableVertexAttribArray(attr_index);
225                 check_error();
226                 glVertexAttribPointer(attr_index, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
227                 check_error();
228         }
229
230         glDrawArrays(GL_TRIANGLES, 0, 3);
231         check_error();
232
233         for (GLint attr_index : { cbcr_position_attribute_index, cbcr_texcoord_attribute_index }) {
234                 glDisableVertexAttribArray(attr_index);
235                 check_error();
236         }
237
238         glUseProgram(0);
239         check_error();
240         glBindFramebuffer(GL_FRAMEBUFFER, 0);
241         check_error();
242
243         resource_pool->release_fbo(fbo);
244         glDeleteVertexArrays(1, &vao);
245         check_error();
246 }
247
248 void ChromaSubsampler::create_uyvy(GLuint y_tex, GLuint cbcr_tex, unsigned width, unsigned height, GLuint dst_tex)
249 {
250         GLuint vao;
251         glGenVertexArrays(1, &vao);
252         check_error();
253
254         glBindVertexArray(vao);
255         check_error();
256
257         GLuint fbo = resource_pool->create_fbo(dst_tex);
258         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
259         glViewport(0, 0, width/2, height);
260         check_error();
261
262         glUseProgram(uyvy_program_num);
263         check_error();
264
265         glUniform1i(uyvy_y_texture_sampler_uniform, 0);
266         check_error();
267         glUniform1i(uyvy_cbcr_texture_sampler_uniform, 1);
268         check_error();
269
270         glActiveTexture(GL_TEXTURE0);
271         check_error();
272         glBindTexture(GL_TEXTURE_2D, y_tex);
273         check_error();
274         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
275         check_error();
276         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
277         check_error();
278         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
279         check_error();
280
281         glActiveTexture(GL_TEXTURE1);
282         check_error();
283         glBindTexture(GL_TEXTURE_2D, cbcr_tex);
284         check_error();
285         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
286         check_error();
287         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
288         check_error();
289         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
290         check_error();
291
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);
300
301         glBindBuffer(GL_ARRAY_BUFFER, vbo);
302         check_error();
303
304         for (GLint attr_index : { uyvy_position_attribute_index, uyvy_texcoord_attribute_index }) {
305                 if (attr_index == -1) continue;
306                 glEnableVertexAttribArray(attr_index);
307                 check_error();
308                 glVertexAttribPointer(attr_index, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
309                 check_error();
310         }
311
312         glDrawArrays(GL_TRIANGLES, 0, 3);
313         check_error();
314
315         for (GLint attr_index : { uyvy_position_attribute_index, uyvy_texcoord_attribute_index }) {
316                 if (attr_index == -1) continue;
317                 glDisableVertexAttribArray(attr_index);
318                 check_error();
319         }
320
321         glActiveTexture(GL_TEXTURE0);
322         check_error();
323         glUseProgram(0);
324         check_error();
325         glBindFramebuffer(GL_FRAMEBUFFER, 0);
326         check_error();
327
328         resource_pool->release_fbo(fbo);
329         glDeleteVertexArrays(1, &vao);
330 }