]> git.sesse.net Git - movit/blob - ycbcr_422interleaved_input.h
Fix broken YCbCr subpixel positioning. Caught by the unit tests.
[movit] / ycbcr_422interleaved_input.h
1 #ifndef _MOVIT_YCBCR_422INTERLEAVED_INPUT_H
2 #define _MOVIT_YCBCR_422INTERLEAVED_INPUT_H 1
3
4 // YCbCr422InterleavedInput is for handling 4:2:2 interleaved 8-bit Y'CbCr,
5 // which you can get from e.g. certain capture cards. (Most other Y'CbCr
6 // encodings are planar, which is handled by YCbCrInput.) Currently we only
7 // handle the UYVY variant, although YUY2 should be easy to support if needed.
8 //
9 // Horizontal chroma placement is freely choosable as with YCbCrInput,
10 // but BT.601 (which at least DeckLink claims to conform to, under the
11 // name CCIR 601) seems to specify chroma positioning to the far left
12 // (that is 0.0); BT.601 Annex 1 (page 7) says “C R and C B samples co-sited
13 // with odd (1st, 3rd, 5th, etc.) Y samples in each line”, and I assume they do
14 // not start counting from 0 when they use the “1st” moniker.
15 //
16 // Interpolation is bilinear as in YCbCrInput (done by the GPU's normal
17 // scaling, except for the Y channel which of course needs some fiddling),
18 // and is done in non-linear light (since that's what everything specifies,
19 // except Rec. 2020 lets you choose between the two). A higher-quality
20 // choice would be to use a single pass of ResampleEffect to scale the
21 // chroma, but for now we are consistent between the two.
22 //
23 // There is a disparity between the interleaving and the way OpenGL typically
24 // expects to sample. In lieu of accessible hardware support (a lot of hardware
25 // supports native interleaved 4:2:2 sampling, but OpenGL drivers seem to
26 // rarely support it), we simply upload the same data twice; once as a
27 // full-width RG texture (from which we sample luma) and once as a half-width
28 // RGBA texture (from which we sample chroma). We throw away half of the color
29 // channels each time, so bandwidth is wasted, but it makes for a very
30 // uncomplicated shader.
31
32 #include <epoxy/gl.h>
33 #include <string>
34
35 #include "effect.h"
36 #include "effect_chain.h"
37 #include "image_format.h"
38 #include "input.h"
39 #include "ycbcr.h"
40
41 namespace movit {
42
43 class ResourcePool;
44
45 class YCbCr422InterleavedInput : public Input {
46 public:
47         // <ycbcr_format> must be consistent with 4:2:2 sampling; specifically:
48         //
49         //  * chroma_subsampling_x must be 2.
50         //  * chroma_subsampling_y must be 1.
51         //
52         // <width> must obviously be an even number. It is the true width of the image
53         // in pixels, ie., the number of horizontal luma samples.
54         YCbCr422InterleavedInput(const ImageFormat &image_format,
55                                  const YCbCrFormat &ycbcr_format,
56                                  unsigned width, unsigned height);
57         ~YCbCr422InterleavedInput();
58
59         virtual std::string effect_type_id() const { return "YCbCr422InterleavedInput"; }
60
61         virtual bool can_output_linear_gamma() const { return false; }
62         virtual AlphaHandling alpha_handling() const { return OUTPUT_BLANK_ALPHA; }
63
64         std::string output_fragment_shader();
65
66         // Uploads the texture if it has changed since last time.
67         void set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num);
68
69         unsigned get_width() const { return width; }
70         unsigned get_height() const { return height; }
71         Colorspace get_color_space() const { return image_format.color_space; }
72         GammaCurve get_gamma_curve() const { return image_format.gamma_curve; }
73         virtual bool can_supply_mipmaps() const { return false; }
74
75         // Tells the input where to fetch the actual pixel data. Note that if you change
76         // this data, you must either call set_pixel_data() again (using the same pointer
77         // is fine), or invalidate_pixel_data(). Otherwise, the texture won't be re-uploaded
78         // on subsequent frames.
79         //
80         // The data can either be a regular pointer (if pbo==0), or a byte offset
81         // into a PBO. The latter will allow you to start uploading the texture data
82         // asynchronously to the GPU, if you have any CPU-intensive work between the
83         // call to set_pixel_data() and the actual rendering. Also, since we upload
84         // the data twice, using a PBO can save texture upload bandwidth. In either case,
85         // the pointer (and PBO, if set) has to be valid at the time of the render call.
86         void set_pixel_data(const unsigned char *pixel_data, GLuint pbo = 0)
87         {
88                 this->pixel_data = pixel_data;
89                 this->pbo = pbo;
90                 invalidate_pixel_data();
91         }
92
93         void invalidate_pixel_data();
94
95         void set_pitch(unsigned pitch) {
96                 assert(pitch % ycbcr_format.chroma_subsampling_x == 0);
97                 pitches[CHANNEL_LUMA] = pitch;
98                 pitches[CHANNEL_CHROMA] = pitch / ycbcr_format.chroma_subsampling_x;
99                 invalidate_pixel_data();
100         }
101
102         virtual void inform_added(EffectChain *chain)
103         {
104                 resource_pool = chain->get_resource_pool();
105         }
106
107         bool set_int(const std::string& key, int value);
108
109 private:
110         ImageFormat image_format;
111         YCbCrFormat ycbcr_format;
112         GLuint pbo;
113
114         // Luma texture is 0, chroma texture is 1.
115         enum Channel {
116                 CHANNEL_LUMA,
117                 CHANNEL_CHROMA
118         };
119         GLuint texture_num[2];
120         GLuint widths[2];
121         unsigned pitches[2];
122
123         unsigned width, height;
124         const unsigned char *pixel_data;
125         ResourcePool *resource_pool;
126 };
127
128 }  // namespace movit
129
130 #endif  // !defined(_MOVIT_YCBCR_422INTERLEAVED_INPUT_H)