]> git.sesse.net Git - movit/blob - ycbcr_input.h
Make the error printed on check_error() slightly friendlier: Include the enum if...
[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                 assert(width != 0);
87                 this->width = width;
88
89                 assert(width % ycbcr_format.chroma_subsampling_x == 0);
90                 pitch[0] = widths[0] = width;
91                 pitch[1] = widths[1] = width / ycbcr_format.chroma_subsampling_x;
92                 pitch[2] = widths[2] = width / ycbcr_format.chroma_subsampling_x;
93                 invalidate_pixel_data();
94         }
95
96         void set_height(unsigned height)
97         {
98                 assert(height != 0);
99                 this->height = height;
100
101                 assert(height % ycbcr_format.chroma_subsampling_y == 0);
102                 heights[0] = height;
103                 heights[1] = height / ycbcr_format.chroma_subsampling_y;
104                 heights[2] = height / ycbcr_format.chroma_subsampling_y;
105                 invalidate_pixel_data();
106         }
107
108         void set_pitch(unsigned channel, unsigned pitch)
109         {
110                 assert(pitch != 0);
111                 assert(channel >= 0 && channel < num_channels);
112                 this->pitch[channel] = pitch;
113                 invalidate_pixel_data();
114         }
115
116         // Tells the input to use the specific OpenGL texture as pixel data for the given
117         // channel. The comments on FlatInput::set_texture_num() also apply here, except
118         // that this input generally does not use mipmaps.
119         void set_texture_num(unsigned channel, GLuint texture_num)
120         {
121                 possibly_release_texture(channel);
122                 this->texture_num[channel] = texture_num;
123                 this->owns_texture[channel] = false;
124         }
125
126         virtual void inform_added(EffectChain *chain)
127         {
128                 resource_pool = chain->get_resource_pool();
129         }
130
131         bool set_int(const std::string& key, int value);
132
133 private:
134         // Release the texture in the given channel if we have any, and it is owned by us.
135         void possibly_release_texture(unsigned channel);
136
137         ImageFormat image_format;
138         YCbCrFormat ycbcr_format;
139         GLuint num_channels;
140         YCbCrInputSplitting ycbcr_input_splitting;
141         GLuint pbos[3], texture_num[3];
142         GLint uniform_tex_y, uniform_tex_cb, uniform_tex_cr;
143
144         unsigned width, height, widths[3], heights[3];
145         const unsigned char *pixel_data[3];
146         unsigned pitch[3];
147         bool owns_texture[3];
148         ResourcePool *resource_pool;
149 };
150
151 }  // namespace movit
152
153 #endif // !defined(_MOVIT_YCBCR_INPUT_H)