]> git.sesse.net Git - movit/blob - ycbcr_input.cpp
Check return value from SDL_Init().
[movit] / ycbcr_input.cpp
1 #include <string.h>
2 #include <assert.h>
3 #include <GL/glew.h>
4
5 #include <Eigen/LU>
6
7 #include "ycbcr_input.h"
8 #include "util.h"
9
10 using namespace Eigen;
11
12 namespace {
13
14 // OpenGL has texel center in (0.5, 0.5), but different formats have
15 // chroma in various other places. If luma samples are X, the chroma
16 // sample is *, and subsampling is 3x3, the situation with chroma
17 // center in (0.5, 0.5) looks approximately like this:
18 //
19 //   X   X
20 //     *   
21 //   X   X
22 //
23 // If, on the other hand, chroma center is in (0.0, 0.5) (common
24 // for e.g. MPEG-4), the figure changes to:
25 //
26 //   X   X
27 //   *      
28 //   X   X
29 //
30 // In other words, (0.0, 0.0) means that the chroma sample is exactly
31 // co-sited on top of the top-left luma sample. Note, however, that
32 // this is _not_ 0.5 texels to the left, since the OpenGL's texel center
33 // is in (0.5, 0.5); it is in (0.25, 0.25). In a sense, the four luma samples
34 // define a square where chroma position (0.0, 0.0) is in texel position
35 // (0.25, 0.25) and chroma position (1.0, 1.0) is in texel position (0.75, 0.75)
36 // (the outer border shows the borders of the texel itself, ie. from
37 // (0, 0) to (1, 1)):
38 //
39 //  ---------
40 // |         |
41 // |  X---X  |
42 // |  | * |  |
43 // |  X---X  |
44 // |         |
45 //  ---------
46 //
47 // Also note that if we have no subsampling, the square will have zero
48 // area and the chroma position does not matter at all.
49 float compute_chroma_offset(float pos, unsigned subsampling_factor, unsigned resolution)
50 {
51         float local_chroma_pos = (0.5 + pos * (subsampling_factor - 1)) / subsampling_factor;
52         return (0.5 - local_chroma_pos) / resolution;
53 }
54
55 }  // namespace
56
57 YCbCrInput::YCbCrInput(const ImageFormat &image_format,
58                        const YCbCrFormat &ycbcr_format,
59                        unsigned width, unsigned height)
60         : image_format(image_format),
61           ycbcr_format(ycbcr_format),
62           needs_update(false),
63           needs_pbo_recreate(false),
64           finalized(false),
65           needs_mipmaps(false),
66           width(width),
67           height(height)
68 {
69         pbos[0] = pbos[1] = pbos[2] = 0;
70         texture_num[0] = texture_num[1] = texture_num[2] = 0;
71
72         assert(width % ycbcr_format.chroma_subsampling_x == 0);
73         pitch[0] = widths[0] = width;
74         pitch[1] = widths[1] = width / ycbcr_format.chroma_subsampling_x;
75         pitch[2] = widths[2] = width / ycbcr_format.chroma_subsampling_x;
76
77         assert(height % ycbcr_format.chroma_subsampling_y == 0);
78         heights[0] = height;
79         heights[1] = height / ycbcr_format.chroma_subsampling_y;
80         heights[2] = height / ycbcr_format.chroma_subsampling_y;
81
82         pixel_data[0] = pixel_data[1] = pixel_data[2] = NULL;
83
84         register_int("needs_mipmaps", &needs_mipmaps);
85 }
86
87 YCbCrInput::~YCbCrInput()
88 {
89         if (pbos[0] != 0) {
90                 glDeleteBuffers(3, pbos);
91                 check_error();
92         }
93         if (texture_num[0] != 0) {
94                 glDeleteTextures(3, texture_num);
95                 check_error();
96         }
97 }
98
99 void YCbCrInput::finalize()
100 {
101         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
102         check_error();
103
104         // Create PBOs to hold the textures holding the input image, and then the texture itself.
105         glGenBuffers(3, pbos);
106         check_error();
107         glGenTextures(3, texture_num);
108         check_error();
109
110         for (unsigned channel = 0; channel < 3; ++channel) {
111                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
112                 check_error();
113                 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, pitch[channel] * heights[channel], NULL, GL_STREAM_DRAW);
114                 check_error();
115                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
116                 check_error();
117                 
118                 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
119                 check_error();
120                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
121                 check_error();
122                 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
123                 check_error();
124                 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE8, widths[channel], heights[channel], 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL);
125                 check_error();
126                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
127                 check_error();
128         }
129
130         needs_update = true;
131         finalized = true;
132 }
133         
134 void YCbCrInput::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
135 {
136         for (unsigned channel = 0; channel < 3; ++channel) {
137                 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
138                 check_error();
139                 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
140                 check_error();
141
142                 if (needs_update || needs_pbo_recreate) {
143                         // Copy the pixel data into the PBO.
144                         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
145                         check_error();
146
147                         if (needs_pbo_recreate) {
148                                 // The pitch has changed; we need to reallocate this PBO.
149                                 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, pitch[channel] * heights[channel], NULL, GL_STREAM_DRAW);
150                                 check_error();
151                         }
152
153                         void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
154                         memcpy(mapped_pbo, pixel_data[channel], pitch[channel] * heights[channel]);
155
156                         glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
157                         check_error();
158
159                         // Re-upload the texture from the PBO.
160                         glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
161                         check_error();
162                         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], heights[channel], GL_LUMINANCE, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
163                         check_error();
164                         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
165                         check_error();
166                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
167                         check_error();
168                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
169                         check_error();
170                         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
171                         check_error();
172                 }
173         }
174
175         // Bind samplers.
176         set_uniform_int(glsl_program_num, prefix, "tex_y", *sampler_num + 0);
177         set_uniform_int(glsl_program_num, prefix, "tex_cb", *sampler_num + 1);
178         set_uniform_int(glsl_program_num, prefix, "tex_cr", *sampler_num + 2);
179
180         *sampler_num += 3;
181         needs_update = false;
182         needs_pbo_recreate = false;
183 }
184
185 std::string YCbCrInput::output_fragment_shader()
186 {
187         float coeff[3], offset[3], scale[3];
188
189         switch (ycbcr_format.luma_coefficients) {
190         case YCBCR_REC_601:
191                 // Rec. 601, page 2.
192                 coeff[0] = 0.299;
193                 coeff[1] = 0.587;
194                 coeff[2] = 0.114;
195                 break;
196
197         case YCBCR_REC_709:
198                 // Rec. 709, page 19.
199                 coeff[0] = 0.2126;
200                 coeff[1] = 0.7152;
201                 coeff[2] = 0.0722;
202                 break;
203         default:
204                 assert(false);
205         }
206
207         if (ycbcr_format.full_range) {
208                 offset[0] = 0.0 / 255.0;
209                 offset[1] = 128.0 / 255.0;
210                 offset[2] = 128.0 / 255.0;
211
212                 scale[0] = 1.0;
213                 scale[1] = 1.0;
214                 scale[2] = 1.0;
215         } else {
216                 // Rec. 601, page 4; Rec. 709, page 19.
217                 offset[0] = 16.0 / 255.0;
218                 offset[1] = 128.0 / 255.0;
219                 offset[2] = 128.0 / 255.0;
220
221                 scale[0] = 255.0 / 219.0;
222                 scale[1] = 255.0 / 224.0;
223                 scale[2] = 255.0 / 224.0;
224         }
225
226         // Matrix to convert RGB to YCbCr. See e.g. Rec. 601.
227         Matrix3d rgb_to_ycbcr;
228         rgb_to_ycbcr(0,0) = coeff[0];
229         rgb_to_ycbcr(0,1) = coeff[1];
230         rgb_to_ycbcr(0,2) = coeff[2];
231
232         float cb_fac = (224.0 / 219.0) / (coeff[0] + coeff[1] + 1.0f - coeff[2]);
233         rgb_to_ycbcr(1,0) = -coeff[0] * cb_fac;
234         rgb_to_ycbcr(1,1) = -coeff[1] * cb_fac;
235         rgb_to_ycbcr(1,2) = (1.0f - coeff[2]) * cb_fac;
236
237         float cr_fac = (224.0 / 219.0) / (1.0f - coeff[0] + coeff[1] + coeff[2]);
238         rgb_to_ycbcr(2,0) = (1.0f - coeff[0]) * cr_fac;
239         rgb_to_ycbcr(2,1) = -coeff[1] * cr_fac;
240         rgb_to_ycbcr(2,2) = -coeff[2] * cr_fac;
241
242         // Inverting the matrix gives us what we need to go from YCbCr back to RGB.
243         Matrix3d ycbcr_to_rgb = rgb_to_ycbcr.inverse();
244
245         std::string frag_shader;
246
247         frag_shader = output_glsl_mat3("PREFIX(inv_ycbcr_matrix)", ycbcr_to_rgb);
248
249         char buf[256];
250         sprintf(buf, "const vec3 PREFIX(offset) = vec3(%.8f, %.8f, %.8f);\n",
251                 offset[0], offset[1], offset[2]);
252         frag_shader += buf;
253
254         sprintf(buf, "const vec3 PREFIX(scale) = vec3(%.8f, %.8f, %.8f);\n",
255                 scale[0], scale[1], scale[2]);
256         frag_shader += buf;
257
258         float cb_offset_x = compute_chroma_offset(
259                 ycbcr_format.cb_x_position, ycbcr_format.chroma_subsampling_x, widths[1]);
260         float cb_offset_y = compute_chroma_offset(
261                 ycbcr_format.cb_y_position, ycbcr_format.chroma_subsampling_y, heights[1]);
262         sprintf(buf, "const vec2 PREFIX(cb_offset) = vec2(%.8f, %.8f);\n",
263                 cb_offset_x, cb_offset_y);
264         frag_shader += buf;
265
266         float cr_offset_x = compute_chroma_offset(
267                 ycbcr_format.cr_x_position, ycbcr_format.chroma_subsampling_x, widths[2]);
268         float cr_offset_y = compute_chroma_offset(
269                 ycbcr_format.cr_y_position, ycbcr_format.chroma_subsampling_y, heights[2]);
270         sprintf(buf, "const vec2 PREFIX(cr_offset) = vec2(%.8f, %.8f);\n",
271                 cr_offset_x, cr_offset_y);
272         frag_shader += buf;
273
274         frag_shader += read_file("ycbcr_input.frag");
275         return frag_shader;
276 }