File: render_sfml.cpp

package info (click to toggle)
guvcview 2.0.6%2Bdebian-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 5,848 kB
  • sloc: ansic: 26,607; sh: 4,156; cpp: 3,586; makefile: 312; sed: 16
file content (474 lines) | stat: -rw-r--r-- 11,883 bytes parent folder | download
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*******************************************************************************#
#           guvcview              http://guvcview.sourceforge.net               #
#                                                                               #
#           Paulo Assis <pj.assis@gmail.com>                                    #
#           Nobuhiro Iwamatsu <iwamatsu@nigauri.org>                            #
#                             Add UYVY color support(Macbook iSight)            #
#           Flemming Frandsen <dren.dk@gmail.com>                               #
#                             Add VU meter OSD                                  #
#                                                                               #
# 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 2 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, write to the Free Software                   #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA     #
#                                                                               #
********************************************************************************/

#include "render_sfml.hpp"
#include <iostream>
#include <cstring>

extern "C" {
#include "gview.h"
#include "gviewrender.h"
#include "render.h"
#include "render_sfml.h"
#include "../config.h"
}

extern int verbosity;

/*
 * yu12 to rgba (rgb32)
 * args:
 *    out - pointer to output rgba data buffer
 *    in - pointer to input yu12 data buffer
 *    width - buffer width (in pixels)
 *    height - buffer height (in pixels)
 *
 * asserts:
 *    none
 *
 * returns: none
 */
static void yu12_to_rgba (uint8_t *out, uint8_t *in, int width, int height)
{
	uint8_t *py1 = in; //line 1
	uint8_t *py2 = py1 + width; //line 2
	uint8_t *pu = in + (width * height);
	uint8_t *pv = pu + ((width * height) / 4);

	uint8_t *pout1 = out; //first line
	uint8_t *pout2 = out + (width * 4); //second line

	int h=0, w=0;

	for(h=0; h < height; h+=2) //every two lines
	{
		py1 = in + (h * width);
		py2 = py1 + width;

		pout1 = out + (h * width * 4);
		pout2 = pout1 + (width * 4);

		for(w=0; w<width; w+=2) //every 2 pixels
		{
			/* standart: r = y0 + 1.402 (v-128) */
			/* logitech: r = y0 + 1.370705 (v-128) */
			*pout1++=CLIP(*py1 + 1.402 * (*pv-128));
			*pout2++=CLIP(*py2 + 1.402 * (*pv-128));
			/* standart: g = y0 - 0.34414 (u-128) - 0.71414 (v-128)*/
			/* logitech: g = y0 - 0.337633 (u-128)- 0.698001 (v-128)*/
			*pout1++=CLIP(*py1 - 0.34414 * (*pu-128) -0.71414*(*pv-128));
			*pout2++=CLIP(*py2 - 0.34414 * (*pu-128) -0.71414*(*pv-128));
			/* standart: b = y0 + 1.772 (u-128) */
			/* logitech: b = y0 + 1.732446 (u-128) */
			*pout1++=CLIP(*py1 + 1.772 *( *pu-128));
			*pout2++=CLIP(*py2 + 1.772 *( *pu-128));
			/* alpha set to 255*/
			*pout1++=255;
			*pout2++=255;

			py1++;
			py2++;

			/* standart: r1 =y1 + 1.402 (v-128) */
			/* logitech: r1 = y1 + 1.370705 (v-128) */
			*pout1++=CLIP(*py1 + 1.402 * (*pv-128));
			*pout2++=CLIP(*py2 + 1.402 * (*pv-128));
			/* standart: g1 = y1 - 0.34414 (u-128) - 0.71414 (v-128)*/
			/* logitech: g1 = y1 - 0.337633 (u-128)- 0.698001 (v-128)*/
			*pout1++=CLIP(*py1 - 0.34414 * (*pu-128) -0.71414 * (*pv-128));
			*pout2++=CLIP(*py2 - 0.34414 * (*pu-128) -0.71414 * (*pv-128));
			/* standart: b1 = y1 + 1.772 (u-128) */
			/* logitech: b1 = y1 + 1.732446 (u-128) */
			*pout1++=CLIP(*py1 + 1.772 * (*pu-128));
			*pout2++=CLIP(*py2 + 1.772 * (*pu-128));
			/* alpha set to 255*/
			*pout1++=255;
			*pout2++=255;

			py1++;
			py2++;
			pu++;
			pv++;
		}
	}
}

SFMLRender::SFMLRender(int width, int height, int flags, int win_w, int win_h)
{
	int w = width;
	int h = height;

	if(win_w > 0)
		w = win_w;
	if(win_h > 0)
		h = win_h;

	use_shader = true;

	pix = NULL;

	//get the current resolution
	sf::VideoMode display_mode = sf::VideoMode::getDesktopMode();

	if(verbosity > 0)
		std::cout << "RENDER: (SFML) desktop resolution (" <<
			display_mode.width << "x" <<
			display_mode.height << ")" << std::endl;

	if((display_mode.width > 0) && (display_mode.height > 0))
	{
		if(w > display_mode.width)
			w = display_mode.width;
		if(h > display_mode.height)
			h = display_mode.height;
	}

	// Create the main window
	if(flags == 1) //fullscreen
	{
		window.create(sf::VideoMode(w, h), "SFML window", sf::Style::Fullscreen);
		if(!window.isOpen())
		{
			std::cerr << "RENDER: (SFML) couldn't open window in fullscreen mode" << std::endl;
			window.create(sf::VideoMode(w, h), "SFML window");
		}
	}
	else
		window.create(sf::VideoMode(w, h), "SFML window");

	if(!window.isOpen())
	{
		std::cerr << "RENDER: (SFML) couldn't open window" << std::endl;
		return;
	}

	if(!texture.create(width, height))
	{
		std::cerr << "RENDER: (SFML) couldn't create texture" << std::endl;
		return;
	}

	if (sf::Shader::isAvailable() )
	{
		const std::string fragmentShader = \
			"uniform sampler2D texY;" \
			"uniform sampler2D texU;" \
			"uniform sampler2D texV;" \
			"void main(void)" \
			"{" \
				"float nx, ny;" \
				"vec3 yuv;" \
				"vec3 rgb;" \
				"vec2 norm_pos = vec2( gl_TexCoord[0].x,gl_TexCoord[0].y);" \
				"yuv.x = texture2D(texY, norm_pos).r;" \
				"yuv.y = texture2D(texU, norm_pos).r - 0.5;" \
				"yuv.z = texture2D(texV, norm_pos).r - 0.5;" \
				"rgb = mat3(      1,       1,       1," \
				"                 0, -.21482, 2.12798," \
				"           1.28033, -.38059,       0) * yuv;" \
				"gl_FragColor = vec4(rgb, 1.0);" \
			"}";

		if(!conv_yuv2rgb_shd.loadFromMemory(fragmentShader, sf::Shader::Fragment))
		{
			std::cerr << "RENDER: (SFML) couldn't load fragment shader for yuv conversion" << std::endl;
			use_shader = false;
		}
		else
		{
			if(!texY.create(width, height))
			{
				std::cerr << "RENDER: (SFML) couldn't create texture" << std::endl;
				return;
			}

			if(!texU.create(width/2, height/2))
			{
				std::cerr << "RENDER: (SFML) couldn't create texture" << std::endl;
				return;
			}

			if(!texV.create(width/2, height/2))
			{
				std::cerr << "RENDER: (SFML) couldn't create texture" << std::endl;
				return;
			}

#if LIBSFML_VER_AT_LEAST(2,4)
			conv_yuv2rgb_shd.setUniform("texY", texY);
			conv_yuv2rgb_shd.setUniform("texU", texU);
			conv_yuv2rgb_shd.setUniform("texV", texV);
#else
                        conv_yuv2rgb_shd.setParameter("texY", texY);
			conv_yuv2rgb_shd.setParameter("texU", texU);
			conv_yuv2rgb_shd.setParameter("texV", texV);
#endif
		}
	}
	else
	{
		std::cerr << "RENDER: (SFML) shader not available for yuv conversion" << std::endl;
		use_shader = false;
	}

	//use pix buffer for rgba conversion
	if(!use_shader)
		pix = (uint8_t *) calloc(width*height*4, sizeof(uint8_t));

	sprite.setTexture(texture);

	texture.setSmooth(true);

	window.clear(sf::Color::Black);
	window.display();

	if(verbosity > 1)
		std::cout << "RENDER: (SFML) window created" << std::endl;
}

SFMLRender::~SFMLRender()
{

	if(pix)
		free(pix);

	window.close();
}

int SFMLRender::render_frame(uint8_t *frame, int width, int height)
{
	//convert yuv to rgba

	if (use_shader)
	{
		uint8_t *pu = frame + (width*height);
		uint8_t *pv = pu + (width*height)/4;

		GLint textureBinding;
		// Save the current texture binding, to avoid messing up SFML's OpenGL states
		glGetIntegerv(GL_TEXTURE_BINDING_2D, &textureBinding);

		// :Y
		sf::Texture::bind(&texY);

		glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, frame);

		// :U
		sf::Texture::bind(&texU);

		glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width/2, height/2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pu);

		// :V
		sf::Texture::bind(&texV);

		glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width/2, height/2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pv);

		// Restore the previous texture binding
		glBindTexture(GL_TEXTURE_2D, textureBinding);

		//draw frame
		window.draw(sprite, &conv_yuv2rgb_shd);
	}
	else
	{
		yu12_to_rgba ((uint8_t *) pix, frame, width, height);
		//update texture
		texture.update(pix);
		//draw frame
		window.draw(sprite);
	}

	window.display();

	return 0;
}

void SFMLRender::set_caption(const char* caption)
{
	window.setTitle(std::string(caption, 0, 30));
}

void SFMLRender::dispatch_events()
{
	// Process events
	sf::Event event;
	while (window.pollEvent(event))
	{
		if (event.type == sf::Event::KeyPressed)
		{
			switch( event.key.code )
			{
				case sf::Keyboard::Escape:
					render_call_event_callback(EV_QUIT);
					break;
				case sf::Keyboard::Up:
					render_call_event_callback(EV_KEY_UP);
					break;

				case sf::Keyboard::Down:
					render_call_event_callback(EV_KEY_DOWN);
					break;

				case sf::Keyboard::Right:
					render_call_event_callback(EV_KEY_RIGHT);
					break;

				case sf::Keyboard::Left:
					render_call_event_callback(EV_KEY_LEFT);
					break;

				case sf::Keyboard::Space:
					render_call_event_callback(EV_KEY_SPACE);
					break;

				case sf::Keyboard::I:
					render_call_event_callback(EV_KEY_I);
					break;

				case sf::Keyboard::V:
					render_call_event_callback(EV_KEY_V);
					break;

				default:
					break;
			}
		}

		// Close window: exit
		if (event.type == sf::Event::Closed)
		{
			if(verbosity > 0)
				std::cout<< "RENDER:(SFML) (event) close\n"<< std::endl;
			render_call_event_callback(EV_QUIT);
		}
	}
}


/******************************* C wrapper functions ********************************/
SFMLRender* render = NULL;

/*
 * init sfml render
 * args:
 *    width - render width
 *    height - render height
 *    flags - window flags:
 *              0- none
 *              1- fullscreen
 *              2- maximized
 *   win_w - window width (0 use render width)
 *   win_h - window height (0 use render height)
 *
 * asserts:
 *
 * returns: error code (0 ok)
 */
 int init_render_sfml(int width, int height, int flags, int win_w, int win_h)
 {
	//clean old render
	if(render)
		delete(render);

	render = new SFMLRender(width, height, flags, win_w, win_h);

	if(!render)
	{
		std::cerr << "RENDER: (SFML) couldn't create render" << std::endl;
		return -1;
	}

	if(!render->has_window())
	{
		std::cerr << "RENDER: (SFML) couldn't create render window" << std::endl;
		return -2;
	}

	return 0;
 }

/*
 * render a frame
 * args:
 *   frame - pointer to frame data (yuyv format)
 *   width - frame width
 *   height - frame height
 *
 * asserts:
 *   poverlay is not nul
 *   frame is not null
 *
 * returns: error code
 */
int render_sfml_frame(uint8_t *frame, int width, int height)
{
	return render->render_frame(frame, width, height);
}

/*
 * set sfml render caption
 * args:
 *   caption - string with render window caption
 *
 * asserts:
 *   none
 *
 * returns: none
 */
void set_render_sfml_caption(const char* caption)
{
	render->set_caption(caption);
}

/*
 * dispatch sfml render events
 * args:
 *   none
 *
 * asserts:
 *   none
 *
 * returns: none
 */
void render_sfml_dispatch_events()
{
	render->dispatch_events();
}

/*
 * clean sfml render data
 * args:
 *   none
 *
 * asserts:
 *   none
 *
 * returns: none
 */
void render_sfml_clean()
{
	delete(render);
	render = NULL;
}