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