Add YCbCr input. Required a bit of reworking of the sRGB extension stuff, but seems...
[movit] / ycbcr_input.cpp
1 #define GL_GLEXT_PROTOTYPES 1
2
3 #include <string.h>
4 #include <GL/gl.h>
5 #include <GL/glext.h>
6 #include <assert.h>
7
8 #include "ycbcr_input.h"
9 #include "util.h"
10
11 YCbCrInput::YCbCrInput(const ImageFormat &image_format,
12                        const YCbCrFormat &ycbcr_format,
13                        unsigned width, unsigned height)
14         : image_format(image_format),
15           ycbcr_format(ycbcr_format),
16           needs_update(false),
17           finalized(false),
18           needs_mipmaps(false),
19           width(width),
20           height(height)
21 {
22         pitch[0] = pitch[1] = pitch[2] = width;
23
24         assert(width % ycbcr_format.chroma_subsampling_x == 0);
25         widths[0] = width;
26         widths[1] = width / ycbcr_format.chroma_subsampling_x;
27         widths[2] = width / ycbcr_format.chroma_subsampling_x;
28
29         assert(height % ycbcr_format.chroma_subsampling_y == 0);
30         heights[0] = height;
31         heights[1] = height / ycbcr_format.chroma_subsampling_y;
32         heights[2] = height / ycbcr_format.chroma_subsampling_y;
33
34         register_int("needs_mipmaps", &needs_mipmaps);
35 }
36
37 void YCbCrInput::finalize()
38 {
39         // Create PBOs to hold the textures holding the input image, and then the texture itself.
40         glGenBuffers(3, pbos);
41         check_error();
42         glGenTextures(3, texture_num);
43         check_error();
44
45         for (unsigned channel = 0; channel < 3; ++channel) {
46                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
47                 check_error();
48                 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, pitch[channel] * heights[channel], NULL, GL_STREAM_DRAW);
49                 check_error();
50                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
51                 check_error();
52                 
53                 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
54                 check_error();
55                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
56                 check_error();
57                 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
58                 check_error();
59                 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE8, widths[channel], heights[channel], 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL);
60                 check_error();
61                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
62                 check_error();
63         }
64
65         needs_update = false;
66         finalized = true;
67 }
68         
69 void YCbCrInput::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
70 {
71         for (unsigned channel = 0; channel < 3; ++channel) {
72                 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
73                 check_error();
74                 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
75                 check_error();
76
77                 if (needs_update) {
78                         // Copy the pixel data into the PBO.
79                         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
80                         check_error();
81                         void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
82                         memcpy(mapped_pbo, pixel_data[channel], pitch[channel] * heights[channel]);
83                         glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
84                         check_error();
85
86                         // Re-upload the texture from the PBO.
87                         glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
88                         check_error();
89                         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], heights[channel], GL_LUMINANCE, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
90                         check_error();
91                         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
92                         check_error();
93                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
94                         check_error();
95                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
96                         check_error();
97                         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
98                         check_error();
99                 }
100         }
101
102         // Bind samplers.
103         set_uniform_int(glsl_program_num, prefix, "tex_y", *sampler_num + 0);
104         set_uniform_int(glsl_program_num, prefix, "tex_cb", *sampler_num + 1);
105         set_uniform_int(glsl_program_num, prefix, "tex_cr", *sampler_num + 2);
106
107         *sampler_num += 3;
108         needs_update = false;
109 }
110
111 std::string YCbCrInput::output_fragment_shader()
112 {
113         float coeff[3], offset[3], scale[3];
114
115         switch (ycbcr_format.luma_coefficients) {
116         case YCBCR_REC_601:
117                 // Rec. 601, page 2.
118                 coeff[0] = 0.299;
119                 coeff[1] = 0.587;
120                 coeff[2] = 0.114;
121                 break;
122
123         case YCBCR_REC_709:
124                 // Rec. 709, page 19.
125                 coeff[0] = 0.2126;
126                 coeff[1] = 0.7152;
127                 coeff[2] = 0.0722;
128                 break;
129         default:
130                 assert(false);
131         }
132
133         if (ycbcr_format.full_range) {
134                 offset[0] = 0.0 / 255.0;
135                 offset[1] = 128.0 / 255.0;
136                 offset[2] = 128.0 / 255.0;
137
138                 scale[0] = 1.0;
139                 scale[1] = 1.0;
140                 scale[2] = 1.0;
141         } else {
142                 // Rec. 601, page 4; Rec. 709, page 19.
143                 offset[0] = 16.0 / 255.0;
144                 offset[1] = 128.0 / 255.0;
145                 offset[2] = 128.0 / 255.0;
146
147                 scale[0] = 255.0 / 219.0;
148                 scale[1] = 255.0 / 224.0;
149                 scale[2] = 255.0 / 224.0;
150         }
151
152         // Matrix to convert RGB to YCbCr. See e.g. Rec. 601.
153         Matrix3x3 rgb_to_ycbcr;
154         rgb_to_ycbcr[0] = coeff[0];
155         rgb_to_ycbcr[3] = coeff[1];
156         rgb_to_ycbcr[6] = coeff[2];
157
158         float cb_fac = (224.0 / 219.0) / (coeff[0] + coeff[1] + 1.0f - coeff[2]);
159         rgb_to_ycbcr[1] = -coeff[0] * cb_fac;
160         rgb_to_ycbcr[4] = -coeff[1] * cb_fac;
161         rgb_to_ycbcr[7] = (1.0f - coeff[2]) * cb_fac;
162
163         float cr_fac = (224.0 / 219.0) / (1.0f - coeff[0] + coeff[1] + coeff[2]);
164         rgb_to_ycbcr[2] = (1.0f - coeff[0]) * cr_fac;
165         rgb_to_ycbcr[5] = -coeff[1] * cr_fac;
166         rgb_to_ycbcr[8] = -coeff[2] * cr_fac;
167
168         // Inverting the matrix gives us what we need to go from YCbCr back to RGB.
169         Matrix3x3 ycbcr_to_rgb;
170         invert_3x3_matrix(rgb_to_ycbcr, ycbcr_to_rgb);
171
172         std::string frag_shader;
173
174         char buf[1024];
175         sprintf(buf,
176                 "const mat3 PREFIX(inv_ycbcr_matrix) = mat3(\n"
177                 "    %.8f, %.8f, %.8f,\n"
178                 "    %.8f, %.8f, %.8f,\n"
179                 "    %.8f, %.8f, %.8f);\n",
180                 ycbcr_to_rgb[0], ycbcr_to_rgb[1], ycbcr_to_rgb[2],
181                 ycbcr_to_rgb[3], ycbcr_to_rgb[4], ycbcr_to_rgb[5],
182                 ycbcr_to_rgb[6], ycbcr_to_rgb[7], ycbcr_to_rgb[8]);
183         frag_shader = buf;
184
185         sprintf(buf, "const vec3 PREFIX(offset) = vec3(%.8f, %.8f, %.8f);\n",
186                 offset[0], offset[1], offset[2]);
187         frag_shader += buf;
188
189         sprintf(buf, "const vec3 PREFIX(scale) = vec3(%.8f, %.8f, %.8f);\n",
190                 scale[0], scale[1], scale[2]);
191         frag_shader += buf;
192
193         // OpenGL has texel center in (0.5, 0.5), but different formats have
194         // chroma in various other places. If luma samples are X, the chroma
195         // sample is *, and subsampling is 3x3, the situation with chroma
196         // center in (0.5, 0.5) looks approximately like this:
197         //
198         //   X     X
199         //      *   
200         //   X     X
201         //
202         // If, on the other hand, chroma center is in (0.0, 0.5) (common
203         // for e.g. MPEG-4), the figure changes to:
204         //
205         //   X     X
206         //   *      
207         //   X     X
208         //
209         // Obviously, the chroma plane here needs to be moved to the left,
210         // which means _adding_ 0.5 to the texture coordinates when sampling
211         // chroma.
212         float chroma_offset_x = (0.5f - ycbcr_format.chroma_x_position) / widths[1];
213         float chroma_offset_y = (0.5f - ycbcr_format.chroma_y_position) / heights[1];
214         sprintf(buf, "const vec2 PREFIX(chroma_offset) = vec2(%.8f, %.8f);\n",
215                 chroma_offset_x, chroma_offset_y);
216         frag_shader += buf;
217
218         frag_shader += read_file("ycbcr_input.frag");
219         return frag_shader;
220 }