]> git.sesse.net Git - movit/blob - ycbcr_422interleaved_input.cpp
Fix an issue where temporary textures could be reused too early by a different thread.
[movit] / ycbcr_422interleaved_input.cpp
1 #include <epoxy/gl.h>
2 #include <assert.h>
3 #include <stdio.h>
4 #include <string.h>
5
6 #include "effect_util.h"
7 #include "resource_pool.h"
8 #include "util.h"
9 #include "ycbcr.h"
10 #include "ycbcr_422interleaved_input.h"
11
12 using namespace Eigen;
13 using namespace std;
14
15 namespace movit {
16
17 YCbCr422InterleavedInput::YCbCr422InterleavedInput(const ImageFormat &image_format,
18                                                    const YCbCrFormat &ycbcr_format,
19                                                    unsigned width, unsigned height)
20         : image_format(image_format),
21           ycbcr_format(ycbcr_format),
22           width(width),
23           height(height),
24           resource_pool(nullptr)
25 {
26         pbo = 0;
27         texture_num[0] = texture_num[1] = 0;
28
29         assert(ycbcr_format.chroma_subsampling_x == 2);
30         assert(ycbcr_format.chroma_subsampling_y == 1);
31         assert(width % ycbcr_format.chroma_subsampling_x == 0);
32
33         widths[CHANNEL_LUMA] = width;
34         widths[CHANNEL_CHROMA] = width / ycbcr_format.chroma_subsampling_x;
35         pitches[CHANNEL_LUMA] = width;
36         pitches[CHANNEL_CHROMA] = width / ycbcr_format.chroma_subsampling_x;
37
38         pixel_data = nullptr;
39
40         register_uniform_sampler2d("tex_y", &uniform_tex_y);
41         register_uniform_sampler2d("tex_cbcr", &uniform_tex_cbcr);
42 }
43
44 YCbCr422InterleavedInput::~YCbCr422InterleavedInput()
45 {
46         for (unsigned channel = 0; channel < 2; ++channel) {
47                 if (texture_num[channel] != 0) {
48                         resource_pool->release_2d_texture(texture_num[channel]);
49                 }
50         }
51 }
52
53 void YCbCr422InterleavedInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
54 {
55         for (unsigned channel = 0; channel < 2; ++channel) {
56                 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
57                 check_error();
58
59                 if (texture_num[channel] == 0) {
60                         // (Re-)upload the texture.
61                         GLuint format, internal_format;
62                         if (channel == CHANNEL_LUMA) {
63                                 format = GL_RG;
64                                 internal_format = GL_RG8;
65                         } else {        
66                                 assert(channel == CHANNEL_CHROMA);
67                                 format = GL_RGBA;
68                                 internal_format = GL_RGBA8;
69                         }
70
71                         texture_num[channel] = resource_pool->create_2d_texture(internal_format, widths[channel], height);
72                         glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
73                         check_error();
74                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
75                         check_error();
76                         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
77                         check_error();
78                         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
79                         check_error();
80                         glPixelStorei(GL_UNPACK_ROW_LENGTH, pitches[channel]);
81                         check_error();
82                         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], height, format, GL_UNSIGNED_BYTE, pixel_data);
83                         check_error();
84                         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
85                         check_error();
86                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
87                         check_error();
88                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
89                         check_error();
90                 } else {
91                         glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
92                         check_error();
93                 }
94         }
95
96         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
97         check_error();
98
99         // Bind samplers.
100         uniform_tex_y = *sampler_num + 0;
101         uniform_tex_cbcr = *sampler_num + 1;
102
103         *sampler_num += 2;
104 }
105
106 string YCbCr422InterleavedInput::output_fragment_shader()
107 {
108         float offset[3];
109         Matrix3d ycbcr_to_rgb;
110         compute_ycbcr_matrix(ycbcr_format, offset, &ycbcr_to_rgb);
111
112         string frag_shader;
113
114         frag_shader = output_glsl_mat3("PREFIX(inv_ycbcr_matrix)", ycbcr_to_rgb);
115         frag_shader += output_glsl_vec3("PREFIX(offset)", offset[0], offset[1], offset[2]);
116
117         float cb_offset_x = compute_chroma_offset(
118                 ycbcr_format.cb_x_position, ycbcr_format.chroma_subsampling_x, widths[CHANNEL_CHROMA]);
119         float cr_offset_x = compute_chroma_offset(
120                 ycbcr_format.cr_x_position, ycbcr_format.chroma_subsampling_x, widths[CHANNEL_CHROMA]);
121         frag_shader += output_glsl_float("PREFIX(cb_offset_x)", cb_offset_x);
122         frag_shader += output_glsl_float("PREFIX(cr_offset_x)", cr_offset_x);
123
124         char buf[256];
125         sprintf(buf, "#define CB_CR_OFFSETS_EQUAL %d\n",
126                 (fabs(ycbcr_format.cb_x_position - ycbcr_format.cr_x_position) < 1e-6));
127         frag_shader += buf;
128
129         frag_shader += read_file("ycbcr_422interleaved_input.frag");
130         return frag_shader;
131 }
132
133 void YCbCr422InterleavedInput::invalidate_pixel_data()
134 {
135         for (unsigned channel = 0; channel < 2; ++channel) {
136                 if (texture_num[channel] != 0) {
137                         resource_pool->release_2d_texture(texture_num[channel]);
138                         texture_num[channel] = 0;
139                 }
140         }
141 }
142
143 bool YCbCr422InterleavedInput::set_int(const std::string& key, int value)
144 {
145         if (key == "needs_mipmaps") {
146                 // We currently do not support this.
147                 return (value == 0);
148         }
149         return Effect::set_int(key, value);
150 }
151
152 }  // namespace movit