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