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