1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* SPDX-License-Identifier: LGPL-2.1-or-later OR MPL-2.0

Copyright (c) 2024 DorotaC
*/

/*! YUYV → RGBA shader for UVC webcams.
*/

use cgmath::Matrix3;
use crate::egl::{ContextRef, CurrentContext, Texture};
use crate::raw;
use glium;
use glium::{program, uniform};
use std::error::Error;
use super::Shader;

/**
 * FIXME: I don't know enough about UVC, but the
 * USB Device Class Definition for Video Devices - FAQ
 * in version 1.5, 2.19 Multiple Color Matching Descriptors and table 2.6
 * states that there are 3 parameters to tweak: Color primary, transfer function, and luma matrix.
 * 
 * Not exactly sure how they work. This here is best effort. Someone with more experience can fix this.
 * 
 * So far, I've gathered:
 * 
 * - the luma transfer function gives the matrix calculating yuv = M·rgb
 * - the primaries matrix calculates rgb = P·xyz
 * - the transfer function calculates gamma
 * 
 * so the output should be srgb = gamma(M⁻¹·yuv).
 * 
 * TODO in libvidi: read the descriptor values.
 */
pub enum ColorSpace {
    /// ITU BT.470
    BT470 = 2,
    /// ITU BT.709, for HDTV
    BT709 = 1,
    /// Doesn't convert the color values
    Identity = 0,
}

impl From<ColorSpace> for Matrix3<f32> {
    fn from(value: ColorSpace) -> Self {
        match value {
            ColorSpace::BT470 => *<&Matrix3::<_>>::from(&[
                1., 0., 1.13983,
                1., -0.39465, -0.58060,
                1., 2.03211, 0.,
            ]),
            // from https://en.wikipedia.org/wiki/YUY2?useskin=vector#HDTV_with_BT.709
            ColorSpace::BT709 => *<&Matrix3::<_>>::from(&[
                1., 0., 1.28033,
                1., -0.21482, -0.38059,
                1., 2.12798, 0.,
            ]),
            ColorSpace::Identity => *<&Matrix3::<_>>::from(&[
                1., 0., 0.,
                0., 1., 0.,
                0., 0., 1.,
            ]),
        }
    }
}

pub enum Gamma {
    Srgb = 1,
    Identity = 0,
}

/// Converts a YUV-encoded texture into a RGB one.
pub struct YuyvToRgba;

impl YuyvToRgba {
    /// Creates a shader
    pub fn new(facade: &impl glium::backend::Facade, dims: (u32, u32))
        -> Result<Shader<Self>, Box<dyn Error>>
    {
        Shader::<Self>::new_with_facade(
            facade,
            dims,
        )
    }
}

impl Shader<YuyvToRgba> {
    /// The only thing that differs between the native R16 and RG88 versions is the fragment shader.
    fn new_with_facade<F: glium::backend::Facade>(
        facade: &F,
        // Output dimensions
        size: (u32, u32),
    ) -> Result<Self, Box<dyn Error>> {
        let (vertices, indices) = super::covering_vertices(facade, size)?;
        /*use glium::CapabilitiesSource;
        dbg!(&facade.get_capabilities().supported_glsl_versions);*/
        Ok(Self {
            program: program!(
                facade,
                // The gbm backend supports this
                120 => {
                    vertex: include_str!("yuv/vert.glsl"),
                    fragment: include_str!("yuv/frag.glsl"),
                },
                // The winit backend supports this
                140 => {
                    vertex: include_str!("yuv/vert.glsl"),
                    fragment: include_str!("yuv/frag.glsl"),
                },
            )?,
            vertices,
            indices,
            _data: Default::default(),
        })
    }
    
    /// Converts image in GL texture in YUYV format `source_tex`
    /// by writing a RGBA image to GL surface `target`.
    ///
    /// TODO: the input texture should be an OpenGL texture, so that shader calls can be chained.
    /// The output is an OpenGL surface to allow rendering directly to the screen.
    pub fn convert<F: glium::backend::Facade>(
        &self,
        facade: &F,
        source_tex: Texture,
        target: &mut impl glium::Surface,
        space: ColorSpace,
        gamma: Gamma,
    ) -> Result<(), Box<dyn Error>>{
        let (width, height) = target.get_dimensions();
        if source_tex.dimensions().height != height {
            Err(super::Error::BadDimensions("Source and target heights must be equal"))?;
        }
        if source_tex.dimensions().width != width * 2 {
            Err(super::Error::BadDimensions("Source width must be twice the target"))?;
        }
        // TODO: allow any monochrome format
        if source_tex.format() != gbm::Format::R8 {
            Err(super::Error::BadFormat("Only R8 supported"))?;
        }
        self.draw_any(
            facade,
            source_tex,
            target,
            uniform! {
                gamma: gamma as i8,
                color_space: space as i8,
            },
        )
    }
}