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