]> git.sesse.net Git - movit/blob - ycbcr_input.h
Add input support for packed 10-bit Y'CbCr.
[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 //   * 10-bit interleaved (chunked) Y'CbCr packed into 32-bit words
13 //     (10:10:10:2), no subsampling (4:4:4 only).
14 //
15 // For the former case, it upsamples planes as needed, using the default linear
16 // upsampling OpenGL gives you. Note that YCbCr422InterleavedInput supports the
17 // important special case of 8-bit 4:2:2 interleaved.
18
19 #include <epoxy/gl.h>
20 #include <assert.h>
21 #include <string>
22
23 #include "effect.h"
24 #include "effect_chain.h"
25 #include "image_format.h"
26 #include "input.h"
27 #include "ycbcr.h"
28
29 namespace movit {
30
31 class ResourcePool;
32
33 // Whether the data is planar (Y', Cb and Cr in one texture each) or not.
34 enum YCbCrInputSplitting {
35         // The standard, default case; Y', Cb and Cr in one texture each.
36         YCBCR_INPUT_PLANAR,
37
38         // Y' in one texture, and then Cb and Cr interleaved in one texture.
39         // In particular, this is a superset of the relatively popular NV12 mode.
40         // If you specify this mode, the “Cr” pointer texture will be unused
41         // (the ”Cb” texture contains both).
42         YCBCR_INPUT_SPLIT_Y_AND_CBCR,
43
44         // Y', Cb and Cr interleaved in the same texture (the “Y” texture;
45         // “Cb” and “Cr” are unused). This means you cannot have any subsampling;
46         // 4:4:4 only.
47         YCBCR_INPUT_INTERLEAVED,
48 };
49
50 class YCbCrInput : public Input {
51 public:
52         // Type can be GL_UNSIGNED_BYTE for 8-bit, or GL_UNSIGNED_INT_2_10_10_10_REV
53         // for 10-bit (YCBCR_INPUT_INTERLEAVED only).
54         YCbCrInput(const ImageFormat &image_format,
55                    const YCbCrFormat &ycbcr_format,
56                    unsigned width, unsigned height,
57                    YCbCrInputSplitting ycbcr_input_splitting = YCBCR_INPUT_PLANAR,
58                    GLenum type = GL_UNSIGNED_BYTE);
59         ~YCbCrInput();
60
61         virtual std::string effect_type_id() const { return "YCbCrInput"; }
62
63         virtual bool can_output_linear_gamma() const { return false; }
64         virtual AlphaHandling alpha_handling() const { return OUTPUT_BLANK_ALPHA; }
65
66         std::string output_fragment_shader();
67
68         // Uploads the texture if it has changed since last time.
69         void set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num);
70
71         unsigned get_width() const { return width; }
72         unsigned get_height() const { return height; }
73         Colorspace get_color_space() const { return image_format.color_space; }
74         GammaCurve get_gamma_curve() const { return image_format.gamma_curve; }
75         virtual bool can_supply_mipmaps() const { return false; }
76
77         // Tells the input where to fetch the actual pixel data. Note that if you change
78         // this data, you must either call set_pixel_data() again (using the same pointer
79         // is fine), or invalidate_pixel_data(). Otherwise, the texture won't be re-uploaded
80         // on subsequent frames.
81         //
82         // The data can either be a regular pointer (if pbo==0), or a byte offset
83         // into a PBO. The latter will allow you to start uploading the texture data
84         // asynchronously to the GPU, if you have any CPU-intensive work between the
85         // call to set_pixel_data() and the actual rendering. In either case,
86         // the pointer (and PBO, if set) has to be valid at the time of the render call.
87         void set_pixel_data(unsigned channel, const unsigned char *pixel_data, GLuint pbo = 0)
88         {
89                 assert(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_2_10_10_10_REV);
90                 assert(channel >= 0 && channel < num_channels);
91                 this->pixel_data[channel] = pixel_data;
92                 this->pbos[channel] = pbo;
93                 invalidate_pixel_data();
94         }
95
96         void set_pixel_data(unsigned channel, const uint32_t *pixel_data, GLuint pbo = 0)
97         {
98                 assert(type == GL_UNSIGNED_INT_2_10_10_10_REV);
99                 assert(channel == 0);
100                 this->pixel_data[channel] = reinterpret_cast<const unsigned char *>(pixel_data);
101                 this->pbos[channel] = pbo;
102                 invalidate_pixel_data();
103         }
104
105         void invalidate_pixel_data();
106
107         // Note: Sets pitch to width, so even if your pitch is unchanged,
108         // you will need to re-set it after this call.
109         void set_width(unsigned width)
110         {
111                 assert(width != 0);
112                 this->width = width;
113
114                 assert(width % ycbcr_format.chroma_subsampling_x == 0);
115                 pitch[0] = widths[0] = width;
116                 pitch[1] = widths[1] = width / ycbcr_format.chroma_subsampling_x;
117                 pitch[2] = widths[2] = width / ycbcr_format.chroma_subsampling_x;
118                 invalidate_pixel_data();
119         }
120
121         void set_height(unsigned height)
122         {
123                 assert(height != 0);
124                 this->height = height;
125
126                 assert(height % ycbcr_format.chroma_subsampling_y == 0);
127                 heights[0] = height;
128                 heights[1] = height / ycbcr_format.chroma_subsampling_y;
129                 heights[2] = height / ycbcr_format.chroma_subsampling_y;
130                 invalidate_pixel_data();
131         }
132
133         void set_pitch(unsigned channel, unsigned pitch)
134         {
135                 assert(pitch != 0);
136                 assert(channel >= 0 && channel < num_channels);
137                 this->pitch[channel] = pitch;
138                 invalidate_pixel_data();
139         }
140
141         // Tells the input to use the specific OpenGL texture as pixel data for the given
142         // channel. The comments on FlatInput::set_texture_num() also apply here, except
143         // that this input generally does not use mipmaps.
144         void set_texture_num(unsigned channel, GLuint texture_num)
145         {
146                 possibly_release_texture(channel);
147                 this->texture_num[channel] = texture_num;
148                 this->owns_texture[channel] = false;
149         }
150
151         virtual void inform_added(EffectChain *chain)
152         {
153                 resource_pool = chain->get_resource_pool();
154         }
155
156         bool set_int(const std::string& key, int value);
157
158 private:
159         // Release the texture in the given channel if we have any, and it is owned by us.
160         void possibly_release_texture(unsigned channel);
161
162         ImageFormat image_format;
163         YCbCrFormat ycbcr_format;
164         GLuint num_channels;
165         YCbCrInputSplitting ycbcr_input_splitting;
166         GLenum type;
167         GLuint pbos[3], texture_num[3];
168         GLint uniform_tex_y, uniform_tex_cb, uniform_tex_cr;
169
170         unsigned width, height, widths[3], heights[3];
171         const unsigned char *pixel_data[3];
172         unsigned pitch[3];
173         bool owns_texture[3];
174         ResourcePool *resource_pool;
175 };
176
177 }  // namespace movit
178
179 #endif // !defined(_MOVIT_YCBCR_INPUT_H)