File: video_output_color.fs.glsl

package info (click to toggle)
bino 1.6.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,636 kB
  • sloc: cpp: 21,288; sh: 4,849; makefile: 314; sed: 16
file content (147 lines) | stat: -rw-r--r-- 4,375 bytes parent folder | download | duplicates (3)
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
/*
 * This file is part of bino, a 3D video player.
 *
 * Copyright (C) 2010, 2011, 2012
 * Martin Lambers <marlam@marlam.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#version 110

// quality: 0 .. 4
#define quality $quality

// layout_yuv_p
// layout_bgra32
#define $layout

// color_space_yuv601
// color_space_yuv709
// color_space_srgb
#define $color_space

// value_range_8bit_full
// value_range_8bit_mpeg
// value_range_10bit_full
// value_range_10bit_mpeg
#define $value_range

// the offset between the y texture coordinates and the appropriate
// u and v texture coordinates, according to the chroma sample location
#define chroma_offset_x $chroma_offset_x
#define chroma_offset_y $chroma_offset_y

// storage_srgb
// storage_linear_rgb
#define $storage

#if defined(layout_yuv_p)
uniform sampler2D y_tex;
uniform sampler2D u_tex;
uniform sampler2D v_tex;
#elif defined(layout_bgra32)
uniform sampler2D srgb_tex;
#endif

/* The YUV triplets used internally in this shader use the following
 * conventions:
 * - All three components are in the range [0,1]
 * - The U and V components refer to the normalized E_Cr and E_Cb
 *   components in the ITU.BT-* documents, shifted from [-0.5,+0.5]
 *   to [0,1]
 * - The color space is either the one defined in ITU.BT-601 or the one
 *   defined in ITU.BT-709. */
#if !defined(layout_bgra32)
vec3 yuv_to_srgb(vec3 yuv)
{
    // Convert the MPEG range to the full range for each component, if necessary
# if defined(value_range_8bit_mpeg)
    yuv = (yuv - vec3(16.0 / 255.0)) * vec3(256.0 / 220.0, 256.0 / 225.0, 256.0 / 225.0);
# elif defined(value_range_10bit_mpeg)
    yuv = (yuv - vec3(64.0 / 1023.0)) * vec3(1024.0 / 877.0, 1024.0 / 897.0, 1024.0 / 897.0);
# endif

# if defined(color_space_yuv709)
    // According to ITU.BT-709 (see entries 3.2 and 3.3 in Sec. 3 ("Signal format"))
    mat3 m = mat3(
            1.0,     1.0,      1.0,
            0.0,    -0.187324, 1.8556,
            1.5748, -0.468124, 0.0);
    return m * (yuv - vec3(0.0, 0.5, 0.5));
# else
    // According to ITU.BT-601 (see formulas in Sec. 2.5.1 and 2.5.2)
    mat3 m = mat3(
            1.0,    1.0,      1.0,
            0.0,   -0.344136, 1.772,
            1.402, -0.714136, 0.0);
    return m * (yuv - vec3(0.0, 0.5, 0.5));
# endif
}
#endif

#if defined(storage_linear_rgb)
# if quality >= 4
// See GL_ARB_framebuffer_sRGB extension
float nonlinear_to_linear(float x)
{
    return (x <= 0.04045 ? (x / 12.92) : pow((x + 0.055) / 1.055, 2.4));
}
vec3 srgb_to_rgb(vec3 srgb)
{
    float r = nonlinear_to_linear(srgb.r);
    float g = nonlinear_to_linear(srgb.g);
    float b = nonlinear_to_linear(srgb.b);
    return vec3(r, g, b);
}
# elif quality >= 2
vec3 srgb_to_rgb(vec3 srgb)
{
    return pow(srgb, vec3(2.2));
}
# else
vec3 srgb_to_rgb(vec3 srgb)
{
    return srgb * srgb;
}
# endif
#endif

vec3 get_srgb(vec2 tex_coord)
{
#if defined(layout_bgra32)
    return texture2D(srgb_tex, tex_coord).xyz;
#elif defined(value_range_8bit_full) || defined(value_range_8bit_mpeg)
    return yuv_to_srgb(vec3(
                texture2D(y_tex, tex_coord).x,
                texture2D(u_tex, tex_coord + vec2(chroma_offset_x, chroma_offset_y)).x,
                texture2D(v_tex, tex_coord + vec2(chroma_offset_x, chroma_offset_y)).x));
#else
    return yuv_to_srgb((65535.0 / 1023.0) * vec3(
                texture2D(y_tex, tex_coord).x,
                texture2D(u_tex, tex_coord + vec2(chroma_offset_x, chroma_offset_y)).x,
                texture2D(v_tex, tex_coord + vec2(chroma_offset_x, chroma_offset_y)).x));
#endif
}

void main()
{
    vec3 srgb = get_srgb(gl_TexCoord[0].xy);
#if defined(storage_srgb)
    gl_FragColor = vec4(srgb, 1.0);
#else
    vec3 rgb = srgb_to_rgb(srgb);
    gl_FragColor = vec4(rgb, 1.0);
#endif
}