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