1 #ifndef _MOVIT_YCBCR_INPUT_H
2 #define _MOVIT_YCBCR_INPUT_H 1
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.
14 #include "effect_chain.h"
15 #include "image_format.h"
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.
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,
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;
39 YCBCR_INPUT_INTERLEAVED,
42 class YCbCrInput : public Input {
44 YCbCrInput(const ImageFormat &image_format,
45 const YCbCrFormat &ycbcr_format,
46 unsigned width, unsigned height,
47 YCbCrInputSplitting ycbcr_input_splitting = YCBCR_INPUT_PLANAR);
50 virtual std::string effect_type_id() const { return "YCbCrInput"; }
52 virtual bool can_output_linear_gamma() const { return false; }
53 virtual AlphaHandling alpha_handling() const { return OUTPUT_BLANK_ALPHA; }
55 std::string output_fragment_shader();
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);
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; }
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.
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)
78 assert(channel >= 0 && channel < num_channels);
79 this->pixel_data[channel] = pixel_data;
80 this->pbos[channel] = pbo;
81 invalidate_pixel_data();
84 void invalidate_pixel_data();
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)
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();
100 void set_height(unsigned height)
103 this->height = height;
105 assert(height % ycbcr_format.chroma_subsampling_y == 0);
107 heights[1] = height / ycbcr_format.chroma_subsampling_y;
108 heights[2] = height / ycbcr_format.chroma_subsampling_y;
109 invalidate_pixel_data();
112 void set_pitch(unsigned channel, unsigned pitch)
115 assert(channel >= 0 && channel < num_channels);
116 this->pitch[channel] = pitch;
117 invalidate_pixel_data();
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)
125 possibly_release_texture(channel);
126 this->texture_num[channel] = texture_num;
127 this->owns_texture[channel] = false;
130 virtual void inform_added(EffectChain *chain)
132 resource_pool = chain->get_resource_pool();
135 bool set_int(const std::string& key, int value);
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);
141 ImageFormat image_format;
142 YCbCrFormat ycbcr_format;
144 YCbCrInputSplitting ycbcr_input_splitting;
145 GLuint pbos[3], texture_num[3];
146 GLint uniform_tex_y, uniform_tex_cb, uniform_tex_cr;
148 unsigned width, height, widths[3], heights[3];
149 const unsigned char *pixel_data[3];
151 bool owns_texture[3];
152 ResourcePool *resource_pool;
157 #endif // !defined(_MOVIT_YCBCR_INPUT_H)