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