]> git.sesse.net Git - movit/blob - ycbcr_input.cpp
Minor optimization in ResampleEffect: Set less GL state.
[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         : image_format(image_format),
23           ycbcr_format(ycbcr_format),
24           width(width),
25           height(height),
26           resource_pool(NULL)
27 {
28         pbos[0] = pbos[1] = pbos[2] = 0;
29         texture_num[0] = texture_num[1] = texture_num[2] = 0;
30
31         assert(width % ycbcr_format.chroma_subsampling_x == 0);
32         pitch[0] = widths[0] = width;
33         pitch[1] = widths[1] = width / ycbcr_format.chroma_subsampling_x;
34         pitch[2] = widths[2] = width / ycbcr_format.chroma_subsampling_x;
35
36         assert(height % ycbcr_format.chroma_subsampling_y == 0);
37         heights[0] = height;
38         heights[1] = height / ycbcr_format.chroma_subsampling_y;
39         heights[2] = height / ycbcr_format.chroma_subsampling_y;
40
41         pixel_data[0] = pixel_data[1] = pixel_data[2] = NULL;
42 }
43
44 YCbCrInput::~YCbCrInput()
45 {
46         for (unsigned channel = 0; channel < 3; ++channel) {
47                 if (texture_num[channel] != 0) {
48                         resource_pool->release_2d_texture(texture_num[channel]);
49                 }
50         }
51 }
52
53 void YCbCrInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
54 {
55         for (unsigned channel = 0; channel < 3; ++channel) {
56                 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
57                 check_error();
58
59                 if (texture_num[channel] == 0) {
60                         // (Re-)upload the texture.
61                         texture_num[channel] = resource_pool->create_2d_texture(GL_R8, widths[channel], heights[channel]);
62                         glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
63                         check_error();
64                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
65                         check_error();
66                         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
67                         check_error();
68                         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
69                         check_error();
70                         glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
71                         check_error();
72                         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], heights[channel], GL_RED, GL_UNSIGNED_BYTE, pixel_data[channel]);
73                         check_error();
74                         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
75                         check_error();
76                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
77                         check_error();
78                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
79                         check_error();
80                 } else {
81                         glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
82                         check_error();
83                 }
84         }
85
86         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
87         check_error();
88
89         // Bind samplers.
90         set_uniform_int(glsl_program_num, prefix, "tex_y", *sampler_num + 0);
91         set_uniform_int(glsl_program_num, prefix, "tex_cb", *sampler_num + 1);
92         set_uniform_int(glsl_program_num, prefix, "tex_cr", *sampler_num + 2);
93
94         *sampler_num += 3;
95 }
96
97 string YCbCrInput::output_fragment_shader()
98 {
99         float offset[3];
100         Matrix3d ycbcr_to_rgb;
101         compute_ycbcr_matrix(ycbcr_format, offset, &ycbcr_to_rgb);
102
103         string frag_shader;
104
105         frag_shader = output_glsl_mat3("PREFIX(inv_ycbcr_matrix)", ycbcr_to_rgb);
106         frag_shader += output_glsl_vec3("PREFIX(offset)", offset[0], offset[1], offset[2]);
107
108         float cb_offset_x = compute_chroma_offset(
109                 ycbcr_format.cb_x_position, ycbcr_format.chroma_subsampling_x, widths[1]);
110         float cb_offset_y = compute_chroma_offset(
111                 ycbcr_format.cb_y_position, ycbcr_format.chroma_subsampling_y, heights[1]);
112         frag_shader += output_glsl_vec2("PREFIX(cb_offset)", cb_offset_x, cb_offset_y);
113
114         float cr_offset_x = compute_chroma_offset(
115                 ycbcr_format.cr_x_position, ycbcr_format.chroma_subsampling_x, widths[2]);
116         float cr_offset_y = compute_chroma_offset(
117                 ycbcr_format.cr_y_position, ycbcr_format.chroma_subsampling_y, heights[2]);
118         frag_shader += output_glsl_vec2("PREFIX(cr_offset)", cr_offset_x, cr_offset_y);
119
120         frag_shader += read_file("ycbcr_input.frag");
121         return frag_shader;
122 }
123
124 void YCbCrInput::invalidate_pixel_data()
125 {
126         for (unsigned channel = 0; channel < 3; ++channel) {
127                 if (texture_num[channel] != 0) {
128                         resource_pool->release_2d_texture(texture_num[channel]);
129                         texture_num[channel] = 0;
130                 }
131         }
132 }
133
134 bool YCbCrInput::set_int(const std::string& key, int value)
135 {
136         if (key == "needs_mipmaps") {
137                 // We currently do not support this.
138                 return (value == 0);
139         }
140         return Effect::set_int(key, value);
141 }
142
143 }  // namespace movit