]> git.sesse.net Git - movit/blob - ycbcr_input.h
Add support for compute shaders.
[movit] / ycbcr_input.h
1 #ifndef _MOVIT_YCBCR_INPUT_H
2 #define _MOVIT_YCBCR_INPUT_H 1
3
4 // YCbCrInput is for handling Y'CbCr (also sometimes, usually rather
5 // imprecisely, called “YUV”), which is typically what you get from a video
6 // decoder. It supports these formats:
7 //
8 //   * 8-bit planar Y'CbCr, possibly subsampled (e.g. 4:2:0).
9 //   * 8-bit semiplanar Y'CbCr (Y' in one plane, CbCr in another),
10 //     possibly subsampled.
11 //   * 8-bit interleaved (chunked) Y'CbCr, no subsampling (4:4:4 only).
12 //   * All of the above in 10- and 12-bit versions, where each sample is
13 //     stored in a 16-bit int (so the 6 or 4 top bits are wasted).
14 //   * 10-bit interleaved (chunked) Y'CbCr packed into 32-bit words
15 //     (10:10:10:2), no subsampling (4:4:4 only).
16 //
17 // For the planar and semiplanar cases, it upsamples planes as needed, using
18 // the default linear upsampling OpenGL gives you. Note that YCbCr422InterleavedInput
19 // supports the important special case of 8-bit 4:2:2 interleaved.
20
21 #include <epoxy/gl.h>
22 #include <assert.h>
23 #include <string>
24
25 #include "effect.h"
26 #include "effect_chain.h"
27 #include "image_format.h"
28 #include "input.h"
29 #include "ycbcr.h"
30
31 namespace movit {
32
33 class ResourcePool;
34
35 // Whether the data is planar (Y', Cb and Cr in one texture each) or not.
36 enum YCbCrInputSplitting {
37         // The standard, default case; Y', Cb and Cr in one texture each.
38         YCBCR_INPUT_PLANAR,
39
40         // Y' in one texture, and then Cb and Cr interleaved in one texture.
41         // In particular, this is a superset of the relatively popular NV12 mode.
42         // If you specify this mode, the “Cr” pointer texture will be unused
43         // (the ”Cb” texture contains both).
44         YCBCR_INPUT_SPLIT_Y_AND_CBCR,
45
46         // Y', Cb and Cr interleaved in the same texture (the “Y” texture;
47         // “Cb” and “Cr” are unused). This means you cannot have any subsampling;
48         // 4:4:4 only.
49         YCBCR_INPUT_INTERLEAVED,
50 };
51
52 class YCbCrInput : public Input {
53 public:
54         // Type can be GL_UNSIGNED_BYTE for 8-bit, GL_UNSIGNED_SHORT for 10- or 12-bit
55         // (or 8-bit, although that's a bit useless), or GL_UNSIGNED_INT_2_10_10_10_REV
56         // for 10-bit (YCBCR_INPUT_INTERLEAVED only).
57         YCbCrInput(const ImageFormat &image_format,
58                    const YCbCrFormat &ycbcr_format,
59                    unsigned width, unsigned height,
60                    YCbCrInputSplitting ycbcr_input_splitting = YCBCR_INPUT_PLANAR,
61                    GLenum type = GL_UNSIGNED_BYTE);
62         ~YCbCrInput();
63
64         virtual std::string effect_type_id() const { return "YCbCrInput"; }
65
66         virtual bool can_output_linear_gamma() const { return false; }
67         virtual AlphaHandling alpha_handling() const { return OUTPUT_BLANK_ALPHA; }
68
69         std::string output_fragment_shader();
70
71         // Uploads the texture if it has changed since last time.
72         void set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num);
73
74         unsigned get_width() const { return width; }
75         unsigned get_height() const { return height; }
76         Colorspace get_color_space() const { return image_format.color_space; }
77         GammaCurve get_gamma_curve() const { return image_format.gamma_curve; }
78         virtual bool can_supply_mipmaps() const { return ycbcr_input_splitting == YCBCR_INPUT_INTERLEAVED; }
79         virtual bool is_single_texture() const { return ycbcr_input_splitting == YCBCR_INPUT_INTERLEAVED; }
80
81         // Tells the input where to fetch the actual pixel data. Note that if you change
82         // this data, you must either call set_pixel_data() again (using the same pointer
83         // is fine), or invalidate_pixel_data(). Otherwise, the texture won't be re-uploaded
84         // on subsequent frames.
85         //
86         // The data can either be a regular pointer (if pbo==0), or a byte offset
87         // into a PBO. The latter will allow you to start uploading the texture data
88         // asynchronously to the GPU, if you have any CPU-intensive work between the
89         // call to set_pixel_data() and the actual rendering. In either case,
90         // the pointer (and PBO, if set) has to be valid at the time of the render call.
91         void set_pixel_data(unsigned channel, const unsigned char *pixel_data, GLuint pbo = 0)
92         {
93                 assert(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_2_10_10_10_REV);
94                 assert(channel >= 0 && channel < num_channels);
95                 this->pixel_data[channel] = pixel_data;
96                 this->pbos[channel] = pbo;
97                 invalidate_pixel_data();
98         }
99
100         void set_pixel_data(unsigned channel, const uint16_t *pixel_data, GLuint pbo = 0)
101         {
102                 assert(type == GL_UNSIGNED_SHORT);
103                 assert(channel >= 0 && channel < num_channels);
104                 this->pixel_data[channel] = reinterpret_cast<const unsigned char *>(pixel_data);
105                 this->pbos[channel] = pbo;
106                 invalidate_pixel_data();
107         }
108
109         void set_pixel_data(unsigned channel, const uint32_t *pixel_data, GLuint pbo = 0)
110         {
111                 assert(type == GL_UNSIGNED_INT_2_10_10_10_REV);
112                 assert(channel == 0);
113                 this->pixel_data[channel] = reinterpret_cast<const unsigned char *>(pixel_data);
114                 this->pbos[channel] = pbo;
115                 invalidate_pixel_data();
116         }
117
118         void invalidate_pixel_data();
119
120         // Note: Sets pitch to width, so even if your pitch is unchanged,
121         // you will need to re-set it after this call.
122         void set_width(unsigned width)
123         {
124                 assert(width != 0);
125                 this->width = width;
126
127                 assert(width % ycbcr_format.chroma_subsampling_x == 0);
128                 pitch[0] = widths[0] = width;
129                 pitch[1] = widths[1] = width / ycbcr_format.chroma_subsampling_x;
130                 pitch[2] = widths[2] = width / ycbcr_format.chroma_subsampling_x;
131                 invalidate_pixel_data();
132         }
133
134         void set_height(unsigned height)
135         {
136                 assert(height != 0);
137                 this->height = height;
138
139                 assert(height % ycbcr_format.chroma_subsampling_y == 0);
140                 heights[0] = height;
141                 heights[1] = height / ycbcr_format.chroma_subsampling_y;
142                 heights[2] = height / ycbcr_format.chroma_subsampling_y;
143                 invalidate_pixel_data();
144         }
145
146         void set_pitch(unsigned channel, unsigned pitch)
147         {
148                 assert(pitch != 0);
149                 assert(channel >= 0 && channel < num_channels);
150                 this->pitch[channel] = pitch;
151                 invalidate_pixel_data();
152         }
153
154         // Tells the input to use the specific OpenGL texture as pixel data for the given
155         // channel. The comments on FlatInput::set_texture_num() also apply here, except
156         // that this input generally does not use mipmaps.
157         void set_texture_num(unsigned channel, GLuint texture_num)
158         {
159                 possibly_release_texture(channel);
160                 this->texture_num[channel] = texture_num;
161                 this->owns_texture[channel] = false;
162         }
163
164         // You can change the Y'CbCr format freely, also after finalize,
165         // although with one limitation: If Cb and Cr come from the same
166         // texture and their offsets offsets are the same (ie., within 1e-6)
167         // when finalizing, they most continue to be so forever, as this
168         // optimization is compiled into the shader.
169         //
170         // If you change subsampling parameters, you'll need to call
171         // set_width() / set_height() again after this.
172         void change_ycbcr_format(const YCbCrFormat &ycbcr_format);
173
174         virtual void inform_added(EffectChain *chain)
175         {
176                 resource_pool = chain->get_resource_pool();
177         }
178
179         bool set_int(const std::string& key, int value);
180
181 private:
182         // Release the texture in the given channel if we have any, and it is owned by us.
183         void possibly_release_texture(unsigned channel);
184
185         ImageFormat image_format;
186         YCbCrFormat ycbcr_format;
187         GLuint num_channels;
188         YCbCrInputSplitting ycbcr_input_splitting;
189         int needs_mipmaps;  // Only allowed if ycbcr_input_splitting == YCBCR_INPUT_INTERLEAVED.
190         GLenum type;
191         GLuint pbos[3], texture_num[3];
192         GLint uniform_tex_y, uniform_tex_cb, uniform_tex_cr;
193         Eigen::Matrix3d uniform_ycbcr_matrix;
194         float uniform_offset[3];
195         Point2D uniform_cb_offset, uniform_cr_offset;
196         bool cb_cr_offsets_equal;
197
198         unsigned width, height, widths[3], heights[3];
199         const unsigned char *pixel_data[3];
200         unsigned pitch[3];
201         bool owns_texture[3];
202         ResourcePool *resource_pool;
203 };
204
205 }  // namespace movit
206
207 #endif // !defined(_MOVIT_YCBCR_INPUT_H)