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