]> git.sesse.net Git - casparcg/blob - core/mixer/image/image_kernel.cpp
2.0.0.2: Merged mixer into core/mixer.
[casparcg] / core / mixer / image / image_kernel.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\r
6 *    CasparCG is free software: you can redistribute it and/or modify\r
7 *    it under the terms of the GNU General Public License as published by\r
8 *    the Free Software Foundation, either version 3 of the License, or\r
9 *    (at your option) any later version.\r
10 *\r
11 *    CasparCG is distributed in the hope that it will be useful,\r
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 *    GNU General Public License for more details.\r
15 \r
16 *    You should have received a copy of the GNU General Public License\r
17 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 */\r
20 #include "../../stdafx.h"\r
21 \r
22 #include "image_kernel.h"\r
23 \r
24 #include <common/exception/exceptions.h>\r
25 #include <common/gl/gl_check.h>\r
26 \r
27 #include <core/video_format.h>\r
28 #include <core/producer/frame/pixel_format.h>\r
29 #include <core/producer/frame/image_transform.h>\r
30 \r
31 #include <Glee.h>\r
32 \r
33 #include <boost/noncopyable.hpp>\r
34 \r
35 #include <unordered_map>\r
36 \r
37 namespace caspar { namespace mixer {\r
38 \r
39 class shader_program : boost::noncopyable\r
40 {\r
41         GLuint program_;\r
42 public:\r
43 \r
44         shader_program() : program_(0) {}\r
45         shader_program(shader_program&& other) : program_(other.program_){other.program_ = 0;}\r
46         shader_program(const std::string& vertex_source_str, const std::string& fragment_source_str) : program_(0)\r
47         {\r
48                 GLint success;\r
49         \r
50                 const char* vertex_source = vertex_source_str.c_str();\r
51                                                 \r
52                 auto vertex_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);\r
53                                         \r
54                 GL(glShaderSourceARB(vertex_shader, 1, &vertex_source, NULL));\r
55                 GL(glCompileShaderARB(vertex_shader));\r
56 \r
57                 GL(glGetObjectParameterivARB(vertex_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success));\r
58                 if (success == GL_FALSE)\r
59                 {\r
60                         char info[2048];\r
61                         GL(glGetInfoLogARB(vertex_shader, sizeof(info), 0, info));\r
62                         GL(glDeleteObjectARB(vertex_shader));\r
63                         std::stringstream str;\r
64                         str << "Failed to compile vertex shader:" << std::endl << info << std::endl;\r
65                         BOOST_THROW_EXCEPTION(gl::gl_error() << msg_info(str.str()));\r
66                 }\r
67                         \r
68                 const char* fragment_source = fragment_source_str.c_str();\r
69                                                 \r
70                 auto fragmemt_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);\r
71                                         \r
72                 GL(glShaderSourceARB(fragmemt_shader, 1, &fragment_source, NULL));\r
73                 GL(glCompileShaderARB(fragmemt_shader));\r
74 \r
75                 GL(glGetObjectParameterivARB(fragmemt_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success));\r
76                 if (success == GL_FALSE)\r
77                 {\r
78                         char info[2048];\r
79                         GL(glGetInfoLogARB(fragmemt_shader, sizeof(info), 0, info));\r
80                         GL(glDeleteObjectARB(fragmemt_shader));\r
81                         std::stringstream str;\r
82                         str << "Failed to compile fragment shader:" << std::endl << info << std::endl;\r
83                         BOOST_THROW_EXCEPTION(gl::gl_error() << msg_info(str.str()));\r
84                 }\r
85                         \r
86                 program_ = glCreateProgramObjectARB();\r
87                         \r
88                 GL(glAttachObjectARB(program_, vertex_shader));\r
89                 GL(glAttachObjectARB(program_, fragmemt_shader));\r
90 \r
91                 GL(glLinkProgramARB(program_));\r
92                         \r
93                 GL(glDeleteObjectARB(vertex_shader));\r
94                 GL(glDeleteObjectARB(fragmemt_shader));\r
95 \r
96                 GL(glGetObjectParameterivARB(program_, GL_OBJECT_LINK_STATUS_ARB, &success));\r
97                 if (success == GL_FALSE)\r
98                 {\r
99                         char info[2048];\r
100                         GL(glGetInfoLogARB(program_, sizeof(info), 0, info));\r
101                         GL(glDeleteObjectARB(program_));\r
102                         std::stringstream str;\r
103                         str << "Failed to link shader program:" << std::endl << info << std::endl;\r
104                         BOOST_THROW_EXCEPTION(gl::gl_error() << msg_info(str.str()));\r
105                 }\r
106                 GL(glUseProgramObjectARB(program_));\r
107                 glUniform1i(glGetUniformLocation(program_, "plane[0]"), 0);\r
108                 glUniform1i(glGetUniformLocation(program_, "plane[1]"), 1);\r
109                 glUniform1i(glGetUniformLocation(program_, "plane[2]"), 2);\r
110                 glUniform1i(glGetUniformLocation(program_, "plane[3]"), 3);\r
111         }\r
112 \r
113         GLint get_location(const char* name)\r
114         {\r
115                 GLint loc = glGetUniformLocation(program_, name);\r
116                 return loc;\r
117         }\r
118 \r
119         shader_program& operator=(shader_program&& other) \r
120         {\r
121                 program_ = other.program_; \r
122                 other.program_ = 0; \r
123                 return *this;\r
124         }\r
125 \r
126         ~shader_program()\r
127         {\r
128                 glDeleteProgram(program_);\r
129         }\r
130 \r
131         void use()\r
132         {       \r
133                 GL(glUseProgramObjectARB(program_));            \r
134         }\r
135 };\r
136 \r
137 GLubyte progressive_pattern[] = {\r
138         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\r
139         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\r
140         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\r
141         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xFF, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};\r
142         \r
143 GLubyte upper_pattern[] = {\r
144         0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,\r
145         0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,\r
146         0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,\r
147         0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00};\r
148                 \r
149 GLubyte lower_pattern[] = {\r
150         0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, \r
151         0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,\r
152         0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,\r
153         0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff};\r
154 \r
155 struct image_kernel::implementation : boost::noncopyable\r
156 {       \r
157         std::unordered_map<core::pixel_format::type, shader_program> shaders_;\r
158 \r
159 public:\r
160         std::unordered_map<core::pixel_format::type, shader_program>& shaders()\r
161         {\r
162                 GL(glEnable(GL_POLYGON_STIPPLE));\r
163                 GL(glEnable(GL_BLEND));\r
164                 GL(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));  \r
165 \r
166                 if(shaders_.empty())\r
167                 {\r
168                 std::string common_vertex = \r
169                         "void main()                                                                                                                    "\r
170                         "{                                                                                                                                              "\r
171                         "       gl_TexCoord[0] = gl_MultiTexCoord0;                                                                     "\r
172                         "       gl_FrontColor = gl_Color;                                                                                       "\r
173                         "       gl_Position = ftransform();                                                                                     "\r
174                         "}                                                                                                                                              ";\r
175 \r
176                 std::string common_fragment = \r
177                         "uniform sampler2D      plane[4];                                                                                       "\r
178                         "uniform float          gain;                                                                                           "\r
179                         "uniform bool           HD;                                                                                                     "\r
180                                                                                                                                                                 \r
181                         // NOTE: YCbCr, ITU-R, http://www.intersil.com/data/an/an9717.pdf               \r
182                         // TODO: Support for more yuv formats might be needed.                                  \r
183                         "vec4 ycbcra_to_bgra_sd(float y, float cb, float cr, float a)                   "\r
184                         "{                                                                                                                                              "\r
185                         "       cb -= 0.5;                                                                                                                      "\r
186                         "       cr -= 0.5;                                                                                                                      "\r
187                         "       y = 1.164*(y-0.0625);                                                                                           "\r
188                         "                                                                                                                                               "\r
189                         "       vec4 color;                                                                                                                     "\r
190                         "       color.r = y + 1.596 * cr;                                                                                       "\r
191                         "       color.g = y - 0.813 * cr - 0.391 * cb;                                                          "\r
192                         "       color.b = y + 2.018 * cb;                                                                                       "\r
193                         "       color.a = a;                                                                                                            "\r
194                         "                                                                                                                                               "\r
195                         "       return color;                                                                                                           "\r
196                         "}                                                                                                                                              "                       \r
197                         "                                                                                                                                               "\r
198 \r
199                         "vec4 ycbcra_to_bgra_hd(float y, float cb, float cr, float a)                   "\r
200                         "{                                                                                                                                              "\r
201                         "       cb -= 0.5;                                                                                                                      "\r
202                         "       cr -= 0.5;                                                                                                                      "\r
203                         "       y = 1.164*(y-0.0625);                                                                                           "\r
204                         "                                                                                                                                               "\r
205                         "       vec4 color;                                                                                                                     "\r
206                         "       color.r = y + 1.793 * cr;                                                                                       "\r
207                         "       color.g = y - 0.534 * cr - 0.213 * cb;                                                          "\r
208                         "       color.b = y + 2.115 * cb;                                                                                       "\r
209                         "       color.a = a;                                                                                                            "\r
210                         "                                                                                                                                               "\r
211                         "       return color;                                                                                                           "\r
212                         "}                                                                                                                                              "                       \r
213                         "                                                                                                                                               ";\r
214                         \r
215                 shaders_[core::pixel_format::abgr] = shader_program(common_vertex, common_fragment +\r
216 \r
217                         "void main()                                                                                                                    "\r
218                         "{                                                                                                                                              "\r
219                         "       vec4 abgr = texture2D(plane[0], gl_TexCoord[0].st);                                     "\r
220                         "       gl_FragColor = abgr.argb * gain;                                                                        "\r
221                         "}                                                                                                                                              ");\r
222                 \r
223                 shaders_[core::pixel_format::argb]= shader_program(common_vertex, common_fragment +\r
224 \r
225                         "void main()                                                                                                                    "       \r
226                         "{                                                                                                                                              "\r
227                         "       vec4 argb = texture2D(plane[0], gl_TexCoord[0].st);                                     "\r
228                         "       gl_FragColor = argb.grab * gl_Color * gain;                                                     "\r
229                         "}                                                                                                                                              ");\r
230                 \r
231                 shaders_[core::pixel_format::bgra]= shader_program(common_vertex, common_fragment +\r
232 \r
233                         "void main()                                                                                                                    "\r
234                         "{                                                                                                                                              "\r
235                         "       vec4 bgra = texture2D(plane[0], gl_TexCoord[0].st);                                     "\r
236                         "       gl_FragColor = bgra.rgba * gl_Color * gain;                                                     "\r
237                         "}                                                                                                                                              ");\r
238                 \r
239                 shaders_[core::pixel_format::rgba] = shader_program(common_vertex, common_fragment +\r
240 \r
241                         "void main()                                                                                                                    "\r
242                         "{                                                                                                                                              "\r
243                         "       vec4 rgba = texture2D(plane[0], gl_TexCoord[0].st);                                     "\r
244                         "       gl_FragColor = rgba.bgra * gl_Color * gain;                                                     "\r
245                         "}                                                                                                                                              ");\r
246                 \r
247                 shaders_[core::pixel_format::ycbcr] = shader_program(common_vertex, common_fragment +\r
248 \r
249                         "void main()                                                                                                                    "\r
250                         "{                                                                                                                                              "\r
251                         "       float y  = texture2D(plane[0], gl_TexCoord[0].st).r;                            "\r
252                         "       float cb = texture2D(plane[1], gl_TexCoord[0].st).r;                            "\r
253                         "       float cr = texture2D(plane[2], gl_TexCoord[0].st).r;                            "\r
254                         "       float a = 1.0;                                                                                                          "       \r
255                         "       if(HD)                                                                                                                          "\r
256                         "               gl_FragColor = ycbcra_to_bgra_hd(y, cb, cr, a) * gl_Color * gain;"\r
257                         "       else                                                                                                                            "\r
258                         "               gl_FragColor = ycbcra_to_bgra_sd(y, cb, cr, a) * gl_Color * gain;"\r
259                         "}                                                                                                                                              ");\r
260                 \r
261                 shaders_[core::pixel_format::ycbcra] = shader_program(common_vertex, common_fragment +\r
262 \r
263                         "void main()                                                                                                                    "\r
264                         "{                                                                                                                                              "\r
265                         "       float y  = texture2D(plane[0], gl_TexCoord[0].st).r;                            "\r
266                         "       float cb = texture2D(plane[1], gl_TexCoord[0].st).r;                            "\r
267                         "       float cr = texture2D(plane[2], gl_TexCoord[0].st).r;                            "\r
268                         "       float a  = texture2D(plane[3], gl_TexCoord[0].st).r;                            "\r
269                         "       if(HD)                                                                                                                          "\r
270                         "               gl_FragColor = ycbcra_to_bgra_hd(y, cb, cr, a) * gl_Color * gain;"\r
271                         "       else                                                                                                                            "\r
272                         "               gl_FragColor = ycbcra_to_bgra_sd(y, cb, cr, a) * gl_Color * gain;"\r
273                         "}                                                                                                                                              ");\r
274                 }\r
275                 return shaders_;\r
276         }\r
277 };\r
278 \r
279 image_kernel::image_kernel() : impl_(new implementation()){}\r
280 \r
281 void image_kernel::apply(const core::pixel_format_desc& pix_desc, const core::image_transform& transform)\r
282 {\r
283         impl_->shaders()[pix_desc.pix_fmt].use();\r
284 \r
285         GL(glUniform1f(impl_->shaders()[pix_desc.pix_fmt].get_location("gain"), static_cast<GLfloat>(transform.get_gain())));\r
286         GL(glUniform1i(impl_->shaders()[pix_desc.pix_fmt].get_location("HD"), pix_desc.planes.at(0).height > 700 ? 1 : 0));\r
287 \r
288         if(transform.get_mode() == core::video_mode::upper)\r
289                 glPolygonStipple(upper_pattern);\r
290         else if(transform.get_mode() == core::video_mode::lower)\r
291                 glPolygonStipple(lower_pattern);\r
292         else\r
293                 glPolygonStipple(progressive_pattern);\r
294 }\r
295 \r
296 }}