]> git.sesse.net Git - movit/blob - ycbcr_input.cpp
Loosen up some restrictions on YCbCrInput if we have interleaved mode.
[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           type(type),
28           width(width),
29           height(height),
30           resource_pool(NULL)
31 {
32         pbos[0] = pbos[1] = pbos[2] = 0;
33         texture_num[0] = texture_num[1] = texture_num[2] = 0;
34
35         set_width(width);
36         set_height(height);
37
38         pixel_data[0] = pixel_data[1] = pixel_data[2] = NULL;
39         owns_texture[0] = owns_texture[1] = owns_texture[2] = false;
40
41         register_uniform_sampler2d("tex_y", &uniform_tex_y);
42
43         if (ycbcr_input_splitting == YCBCR_INPUT_INTERLEAVED) {
44                 num_channels = 1;
45                 assert(ycbcr_format.chroma_subsampling_x == 1);
46                 assert(ycbcr_format.chroma_subsampling_y == 1);
47         } else if (ycbcr_input_splitting == YCBCR_INPUT_SPLIT_Y_AND_CBCR) {
48                 num_channels = 2;
49                 register_uniform_sampler2d("tex_cbcr", &uniform_tex_cb);
50         } else {
51                 assert(ycbcr_input_splitting == YCBCR_INPUT_PLANAR);
52                 num_channels = 3;
53                 register_uniform_sampler2d("tex_cb", &uniform_tex_cb);
54                 register_uniform_sampler2d("tex_cr", &uniform_tex_cr);
55         }
56 }
57
58 YCbCrInput::~YCbCrInput()
59 {
60         for (unsigned channel = 0; channel < num_channels; ++channel) {
61                 possibly_release_texture(channel);
62         }
63 }
64
65 void YCbCrInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
66 {
67         for (unsigned channel = 0; channel < num_channels; ++channel) {
68                 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
69                 check_error();
70
71                 if (texture_num[channel] == 0 && (pbos[channel] != 0 || pixel_data[channel] != NULL)) {
72                         GLenum format, internal_format;
73                         if (channel == 0 && ycbcr_input_splitting == YCBCR_INPUT_INTERLEAVED) {
74                                 if (type == GL_UNSIGNED_INT_2_10_10_10_REV) {
75                                         format = GL_RGBA;
76                                         internal_format = GL_RGB10_A2;
77                                 } else if (type == GL_UNSIGNED_SHORT) {
78                                         format = GL_RGB;
79                                         internal_format = GL_RGB16;
80                                 } else {
81                                         assert(type == GL_UNSIGNED_BYTE);
82                                         format = GL_RGB;
83                                         internal_format = GL_RGB8;
84                                 }
85                         } else if (channel == 1 && ycbcr_input_splitting == YCBCR_INPUT_SPLIT_Y_AND_CBCR) {
86                                 format = GL_RG;
87                                 if (type == GL_UNSIGNED_SHORT) {
88                                         internal_format = GL_RG16;
89                                 } else {
90                                         assert(type == GL_UNSIGNED_BYTE);
91                                         internal_format = GL_RG8;
92                                 }
93                         } else {
94                                 format = GL_RED;
95                                 if (type == GL_UNSIGNED_SHORT) {
96                                         internal_format = GL_R16;
97                                 } else {
98                                         assert(type == GL_UNSIGNED_BYTE);
99                                         internal_format = GL_R8;
100                                 }
101                         }
102
103                         // (Re-)upload the texture.
104                         texture_num[channel] = resource_pool->create_2d_texture(internal_format, widths[channel], heights[channel]);
105                         glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
106                         check_error();
107                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
108                         check_error();
109                         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
110                         check_error();
111                         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
112                         check_error();
113                         glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
114                         check_error();
115                         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], heights[channel], format, type, pixel_data[channel]);
116                         check_error();
117                         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
118                         check_error();
119                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
120                         check_error();
121                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
122                         check_error();
123                         owns_texture[channel] = true;
124                 } else {
125                         glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
126                         check_error();
127                 }
128         }
129
130         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
131         check_error();
132
133         // Bind samplers.
134         uniform_tex_y = *sampler_num + 0;
135         uniform_tex_cb = *sampler_num + 1;
136         if (ycbcr_input_splitting == YCBCR_INPUT_PLANAR) {
137                 uniform_tex_cr = *sampler_num + 2;
138         }
139
140         *sampler_num += num_channels;
141 }
142
143 string YCbCrInput::output_fragment_shader()
144 {
145         float offset[3];
146         Matrix3d ycbcr_to_rgb;
147         compute_ycbcr_matrix(ycbcr_format, offset, &ycbcr_to_rgb);
148
149         if (type == GL_UNSIGNED_SHORT) {
150                 // For 10-bit or 12-bit packed into 16-bit, we need to scale the values
151                 // so that the max value goes from 1023 (or 4095) to 65535. We do this
152                 // by folding the scaling into the conversion matrix, so it comes essentially
153                 // for free. However, the offset is before the scaling (and thus assumes
154                 // correctly scaled values), so we need to adjust that the other way.
155                 double scale = 65535.0 / (ycbcr_format.num_levels - 1);
156                 offset[0] /= scale;
157                 offset[1] /= scale;
158                 offset[2] /= scale;
159                 ycbcr_to_rgb *= scale;
160         }
161
162         string frag_shader;
163
164         frag_shader = output_glsl_mat3("PREFIX(inv_ycbcr_matrix)", ycbcr_to_rgb);
165         frag_shader += output_glsl_vec3("PREFIX(offset)", offset[0], offset[1], offset[2]);
166
167         float cb_offset_x = compute_chroma_offset(
168                 ycbcr_format.cb_x_position, ycbcr_format.chroma_subsampling_x, widths[1]);
169         float cb_offset_y = compute_chroma_offset(
170                 ycbcr_format.cb_y_position, ycbcr_format.chroma_subsampling_y, heights[1]);
171         frag_shader += output_glsl_vec2("PREFIX(cb_offset)", cb_offset_x, cb_offset_y);
172
173         float cr_offset_x = compute_chroma_offset(
174                 ycbcr_format.cr_x_position, ycbcr_format.chroma_subsampling_x, widths[2]);
175         float cr_offset_y = compute_chroma_offset(
176                 ycbcr_format.cr_y_position, ycbcr_format.chroma_subsampling_y, heights[2]);
177         frag_shader += output_glsl_vec2("PREFIX(cr_offset)", cr_offset_x, cr_offset_y);
178
179         if (ycbcr_input_splitting == YCBCR_INPUT_INTERLEAVED) {
180                 frag_shader += "#define Y_CB_CR_SAME_TEXTURE 1\n";
181         } else if (ycbcr_input_splitting == YCBCR_INPUT_SPLIT_Y_AND_CBCR) {
182                 char buf[256];
183                 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",
184                         (fabs(ycbcr_format.cb_x_position - ycbcr_format.cr_x_position) < 1e-6));
185                 frag_shader += buf;
186         } else {
187                 frag_shader += "#define Y_CB_CR_SAME_TEXTURE 0\n#define CB_CR_SAME_TEXTURE 0\n";
188         }
189
190         frag_shader += read_file("ycbcr_input.frag");
191         return frag_shader;
192 }
193
194 void YCbCrInput::invalidate_pixel_data()
195 {
196         for (unsigned channel = 0; channel < 3; ++channel) {
197                 possibly_release_texture(channel);
198         }
199 }
200
201 bool YCbCrInput::set_int(const std::string& key, int value)
202 {
203         if (key == "needs_mipmaps") {
204                 // We currently do not support this.
205                 return (value == 0);
206         }
207         return Effect::set_int(key, value);
208 }
209
210 void YCbCrInput::possibly_release_texture(unsigned channel)
211 {
212         if (texture_num[channel] != 0 && owns_texture[channel]) {
213                 resource_pool->release_2d_texture(texture_num[channel]);
214                 texture_num[channel] = 0;
215                 owns_texture[channel] = false;
216         }
217 }
218
219 }  // namespace movit