]> git.sesse.net Git - nageru/blob - chroma_subsampler.cpp
04fd5945f0f3b80ae3fc1c5edc0201f432377283
[nageru] / chroma_subsampler.cpp
1 #include "chroma_subsampler.h"
2
3 #include <movit/util.h>
4 #include <string>
5
6 #define BUFFER_OFFSET(i) ((char *)nullptr + (i))
7
8 using namespace std;
9
10 string read_file(const string &filename);
11 GLuint compile_shader(const string &shader_src, GLenum type);
12 GLuint link_program(GLuint vs_obj, GLuint fs_obj);
13 void bind_sampler(GLuint program, GLint location, GLuint texture_unit, GLuint tex, GLuint sampler);
14
15 extern GLuint linear_sampler;
16
17 ChromaSubsampler::ChromaSubsampler()
18 {
19         // Set up stuff for 4:2:2 conversion.
20         //
21         // Note: Due to the horizontally co-sited chroma/luma samples in H.264
22         // (chroma position is left for horizontal),
23         // we need to be a bit careful in our subsampling. A diagram will make
24         // this clearer, showing some luma and chroma samples:
25         //
26         //     a   b   c   d
27         //   +---+---+---+---+
28         //   |   |   |   |   |
29         //   | Y | Y | Y | Y |
30         //   |   |   |   |   |
31         //   +---+---+---+---+
32         //
33         // +-------+-------+
34         // |       |       |
35         // |   C   |   C   |
36         // |       |       |
37         // +-------+-------+
38         //
39         // Clearly, the rightmost chroma sample here needs to be equivalent to
40         // b/4 + c/2 + d/4. (We could also implement more sophisticated filters,
41         // of course, but as long as the upsampling is not going to be equally
42         // sophisticated, it's probably not worth it.) If we sample once with
43         // no mipmapping, we get just c, ie., no actual filtering in the
44         // horizontal direction. (For the vertical direction, we can just
45         // sample in the middle to get the right filtering.) One could imagine
46         // we could use mipmapping (assuming we can create mipmaps cheaply),
47         // but then, what we'd get is this:
48         //
49         //    (a+b)/2 (c+d)/2
50         //   +-------+-------+
51         //   |       |       |
52         //   |   Y   |   Y   |
53         //   |       |       |
54         //   +-------+-------+
55         //
56         // +-------+-------+
57         // |       |       |
58         // |   C   |   C   |
59         // |       |       |
60         // +-------+-------+
61         //
62         // which ends up sampling equally from a and b, which clearly isn't right. Instead,
63         // we need to do two (non-mipmapped) chroma samples, both hitting exactly in-between
64         // source pixels.
65         //
66         // Sampling in-between b and c gives us the sample (b+c)/2, and similarly for c and d.
67         // Taking the average of these gives of (b+c)/4 + (c+d)/4 = b/4 + c/2 + d/4, which is
68         // exactly what we want.
69         //
70         // See also http://www.poynton.com/PDFs/Merging_RGB_and_422.pdf, pages 6–7.
71
72         cbcr_vs_obj = compile_shader(read_file("chroma_subsample.vert"), GL_VERTEX_SHADER);
73         cbcr_fs_obj = compile_shader(read_file("chroma_subsample.frag"), GL_FRAGMENT_SHADER);
74         cbcr_program = link_program(cbcr_vs_obj, cbcr_fs_obj);
75
76         // Set up the VAO containing all the required position data.
77         glCreateVertexArrays(1, &vao);
78         glBindVertexArray(vao);
79
80         float vertices[] = {
81                 0.0f, 2.0f,
82                 0.0f, 0.0f,
83                 2.0f, 0.0f
84         };
85         glCreateBuffers(1, &vbo);
86         glNamedBufferData(vbo, sizeof(vertices), vertices, GL_STATIC_DRAW);
87         glBindBuffer(GL_ARRAY_BUFFER, vbo);
88
89         GLint position_attrib = 0;  // Hard-coded in every vertex shader.
90         glEnableVertexArrayAttrib(vao, position_attrib);
91         glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
92
93         uniform_cbcr_tex = glGetUniformLocation(cbcr_program, "cbcr_tex");
94         uniform_chroma_offset_0 = glGetUniformLocation(cbcr_program, "chroma_offset_0");
95         uniform_chroma_offset_1 = glGetUniformLocation(cbcr_program, "chroma_offset_1");
96 }
97
98 ChromaSubsampler::~ChromaSubsampler()
99 {
100         glDeleteProgram(cbcr_program);
101         check_error();
102         glDeleteBuffers(1, &vbo);
103         check_error();
104         glDeleteVertexArrays(1, &vao);
105         check_error();
106 }
107
108 void ChromaSubsampler::subsample_chroma(GLuint cbcr_tex, unsigned width, unsigned height, GLuint cb_tex, GLuint cr_tex)
109 {
110         glUseProgram(cbcr_program);
111         bind_sampler(cbcr_program, uniform_cbcr_tex, 0, cbcr_tex, linear_sampler);
112         glProgramUniform2f(cbcr_program, uniform_chroma_offset_0, -1.0f / width, 0.0f);
113         glProgramUniform2f(cbcr_program, uniform_chroma_offset_1, -0.0f / width, 0.0f);
114
115         glViewport(0, 0, width / 2, height);
116         fbos.render_to(cb_tex, cr_tex);
117
118         glBindVertexArray(vao);
119         glDrawArrays(GL_TRIANGLES, 0, 3);
120 }