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