]> git.sesse.net Git - movit/blob - ycbcr_input.h
6f4b66d437b1e5cc140e0fa10264bbc24115f6f2
[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         void set_pitch(unsigned channel, unsigned pitch) {
83                 assert(channel >= 0 && channel < num_channels);
84                 this->pitch[channel] = pitch;
85                 invalidate_pixel_data();
86         }
87
88         // Tells the input to use the specific OpenGL texture as pixel data for the given
89         // channel. The comments on FlatInput::set_texture_num() also apply here, except
90         // that this input generally does not use mipmaps.
91         void set_texture_num(unsigned channel, GLuint texture_num)
92         {
93                 possibly_release_texture(channel);
94                 this->texture_num[channel] = texture_num;
95                 this->owns_texture[channel] = false;
96         }
97
98         virtual void inform_added(EffectChain *chain)
99         {
100                 resource_pool = chain->get_resource_pool();
101         }
102
103         bool set_int(const std::string& key, int value);
104
105 private:
106         // Release the texture in the given channel if we have any, and it is owned by us.
107         void possibly_release_texture(unsigned channel);
108
109         ImageFormat image_format;
110         YCbCrFormat ycbcr_format;
111         GLuint num_channels;
112         YCbCrInputSplitting ycbcr_input_splitting;
113         GLuint pbos[3], texture_num[3];
114         GLint uniform_tex_y, uniform_tex_cb, uniform_tex_cr;
115
116         unsigned width, height, widths[3], heights[3];
117         const unsigned char *pixel_data[3];
118         unsigned pitch[3];
119         bool owns_texture[3];
120         ResourcePool *resource_pool;
121 };
122
123 }  // namespace movit
124
125 #endif // !defined(_MOVIT_YCBCR_INPUT_H)