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