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