]> git.sesse.net Git - movit/blob - ycbcr_input.h
Send shader compile log to stderr instead of stdout.
[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 <GL/glew.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 class ResourcePool;
18
19 struct YCbCrFormat {
20         // Which formula for Y' to use.
21         YCbCrLumaCoefficients luma_coefficients;
22
23         // If true, assume Y'CbCr coefficients are full-range, ie. go from 0 to 255
24         // instead of the limited 220/225 steps in classic MPEG. For instance,
25         // JPEG uses the Rec. 601 luma coefficients, but full range.
26         bool full_range;
27
28         // Sampling factors for chroma components. For no subsampling (4:4:4),
29         // set both to 1.
30         unsigned chroma_subsampling_x, chroma_subsampling_y;
31
32         // Positioning of the chroma samples. MPEG-1 and JPEG is (0.5, 0.5);
33         // MPEG-2 and newer typically are (0.0, 0.5).
34         float cb_x_position, cb_y_position;
35         float cr_x_position, cr_y_position;
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         ~YCbCrInput();
44
45         virtual std::string effect_type_id() const { return "YCbCrInput"; }
46
47         // Create the texture itself. We cannot do this in the constructor,
48         // because we don't necessarily know all the settings (sRGB texture,
49         // mipmap generation) at that point.
50         void finalize();
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
65         // Tells the input where to fetch the actual pixel data. Note that if you change
66         // this data, you must either call set_pixel_data() again (using the same pointer
67         // is fine), or invalidate_pixel_data(). Otherwise, the texture won't be re-uploaded
68         // on subsequent frames.
69         //
70         // The data can either be a regular pointer (if pbo==0), or a byte offset
71         // into a PBO. The latter will allow you to start uploading the texture data
72         // asynchronously to the GPU, if you have any CPU-intensive work between the
73         // call to set_pixel_data() and the actual rendering. In either case,
74         // the pointer (and PBO, if set) has to be valid at the time of the render call.
75         void set_pixel_data(unsigned channel, const unsigned char *pixel_data, GLuint pbo = 0)
76         {
77                 assert(channel >= 0 && channel < 3);
78                 this->pixel_data[channel] = pixel_data;
79                 this->pbos[channel] = pbo;
80                 invalidate_pixel_data();
81         }
82
83         void invalidate_pixel_data();
84
85         void set_pitch(unsigned channel, unsigned pitch) {
86                 assert(channel >= 0 && channel < 3);
87                 this->pitch[channel] = pitch;
88         }
89
90         virtual void inform_added(EffectChain *chain)
91         {
92                 resource_pool = chain->get_resource_pool();
93         }
94
95 private:
96         ImageFormat image_format;
97         YCbCrFormat ycbcr_format;
98         GLuint pbos[3], texture_num[3];
99         bool finalized;
100
101         int needs_mipmaps;
102
103         unsigned width, height, widths[3], heights[3];
104         const unsigned char *pixel_data[3];
105         unsigned pitch[3];
106         ResourcePool *resource_pool;
107 };
108
109 #endif // !defined(_MOVIT_YCBCR_INPUT_H)