]> git.sesse.net Git - movit/blob - ycbcr_input.h
Allow inputs to say they cannot support mipmaps.
[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
17 namespace movit {
18
19 class ResourcePool;
20
21 struct YCbCrFormat {
22         // Which formula for Y' to use.
23         YCbCrLumaCoefficients luma_coefficients;
24
25         // If true, assume Y'CbCr coefficients are full-range, ie. go from 0 to 255
26         // instead of the limited 220/225 steps in classic MPEG. For instance,
27         // JPEG uses the Rec. 601 luma coefficients, but full range.
28         bool full_range;
29
30         // Sampling factors for chroma components. For no subsampling (4:4:4),
31         // set both to 1.
32         unsigned chroma_subsampling_x, chroma_subsampling_y;
33
34         // Positioning of the chroma samples. MPEG-1 and JPEG is (0.5, 0.5);
35         // MPEG-2 and newer typically are (0.0, 0.5).
36         float cb_x_position, cb_y_position;
37         float cr_x_position, cr_y_position;
38 };
39
40 class YCbCrInput : public Input {
41 public:
42         YCbCrInput(const ImageFormat &image_format,
43                    const YCbCrFormat &ycbcr_format,
44                    unsigned width, unsigned height);
45         ~YCbCrInput();
46
47         virtual std::string effect_type_id() const { return "YCbCrInput"; }
48
49         virtual bool can_output_linear_gamma() const { return false; }
50         virtual AlphaHandling alpha_handling() const { return OUTPUT_BLANK_ALPHA; }
51
52         std::string output_fragment_shader();
53
54         // Uploads the texture if it has changed since last time.
55         void set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num);
56
57         unsigned get_width() const { return width; }
58         unsigned get_height() const { return height; }
59         Colorspace get_color_space() const { return image_format.color_space; }
60         GammaCurve get_gamma_curve() const { return image_format.gamma_curve; }
61         virtual bool can_supply_mipmaps() const { return false; }
62
63         // Tells the input where to fetch the actual pixel data. Note that if you change
64         // this data, you must either call set_pixel_data() again (using the same pointer
65         // is fine), or invalidate_pixel_data(). Otherwise, the texture won't be re-uploaded
66         // on subsequent frames.
67         //
68         // The data can either be a regular pointer (if pbo==0), or a byte offset
69         // into a PBO. The latter will allow you to start uploading the texture data
70         // asynchronously to the GPU, if you have any CPU-intensive work between the
71         // call to set_pixel_data() and the actual rendering. In either case,
72         // the pointer (and PBO, if set) has to be valid at the time of the render call.
73         void set_pixel_data(unsigned channel, const unsigned char *pixel_data, GLuint pbo = 0)
74         {
75                 assert(channel >= 0 && channel < 3);
76                 this->pixel_data[channel] = pixel_data;
77                 this->pbos[channel] = pbo;
78                 invalidate_pixel_data();
79         }
80
81         void invalidate_pixel_data();
82
83         void set_pitch(unsigned channel, unsigned pitch) {
84                 assert(channel >= 0 && channel < 3);
85                 this->pitch[channel] = pitch;
86                 invalidate_pixel_data();
87         }
88
89         virtual void inform_added(EffectChain *chain)
90         {
91                 resource_pool = chain->get_resource_pool();
92         }
93
94 private:
95         ImageFormat image_format;
96         YCbCrFormat ycbcr_format;
97         GLuint pbos[3], texture_num[3];
98
99         unsigned width, height, widths[3], heights[3];
100         const unsigned char *pixel_data[3];
101         unsigned pitch[3];
102         ResourcePool *resource_pool;
103 };
104
105 }  // namespace movit
106
107 #endif // !defined(_MOVIT_YCBCR_INPUT_H)