File: vgrabbj.c

package info (click to toggle)
vgrabbj 0.9.6-3.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 716 kB
  • ctags: 297
  • sloc: sh: 3,058; ansic: 2,492; makefile: 71
file content (407 lines) | stat: -rw-r--r-- 11,587 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
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
/* Simple Video4Linux image grabber. Made for my Philips Vesta Pro
 * 
 * Copyright (C) 2000, 2001, 2002 Jens Gecius, Hannover, Germany
 * eMail: devel@gecius.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 2 of the License, or
 * (at you 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 <vgrabbj.h>
#include <v_plist.h>

int signal_terminate=0;

void cleanup(struct vconfig *vconf) {
  int tmp=vconf->loop;

  v_update_ptr(vconf);
  if (vconf->openonce) {
    free_mmap(vconf);
    close_device(vconf);
    }
#ifdef LIBTTF
  if ( vconf->use_ts && vconf->ttinit ) {
    Face_Done(vconf->ttinit->instance, vconf->ttinit->face);
    TT_Done_FreeType(vconf->ttinit->engine);
  }
  if ( vconf->ttinit ) {
    vconf->ttinit->properties=free_ptr(vconf->ttinit->properties);
    vconf->ttinit=free_ptr(vconf->ttinit);
  }
  vconf->timestamp=free_ptr(vconf->timestamp);
  vconf->font=free_ptr(vconf->font);
  v_error(vconf, LOG_DEBUG, "TrueType engine ended, vars freed");
#endif
  vconf->buffer=free_ptr(vconf->buffer);
  vconf->o_buffer=free_ptr(vconf->o_buffer);
  v_error(vconf, LOG_DEBUG, "Buffers freed");
  vconf->tmpout=free_ptr(vconf->tmpout);
  vconf->in=free_ptr(vconf->in);
  vconf->out=free_ptr(vconf->out);
  vconf->conf_file=free_ptr(vconf->conf_file);
  v_error(vconf, LOG_DEBUG, "vars freed");

  vconf=free_ptr(vconf);
			 
  if ( tmp ) {
     closelog();
    _exit(1);
  }
}

/* Event handler for SIGHUP */
/* to re-read the configuration file */



void v_error(struct vconfig *vconf, int msg, char *fmt, ...)
{
  va_list arg_ptr;
  static char buf[MAX_ERRORMSG_LENGTH];

  /* Loglevels:

     0     silent (NONE)
     2     severe errors which cause vgrabbj to exit in daemon mode (LOG_CRIT)
     3     critical errors which cause vgrabbj to exit in non-daemon mode
           and retry forever in daemon-mode (with a sleep(1) inbetween) (LOG_ERR)
     4     warning conditions (LOG_WARNING)
     6     normal information (LOG_INFO)
     7     debug information (LOG_DEBUG)

     Every log-message with a loglevel equal-smaller than LOGLEVEL-Level (supplied
     via command line, otherwise default=4) will be 
         a) displayed on screen (stderr) if non-daemon
         b) written to syslog if daemon

     One exception:

     In daemon mode, every message before successful fork into background
     will be written to stderr (if any).

  */

  if ( msg <= vconf->debug ) {
   
    va_start(arg_ptr, fmt);
    vsprintf(buf, fmt, arg_ptr);
    strcat(buf, "\n");
    va_end(arg_ptr);
    if (vconf->loop && vconf->init_done) {
      syslog(msg, buf);
      if (msg == 3) {
	sleep(1);
      }
    } else {
      fprintf(stderr, buf);
      fflush(stderr);
    }
    
  }

  /* Decision about exiting is independent of log or not to log */

  if ( (vconf->loop && msg < 3) || (msg==3 && vconf->err_count++ > 3600) ) {
    syslog(LOG_ERR, "Fatal Error (daemon), exiting...\n"); // exit
    cleanup(vconf);
    closelog();
    _exit(1);
  } else if ( !vconf->loop && msg < 4 ) {
    fprintf(stderr, "Fatal Error (non-daemon), exiting...\n"); // exit
    cleanup(vconf);
    exit(1);
  }
}

/* Get information from v4l device and show them  */

void show_capabilities(char *in, char *pname) 
{
  struct video_capability cap;
  struct video_window win;
  struct video_picture pic;
  int dev;

  if ( (dev = open(in, O_RDONLY)) < 0 ) {
    fprintf(stderr, "Can't open device %s\n", in);
    exit(1);
  }
  if (ioctl(dev, VIDIOCGCAP, &cap) < 0) {
    fprintf(stderr, "Can't get capabilities of device %s\n", in);
    exit(1);
  }
  if (ioctl(dev, VIDIOCGPICT, &pic) < 0) {
    fprintf(stderr, "Can't get picture properties of device %s\n", in);
    exit(1);
  }
  if (ioctl(dev, VIDIOCGWIN, &win) < 0) {
    fprintf(stderr, "Can't get overlay values of device %s\n", in);
    exit(1);
  }

  fprintf(stderr,"%s, Version %s\n"
	  "Videodevice name: %s (%s)\n"
	  "Capabilities\n"
	  "Type     : %d\tValues can be looked up at videodev.h\n"
	  "Channels : %d\n"
	  "Audio    : %d\n"
	  "MaxWidth : %d\n"
	  "MaxHeight: %d\n"
	  "MinWidth : %d\n"
	  "MinHeigth: %d\n"
	  "\nCurrent Settings:\n"
	  "Brightness: %d\n"
	  "Hue       : %d\n"
	  "Color     : %d\n"
	  "Contrast  : %d\n"
	  "Whiteness : %d\n"
	  "Depth     : %d\n"
	  "Palette   : %s (%d)\n"
	  "Width     : %d\n"
	  "Height    : %d\n"
	  "Chromakey : %d\n",
	  basename(pname), VERSION, in, cap.name, cap.type, cap.channels, cap.audios,
	  cap.maxwidth, cap.maxheight, cap.minwidth, cap.minheight,
	  pic.brightness, pic.hue, pic.colour, pic.contrast,
	  pic.whiteness, pic.depth, plist[pic.palette].name, pic.palette,
	  win.width, win.height, win.chromakey);
  dev=close(dev);
  if (dev)
    fprintf(stderr, "Error occured while closing %s\n", in);
  exit(0);
}


/* Read the image from device, adjust brightness, if wanted, for RGB24 */

unsigned char *read_image(struct vconfig *vconf, int size) {
  int f, newbright;
  int err_count;
  int discard=vconf->discard;

  v_error(vconf, LOG_DEBUG, "Palette to be used: %s (%d), size: %d",
	  plist[vconf->vpic.palette].name, vconf->vpic.palette, img_size(vconf, vconf->vpic.palette));

  /* Opening input device */
  if ( !vconf->openonce ) {
    open_device(vconf);

    /* and Re-initialize the palette, in case someone changed it meanwhile */

    while (ioctl(vconf->dev, VIDIOCSPICT, &vconf->vpic) < 0 )
      v_error(vconf, LOG_ERR, "Device %s couldn't be reset to known palette %s",
	      vconf->in, vconf->vpic.palette);
    if (vconf->windowsize)
      while (ioctl(vconf->dev, VIDIOCSWIN, &vconf->win) )
	v_error(vconf, LOG_ERR, "Problem setting window size"); // exit

    set_picture_parms(vconf);
  }


  /* Read image via read() */

  if (!vconf->usemmap) {
    if (vconf->autobrightness)
      v_error(vconf, LOG_INFO, "Forced to use brightness adj. - using read()");
    else
      v_error(vconf, LOG_INFO, "Could not get mmap-buffer - Falling back to read()");
    do {
      err_count=0;
      if (vconf->autobrightness && vconf->vpic.palette==VIDEO_PALETTE_RGB24) {
	v_error(vconf, LOG_INFO, "Doing brightness adjustment");
	do {
	  while (read(vconf->dev, vconf->buffer, size) < size)
	    v_error(vconf, LOG_ERR, "Error reading from %s", vconf->in);
	  f = brightness_adj(vconf, &newbright);
	  if (f) {
	    vconf->vpic.brightness += (newbright << 8);
	    if (ioctl(vconf->dev, VIDIOCSPICT, &vconf->vpic)==-1) 
	      v_error(vconf, LOG_WARNING, "Problem setting brightness");
	    err_count++;
	  
	    if (err_count>100) {
	      v_error(vconf, LOG_WARNING, "Brightness not optimal");
	      break;
	    }
	  }
	} while (f);
	v_error(vconf, LOG_INFO, "Brightness adjusted");
      } else {
	v_error(vconf, LOG_DEBUG, "Using normal read for image grabbing");
	read(vconf->dev, vconf->buffer, size);
      }
    } while (discard--);

    /* We're reading the image via a mmap'd area of the driver */

  } else { 
    v_error(vconf, LOG_DEBUG, "Using mmap for image grabbing");
    if (!vconf->openonce)
      init_mmap(vconf);
    do {
      err_count=0;
      do {
	if  (err_count++>100) {
	  v_error(vconf, LOG_ERR, "Could not grab frame (100 tries)");
	  break;
	}
      } while (ioctl(vconf->dev, VIDIOCMCAPTURE, &vconf->vmap) < 0);

      err_count=0;
      do {
	if (err_count++>100) {
	  v_error(vconf, LOG_ERR, "Could not sync with frame (100 tries)");
	  break;
	}
      } while (ioctl(vconf->dev, VIDIOCSYNC, &vconf->vmap.frame) < 0);

      vconf->buffer=memcpy(vconf->buffer, vconf->map+vconf->vbuf.offsets[vconf->vmap.frame], size);

      if (discard)
	v_error(vconf, LOG_DEBUG, "%d frames to discard", discard);
      
    } while (discard--);
    if (!vconf->openonce)
      free_mmap(vconf);
  }
  
  v_error(vconf, LOG_DEBUG, "Image successfully read");

  if (!vconf->openonce)
    close_device(vconf);
  
  return vconf->buffer;
}


unsigned char *conv_image(struct vconfig *vconf) {

  switch (vconf->vpic.palette) {
  case VIDEO_PALETTE_RGB24:
    vconf->o_buffer=memcpy(vconf->o_buffer, vconf->buffer, 
		    vconf->win.width * vconf->win.height * 3);
    v_error(vconf, LOG_DEBUG, "No conversion, we have RGB24");
    break;
  case VIDEO_PALETTE_RGB32:
    v_error(vconf, LOG_INFO, "Got RGB32, converting...");
   
    vconf->o_buffer=conv_rgb32_rgb24(vconf);
    break;
  case VIDEO_PALETTE_YUV420P:
    v_error(vconf, LOG_INFO, "Got YUV420p, converting...");
   
    ccvt_420p_bgr24(vconf->win.width, vconf->win.height, vconf->buffer,
		    vconf->buffer + (vconf->win.width * vconf->win.height),
		    vconf->buffer + (vconf->win.width * vconf->win.height)+
		    (vconf->win.width * vconf->win.height / 4),
		    vconf->o_buffer);
    break;
  case VIDEO_PALETTE_YUV420:
    v_error(vconf, LOG_INFO, "Got YUV420, converting...");

    ccvt_420i_rgb24(vconf->win.width, vconf->win.height, vconf->buffer, vconf->o_buffer);
    break;
  case VIDEO_PALETTE_YUYV:
  case VIDEO_PALETTE_YUV422:
    v_error(vconf, LOG_INFO, "Got YUYV, converting...");
      
    ccvt_yuyv_bgr24(vconf->win.width, vconf->win.height, vconf->buffer, vconf->o_buffer);
    break;
  default:
    v_error(vconf, LOG_CRIT, "Should not happen - Unknown input image format");
    break;
  }

  if (vconf->vpic.palette!=VIDEO_PALETTE_RGB24)
    v_error(vconf, LOG_DEBUG, "converted to RGB24");
     
  if (vconf->switch_bgr) {
    vconf->o_buffer=switch_color(vconf);
    v_error(vconf, LOG_DEBUG, "Switched from BGR to RGB - or vice versa");
  }
  return vconf->o_buffer;
}
		

/* Main loop  */

int main(int argc, char *argv[]) 
{
  struct vconfig *vconf=NULL;

#ifdef DEBUGGING
  mtrace();
#endif

  vconf=v_init(vconf, argc, argv);

  if (vconf->loop && (!vconf->nofork) ) 
    daemonize(vconf, basename(argv[0]));
  else
    v_error(vconf, LOG_WARNING, "Reading image from %s", vconf->in);

  // now start the loop (if daemon), read image and convert it, if necessary 
  do {
    vconf->buffer=read_image(vconf, img_size(vconf, vconf->vpic.palette));
    vconf->o_buffer = conv_image(vconf);
    
    if (vconf->swaprl) 
      vconf->o_buffer=swap_left_right(vconf->o_buffer, vconf->win.width, vconf->win.height);

    if (vconf->swaptb)
      vconf->o_buffer=swap_top_bottom(vconf->o_buffer, vconf->win.width, vconf->win.height);

#ifdef LIBTTF
    if (vconf->use_ts) 
      vconf->o_buffer=inserttext(vconf->ttinit, vconf->o_buffer, vconf);
#endif
    
    write_image(vconf);
    
    vconf->err_count=0;
    usleep(vconf->loop);
    
    // We got a signal, we do have to...
    switch (signal_terminate) {
    case 0:
      // No signal received
      break;
    case SIGTERM:
      // We got sigkill, cleanup (for daemon mode) and exit
      signal_terminate=0;
      cleanup(vconf);
      break;
    case SIGHUP:
      // We got sighup, re-read the config-file (do NOT parse commandline)
      signal_terminate=0;
      vconf=v_reinit(vconf);
      break;
    default:
      v_error(vconf, LOG_DEBUG, "Unknown signal received, ignoring.");
      break;
    }
  } while (vconf->loop);
  
  cleanup(vconf);
  exit(0);
}