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