File: render_sdl1.c

package info (click to toggle)
guvcview 2.0.4%2Bdebian-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,940 kB
  • ctags: 2,911
  • sloc: ansic: 26,210; sh: 11,252; cpp: 3,226; makefile: 300; sed: 16
file content (400 lines) | stat: -rw-r--r-- 10,274 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
/*******************************************************************************#
#           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 <SDL.h>
#include <assert.h>

#include "gview.h"
#include "gviewrender.h"
#include "render.h"

extern int verbosity;

static int desktop_w = 0;
static int desktop_h = 0;

static int bpp = 0;

static SDL_Surface *pscreen = NULL;
static SDL_Overlay *poverlay = NULL;

static SDL_Rect drect;

static Uint32 SDL_VIDEO_Flags =
        SDL_ANYFORMAT | SDL_RESIZABLE;

static const SDL_VideoInfo *info;

/*
 * initialize sdl video
 * args:
 *   width - video width
 *   height - video height
 *   flags - window flags:
 *              0- none
 *              1- fullscreen
 *              2- maximized
 *
 * asserts:
 *   none
 *
 * returns: pointer to SDL_Overlay
 */
static SDL_Overlay * video_init(int width, int height, int flags)
{
	if(verbosity > 0)
		printf("RENDER: Initializing SDL1 render\n");

	int video_w = width;
	int video_h = height;
	
    if (pscreen == NULL) //init SDL
    {
        char driver[128];
        /*----------------------------- Test SDL capabilities ---------------------*/
        if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0)
        {
            fprintf(stderr, "RENDER: Couldn't initialize SDL: %s\n", SDL_GetError());
            return NULL;
        }

        /*use hardware acceleration as default*/
        if ( ! getenv("SDL_VIDEO_YUV_HWACCEL") ) putenv("SDL_VIDEO_YUV_HWACCEL=1");
        if ( ! getenv("SDL_VIDEO_YUV_DIRECT") ) putenv("SDL_VIDEO_YUV_DIRECT=1");


        if (SDL_VideoDriverName(driver, sizeof(driver)) && verbosity > 0)
        {
            printf("RENDER: Video driver: %s\n", driver);
        }

        info = SDL_GetVideoInfo();

        if (info->wm_available && verbosity > 0) printf("RENDER: A window manager is available\n");

        if (info->hw_available)
        {
            if (verbosity > 0)
                printf("RENDER: Hardware surfaces are available (%dK video memory)\n", info->video_mem);

            SDL_VIDEO_Flags |= SDL_HWSURFACE;
            SDL_VIDEO_Flags |= SDL_DOUBLEBUF;
        }
        else
        {
            SDL_VIDEO_Flags |= SDL_SWSURFACE;
        }

        if (info->blit_hw)
        {
            if (verbosity > 0) printf("RENDER: Copy blits between hardware surfaces are accelerated\n");

            SDL_VIDEO_Flags |= SDL_ASYNCBLIT;
        }

        desktop_w = info->current_w; //get desktop width
        desktop_h = info->current_h; //get desktop height

        if(!desktop_w) desktop_w = 800;
        if(!desktop_h) desktop_h = 600;

        switch(flags)
	    {
		    case 2: /*maximize*/
		       video_w = desktop_w;
		       video_h = desktop_h;
		       break;
		    case 1: /*fullscreen*/
		       SDL_VIDEO_Flags |= SDL_FULLSCREEN;
		       break;
		    case 0:
		    default:
		       break;
	    }

        if (verbosity > 0)
        {
            if (info->blit_hw_CC) printf("Colorkey blits between hardware surfaces are accelerated\n");
            if (info->blit_hw_A) printf("Alpha blits between hardware surfaces are accelerated\n");
            if (info->blit_sw) printf("Copy blits from software surfaces to hardware surfaces are accelerated\n");
            if (info->blit_sw_CC) printf("Colorkey blits from software surfaces to hardware surfaces are accelerated\n");
            if (info->blit_sw_A) printf("Alpha blits from software surfaces to hardware surfaces are accelerated\n");
            if (info->blit_fill) printf("Color fills on hardware surfaces are accelerated\n");
        }

        SDL_WM_SetCaption("Guvcview Video", NULL);

        /* enable key repeat */
        SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
    }

    /*------------------------------ SDL init video ---------------------*/
    if(verbosity > 0)
    {
        printf("RENDER: Desktop resolution = %ix%i\n", desktop_w, desktop_h);
        printf("RENDER: Checking video mode %ix%i@32bpp\n", video_w, video_h);
    }

    bpp = SDL_VideoModeOK(
        video_w,
        video_h,
        32,
        SDL_VIDEO_Flags);

    if(!bpp)
    {
        fprintf(stderr, "RENDER: resolution not available\n");
        /*resize video mode*/
        if ((video_w > desktop_w) || (video_h > desktop_h))
        {
            video_w = desktop_w; /*use desktop video resolution*/
            video_h = desktop_h;
        }
        else
        {
            video_w = 800;
            video_h = 600;
        }
        fprintf(stderr, "RENDER: resizing to %ix%i\n", video_w, video_h);

    }
    else
    {
        if ((bpp != 32) && verbosity > 0) printf("RENDER: recomended color depth = %i\n", bpp);
    }

	if(verbosity > 0)
		printf("RENDER: setting video mode %ix%i@%ibpp\n", width, height, bpp);

    pscreen = SDL_SetVideoMode(
        video_w,
        video_h,
        bpp,
        SDL_VIDEO_Flags);

    if(pscreen == NULL)
    {
        return (NULL);
    }
    if(verbosity > 0)
		printf("RENDER: creating an overlay\n");
    /*use requested resolution for overlay even if not available as video mode*/
    SDL_Overlay* overlay=NULL;
    overlay = SDL_CreateYUVOverlay(width, height,
		SDL_IYUV_OVERLAY, /*yuv420p*/
		pscreen);

    SDL_ShowCursor(SDL_DISABLE);
    return (overlay);
}

/*
 * init sdl1 render
 * args:
 *    width - overlay width
 *    height - overlay height
 *    flags - window flags:
 *              0- none
 *              1- fullscreen
 *              2- maximized
 *
 * asserts:
 *
 * returns: error code (0 ok)
 */
 int init_render_sdl1(int width, int height, int flags)
 {
	poverlay = video_init(width, height, flags);

	if(poverlay == NULL)
	{
		fprintf(stderr, "RENDER: Couldn't create yuv overlay (try disabling hardware accelaration)\n");
		return -1;
	}

	assert(pscreen != NULL);

	drect.x = 0;
	drect.y = 0;
	drect.w = pscreen->w;
	drect.h = pscreen->h;

	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_sdl1_frame(uint8_t *frame, int width, int height)
{
	/*asserts*/
	assert(poverlay != NULL);
	assert(frame != NULL);

	uint8_t *p = (uint8_t *) poverlay->pixels[0];

	int size = width * height * 3/2; /* for IYUV */

	 SDL_LockYUVOverlay(poverlay);
     memcpy(p, frame, size);

     SDL_UnlockYUVOverlay(poverlay);
     SDL_DisplayYUVOverlay(poverlay, &drect);
}

/*
 * set sdl1 render caption
 * args:
 *   caption - string with render window caption
 *
 * asserts:
 *   none
 *
 * returns: none
 */
void set_render_sdl1_caption(const char* caption)
{
	SDL_WM_SetCaption(caption, NULL);
}

/*
 * dispatch sdl1 render events
 * args:
 *   none
 *
 * asserts:
 *   none
 *
 * returns: none
 */
void render_sdl1_dispatch_events()
{
	SDL_Event event;

	/* Poll for events */
	while( SDL_PollEvent(&event) )
	{
		if(event.type==SDL_KEYDOWN)
		{
			switch( event.key.keysym.sym )
            {
				/* Keyboard event */
                /* Pass the event data onto PrintKeyInfo() */
                case SDLK_ESCAPE:
					render_call_event_callback(EV_QUIT);
					break;

				case SDLK_UP:
					render_call_event_callback(EV_KEY_UP);
					break;

				case SDLK_DOWN:
					render_call_event_callback(EV_KEY_DOWN);
					break;

				case SDLK_RIGHT:
					render_call_event_callback(EV_KEY_RIGHT);
					break;

				case SDLK_LEFT:
					render_call_event_callback(EV_KEY_LEFT);
					break;

				case SDLK_SPACE:
					render_call_event_callback(EV_KEY_SPACE);
					break;

				case SDLK_i:
					render_call_event_callback(EV_KEY_I);
					break;

				case SDLK_v:
					render_call_event_callback(EV_KEY_V);
					break;

				default:
					break;

			}

			switch( event.key.keysym.scancode )
			{
				case 220: /*webcam button*/
					break;
			}
		}

		if(event.type==SDL_VIDEORESIZE)
		{
			pscreen =
				SDL_SetVideoMode(
					event.resize.w,
					event.resize.h,
					bpp,
					SDL_VIDEO_Flags);

			drect.w = event.resize.w;
			drect.h = event.resize.h;
		}

		if(event.type==SDL_QUIT)
		{
			if(verbosity > 0)
				printf("RENDER: (event) quit\n");
			render_call_event_callback(EV_QUIT);
		}
	}
}
/*
 * clean sdl1 render data
 * args:
 *   none
 *
 * asserts:
 *   none
 *
 * returns: none
 */
void render_sdl1_clean()
{
	if(poverlay)
		SDL_FreeYUVOverlay(poverlay);
	poverlay = NULL;

	SDL_Quit();

	pscreen = NULL;
}