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