]> git.sesse.net Git - movit/blob - ycbcr_input.cpp
Get rid of a bunch of STL inefficiencies in FBO freelist handling.
[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         : image_format(image_format),
24           ycbcr_format(ycbcr_format),
25           ycbcr_input_splitting(ycbcr_input_splitting),
26           width(width),
27           height(height),
28           resource_pool(NULL)
29 {
30         pbos[0] = pbos[1] = pbos[2] = 0;
31         texture_num[0] = texture_num[1] = texture_num[2] = 0;
32
33         assert(width % ycbcr_format.chroma_subsampling_x == 0);
34         pitch[0] = widths[0] = width;
35         pitch[1] = widths[1] = width / ycbcr_format.chroma_subsampling_x;
36         pitch[2] = widths[2] = width / ycbcr_format.chroma_subsampling_x;
37
38         assert(height % ycbcr_format.chroma_subsampling_y == 0);
39         heights[0] = height;
40         heights[1] = height / ycbcr_format.chroma_subsampling_y;
41         heights[2] = height / ycbcr_format.chroma_subsampling_y;
42
43         pixel_data[0] = pixel_data[1] = pixel_data[2] = NULL;
44         owns_texture[0] = owns_texture[1] = owns_texture[2] = false;
45
46         register_uniform_sampler2d("tex_y", &uniform_tex_y);
47
48         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
59 YCbCrInput::~YCbCrInput()
60 {
61         for (unsigned channel = 0; channel < num_channels; ++channel) {
62                 possibly_release_texture(channel);
63         }
64 }
65
66 void YCbCrInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
67 {
68         for (unsigned channel = 0; channel < num_channels; ++channel) {
69                 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
70                 check_error();
71
72                 if (texture_num[channel] == 0) {
73                         GLenum format, internal_format;
74                         if (channel == 1 && ycbcr_input_splitting == YCBCR_INPUT_SPLIT_Y_AND_CBCR) {
75                                 format = GL_RG;
76                                 internal_format = GL_RG8;
77                         } else {
78                                 format = GL_RED;
79                                 internal_format = GL_R8;
80                         }
81
82                         // (Re-)upload the texture.
83                         texture_num[channel] = resource_pool->create_2d_texture(internal_format, widths[channel], heights[channel]);
84                         glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
85                         check_error();
86                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
87                         check_error();
88                         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
89                         check_error();
90                         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
91                         check_error();
92                         glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
93                         check_error();
94                         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], heights[channel], format, GL_UNSIGNED_BYTE, pixel_data[channel]);
95                         check_error();
96                         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
97                         check_error();
98                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
99                         check_error();
100                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
101                         check_error();
102                         owns_texture[channel] = true;
103                 } else {
104                         glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
105                         check_error();
106                 }
107         }
108
109         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
110         check_error();
111
112         // Bind samplers.
113         uniform_tex_y = *sampler_num + 0;
114         uniform_tex_cb = *sampler_num + 1;
115         if (ycbcr_input_splitting == YCBCR_INPUT_PLANAR) {
116                 uniform_tex_cr = *sampler_num + 2;
117         }
118
119         *sampler_num += num_channels;
120 }
121
122 string YCbCrInput::output_fragment_shader()
123 {
124         float offset[3];
125         Matrix3d ycbcr_to_rgb;
126         compute_ycbcr_matrix(ycbcr_format, offset, &ycbcr_to_rgb);
127
128         string frag_shader;
129
130         frag_shader = output_glsl_mat3("PREFIX(inv_ycbcr_matrix)", ycbcr_to_rgb);
131         frag_shader += output_glsl_vec3("PREFIX(offset)", offset[0], offset[1], offset[2]);
132
133         float cb_offset_x = compute_chroma_offset(
134                 ycbcr_format.cb_x_position, ycbcr_format.chroma_subsampling_x, widths[1]);
135         float cb_offset_y = compute_chroma_offset(
136                 ycbcr_format.cb_y_position, ycbcr_format.chroma_subsampling_y, heights[1]);
137         frag_shader += output_glsl_vec2("PREFIX(cb_offset)", cb_offset_x, cb_offset_y);
138
139         float cr_offset_x = compute_chroma_offset(
140                 ycbcr_format.cr_x_position, ycbcr_format.chroma_subsampling_x, widths[2]);
141         float cr_offset_y = compute_chroma_offset(
142                 ycbcr_format.cr_y_position, ycbcr_format.chroma_subsampling_y, heights[2]);
143         frag_shader += output_glsl_vec2("PREFIX(cr_offset)", cr_offset_x, cr_offset_y);
144
145         if (ycbcr_input_splitting == YCBCR_INPUT_SPLIT_Y_AND_CBCR) {
146                 char buf[256];
147                 snprintf(buf, sizeof(buf), "#define CB_CR_SAME_TEXTURE 1\n#define CB_CR_OFFSETS_EQUAL %d\n",
148                         (fabs(ycbcr_format.cb_x_position - ycbcr_format.cr_x_position) < 1e-6));
149                 frag_shader += buf;
150         } else {
151                 frag_shader += "#define CB_CR_SAME_TEXTURE 0\n";
152         }
153
154         frag_shader += read_file("ycbcr_input.frag");
155         return frag_shader;
156 }
157
158 void YCbCrInput::invalidate_pixel_data()
159 {
160         for (unsigned channel = 0; channel < 3; ++channel) {
161                 possibly_release_texture(channel);
162         }
163 }
164
165 bool YCbCrInput::set_int(const std::string& key, int value)
166 {
167         if (key == "needs_mipmaps") {
168                 // We currently do not support this.
169                 return (value == 0);
170         }
171         return Effect::set_int(key, value);
172 }
173
174 void YCbCrInput::possibly_release_texture(unsigned channel)
175 {
176         if (texture_num[channel] != 0 && owns_texture[channel]) {
177                 resource_pool->release_2d_texture(texture_num[channel]);
178                 texture_num[channel] = 0;
179                 owns_texture[channel] = false;
180         }
181 }
182
183 }  // namespace movit