e725750291b4e2ce8f7f71533175e83f8dfac469
[movit] / ycbcr_input.cpp
1 #include <Eigen/Core>
2 #include <Eigen/LU>
3 #include <epoxy/gl.h>
4 #include <assert.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 #include "effect_util.h"
9 #include "resource_pool.h"
10 #include "util.h"
11 #include "ycbcr.h"
12 #include "ycbcr_input.h"
13
14 using namespace Eigen;
15 using namespace std;
16
17 namespace movit {
18
19 YCbCrInput::YCbCrInput(const ImageFormat &image_format,
20                        const YCbCrFormat &ycbcr_format,
21                        unsigned width, unsigned height,
22                        YCbCrInputSplitting ycbcr_input_splitting,
23                        GLenum type)
24         : image_format(image_format),
25           ycbcr_format(ycbcr_format),
26           ycbcr_input_splitting(ycbcr_input_splitting),
27           needs_mipmaps(false),
28           type(type),
29           width(width),
30           height(height),
31           resource_pool(NULL)
32 {
33         pbos[0] = pbos[1] = pbos[2] = 0;
34         texture_num[0] = texture_num[1] = texture_num[2] = 0;
35
36         set_width(width);
37         set_height(height);
38
39         pixel_data[0] = pixel_data[1] = pixel_data[2] = NULL;
40         owns_texture[0] = owns_texture[1] = owns_texture[2] = false;
41
42         register_uniform_sampler2d("tex_y", &uniform_tex_y);
43
44         if (ycbcr_input_splitting == YCBCR_INPUT_INTERLEAVED) {
45                 num_channels = 1;
46                 assert(ycbcr_format.chroma_subsampling_x == 1);
47                 assert(ycbcr_format.chroma_subsampling_y == 1);
48         } else if (ycbcr_input_splitting == YCBCR_INPUT_SPLIT_Y_AND_CBCR) {
49                 num_channels = 2;
50                 register_uniform_sampler2d("tex_cbcr", &uniform_tex_cb);
51         } else {
52                 assert(ycbcr_input_splitting == YCBCR_INPUT_PLANAR);
53                 num_channels = 3;
54                 register_uniform_sampler2d("tex_cb", &uniform_tex_cb);
55                 register_uniform_sampler2d("tex_cr", &uniform_tex_cr);
56         }
57
58         register_int("needs_mipmaps", &needs_mipmaps);
59 }
60
61 YCbCrInput::~YCbCrInput()
62 {
63         for (unsigned channel = 0; channel < num_channels; ++channel) {
64                 possibly_release_texture(channel);
65         }
66 }
67
68 void YCbCrInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
69 {
70         for (unsigned channel = 0; channel < num_channels; ++channel) {
71                 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
72                 check_error();
73
74                 if (texture_num[channel] == 0 && (pbos[channel] != 0 || pixel_data[channel] != NULL)) {
75                         GLenum format, internal_format;
76                         if (channel == 0 && ycbcr_input_splitting == YCBCR_INPUT_INTERLEAVED) {
77                                 if (type == GL_UNSIGNED_INT_2_10_10_10_REV) {
78                                         format = GL_RGBA;
79                                         internal_format = GL_RGB10_A2;
80                                 } else if (type == GL_UNSIGNED_SHORT) {
81                                         format = GL_RGB;
82                                         internal_format = GL_RGB16;
83                                 } else {
84                                         assert(type == GL_UNSIGNED_BYTE);
85                                         format = GL_RGB;
86                                         internal_format = GL_RGB8;
87                                 }
88                         } else if (channel == 1 && ycbcr_input_splitting == YCBCR_INPUT_SPLIT_Y_AND_CBCR) {
89                                 format = GL_RG;
90                                 if (type == GL_UNSIGNED_SHORT) {
91                                         internal_format = GL_RG16;
92                                 } else {
93                                         assert(type == GL_UNSIGNED_BYTE);
94                                         internal_format = GL_RG8;
95                                 }
96                         } else {
97                                 format = GL_RED;
98                                 if (type == GL_UNSIGNED_SHORT) {
99                                         internal_format = GL_R16;
100                                 } else {
101                                         assert(type == GL_UNSIGNED_BYTE);
102                                         internal_format = GL_R8;
103                                 }
104                         }
105
106                         // (Re-)upload the texture.
107                         texture_num[channel] = resource_pool->create_2d_texture(internal_format, widths[channel], heights[channel]);
108                         glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
109                         check_error();
110                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
111                         check_error();
112                         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
113                         check_error();
114                         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
115                         check_error();
116                         glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
117                         check_error();
118                         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], heights[channel], format, type, pixel_data[channel]);
119                         check_error();
120                         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
121                         check_error();
122                         if (needs_mipmaps) {
123                                 glGenerateMipmap(GL_TEXTURE_2D);
124                                 check_error();
125                         }
126                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
127                         check_error();
128                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
129                         check_error();
130                         owns_texture[channel] = true;
131                 } else {
132                         glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
133                         check_error();
134                 }
135         }
136
137         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
138         check_error();
139
140         // Bind samplers.
141         uniform_tex_y = *sampler_num + 0;
142         if (ycbcr_input_splitting != YCBCR_INPUT_INTERLEAVED) {
143                 uniform_tex_cb = *sampler_num + 1;
144         }
145         if (ycbcr_input_splitting == YCBCR_INPUT_PLANAR) {
146                 uniform_tex_cr = *sampler_num + 2;
147         }
148
149         *sampler_num += num_channels;
150 }
151
152 string YCbCrInput::output_fragment_shader()
153 {
154         float offset[3];
155         Matrix3d ycbcr_to_rgb;
156         compute_ycbcr_matrix(ycbcr_format, offset, &ycbcr_to_rgb, type);
157
158         string frag_shader;
159
160         frag_shader = output_glsl_mat3("PREFIX(inv_ycbcr_matrix)", ycbcr_to_rgb);
161         frag_shader += output_glsl_vec3("PREFIX(offset)", offset[0], offset[1], offset[2]);
162
163         float cb_offset_x = compute_chroma_offset(
164                 ycbcr_format.cb_x_position, ycbcr_format.chroma_subsampling_x, widths[1]);
165         float cb_offset_y = compute_chroma_offset(
166                 ycbcr_format.cb_y_position, ycbcr_format.chroma_subsampling_y, heights[1]);
167         frag_shader += output_glsl_vec2("PREFIX(cb_offset)", cb_offset_x, cb_offset_y);
168
169         float cr_offset_x = compute_chroma_offset(
170                 ycbcr_format.cr_x_position, ycbcr_format.chroma_subsampling_x, widths[2]);
171         float cr_offset_y = compute_chroma_offset(
172                 ycbcr_format.cr_y_position, ycbcr_format.chroma_subsampling_y, heights[2]);
173         frag_shader += output_glsl_vec2("PREFIX(cr_offset)", cr_offset_x, cr_offset_y);
174
175         if (ycbcr_input_splitting == YCBCR_INPUT_INTERLEAVED) {
176                 frag_shader += "#define Y_CB_CR_SAME_TEXTURE 1\n";
177         } else if (ycbcr_input_splitting == YCBCR_INPUT_SPLIT_Y_AND_CBCR) {
178                 char buf[256];
179                 snprintf(buf, sizeof(buf), "#define Y_CB_CR_SAME_TEXTURE 0\n#define CB_CR_SAME_TEXTURE 1\n#define CB_CR_OFFSETS_EQUAL %d\n",
180                         (fabs(ycbcr_format.cb_x_position - ycbcr_format.cr_x_position) < 1e-6));
181                 frag_shader += buf;
182         } else {
183                 frag_shader += "#define Y_CB_CR_SAME_TEXTURE 0\n#define CB_CR_SAME_TEXTURE 0\n";
184         }
185
186         frag_shader += read_file("ycbcr_input.frag");
187         return frag_shader;
188 }
189
190 void YCbCrInput::invalidate_pixel_data()
191 {
192         for (unsigned channel = 0; channel < 3; ++channel) {
193                 possibly_release_texture(channel);
194         }
195 }
196
197 bool YCbCrInput::set_int(const std::string& key, int value)
198 {
199         if (key == "needs_mipmaps") {
200                 if (ycbcr_input_splitting != YCBCR_INPUT_INTERLEAVED && value != 0) {
201                         // We do not currently support this.
202                         return false;
203                 }
204         }
205         return Effect::set_int(key, value);
206 }
207
208 void YCbCrInput::possibly_release_texture(unsigned channel)
209 {
210         if (texture_num[channel] != 0 && owns_texture[channel]) {
211                 resource_pool->release_2d_texture(texture_num[channel]);
212                 texture_num[channel] = 0;
213                 owns_texture[channel] = false;
214         }
215 }
216
217 }  // namespace movit