aboutsummaryrefslogtreecommitdiffstats
path: root/grab-v4l.c
blob: ea567ee85583aa96e87e4bf3a18f97fb90ee739f (plain)
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/mman.h>

#include <X11/Intrinsic.h>

#include "grab.h"

#include <asm/types.h>		/* XXX glibc */

#if USE_KERNEL_VIDEODEV
# include <linux/videodev.h>
#else
# include "videodev.h"
#endif

static int MEM_SIZE;
static int MEM_SIZE_try[] = { 0x151000, 0x144000 };

/* ---------------------------------------------------------------------- */

/* prototypes */
static int   grab_open(char *filename, int sw, int sh,
		       int format, int pixmap, void *base, int width);
static int   grab_close();
static int   grab_overlay(int x, int y, int width, int height, int format,
			  struct OVERLAY_CLIP *oc, int count);
static void* grab_scr(void *dest, int width, int height, int single);
static void* grab_one(int width, int height);
static int   grab_tune(unsigned long freq);
static int   grab_tuned();
static int   grab_input(int input, int norm);
static int   grab_picture(int color, int bright, int hue, int contrast);
static int   grab_audio(int mute, int volume, int *mode);

/* ---------------------------------------------------------------------- */

static char *device_cap[] = {
    "capture", "tuner", "teletext", "overlay", "chromakey", "clipping",
    "frameram", "scales", "monochrome", NULL
};

static char *device_pal[] = {
    "-", "grey", "hi240", "rgb16", "rgb24", "rgb32", "rgb15", NULL
};

static struct STRTAB norms[] = {
    {  0, "PAL" },
    {  1, "NTSC" },
    {  2, "SECAM" },
    {  3, "AUTO" },
    { -1, NULL }
};
static struct STRTAB *inputs;

static int    fd = -1;

/* generic informations */
static struct video_capability  capability;
static struct video_channel     *channels;
static struct video_audio       *audios;
static struct video_tuner       tuner;
static struct video_picture     pict;
static int                      cur_input;

/* overlay */
static struct video_window      ov_win;
static struct video_clip        ov_clips[32];
static struct video_buffer      ov_fbuf;

/* screen grab */
static struct video_mmap        gb_even;
static struct video_mmap        gb_odd;
static int                      even,pixmap_bytes;
static int                      gb_grab,gb_sync;

static char *map = NULL;

/* state */
static int                      overlay, swidth, sheight;

/* pass 0/1 by reference */
static int                      one = 1, zero = 0;

struct GRABBER grab_v4l = {
    "video4linux",
    VIDEO_RGB16 | VIDEO_RGB24 | VIDEO_RGB32,
    0,
    norms,NULL,

    grab_open,grab_close,
    grab_overlay,
    grab_scr,
    grab_one,
    grab_tune,
    grab_tuned,
    grab_input,
    grab_picture,
    grab_audio
};

/* ---------------------------------------------------------------------- */

static int
grab_open(char *filename, int sw, int sh,
	  int format, int pixmap, void *base, int width)
{
    int i;

    if (-1 != fd)
	goto err;
    
    if (-1 == (fd = open(filename ? filename : "/dev/video",O_RDWR))) {
	fprintf(stderr,"open %s: %s\n",
		filename ? filename : "/dev/video",strerror(errno));
	goto err;
    }

    if (-1 == ioctl(fd,VIDIOCGCAP,&capability))
	goto err;

    if (debug)
	fprintf(stderr, "v4l: open\n");

    /* XXX set input to television (avoid some trouble while
       asking for settings) */
    grab_input(0,-1);

    swidth  = sw;
    sheight = sh;

    if (debug) {
	fprintf(stderr,"%s:",capability.name);
	for (i = 0; device_cap[i] != NULL; i++)
	    if (capability.type & (1 << i))
		fprintf(stderr," %s",device_cap[i]);
	fprintf(stderr,"\n");
    }

    /* input sources */
    if (debug)
	fprintf(stderr,"  channels: %d\n",capability.channels);
    channels = malloc(sizeof(struct video_channel)*capability.channels);
    memset(channels,0,sizeof(struct video_channel)*capability.channels);
    inputs = malloc(sizeof(struct STRTAB)*(capability.channels+1));
    memset(channels,0,sizeof(struct STRTAB)*(capability.channels+1));
    for (i = 0; i < capability.channels; i++) {
	channels[i].channel = i;
	if (-1 == ioctl(fd,VIDIOCGCHAN,&channels[i]))
	    perror("ioctl VIDIOCGCHAN"), exit(0);
	inputs[i].nr  = i;
	inputs[i].str = channels[i].name;
	if (debug)
	    fprintf(stderr,"    %s: %d %s%s %s%s\n",
		    channels[i].name,
		    channels[i].tuners,
		    (channels[i].flags & VIDEO_VC_TUNER)   ? "tuner "  : "",
		    (channels[i].flags & VIDEO_VC_AUDIO)   ? "audio "  : "",
		    (channels[i].type & VIDEO_TYPE_TV)     ? "tv "     : "",
		    (channels[i].type & VIDEO_TYPE_CAMERA) ? "camera " : "");
    }
    inputs[i].nr  = -1;
    inputs[i].str = NULL;
    grab_v4l.inputs =inputs;

    /* audios */
    if (debug)
	fprintf(stderr,"  audios  : %d\n",capability.audios);
    audios = malloc(sizeof(struct video_audio)*capability.audios);
    memset(audios,0,sizeof(struct video_audio)*capability.audios);
    for (i = 0; i < capability.audios; i++) {
	audios[i].audio = i;
	if (-1 == ioctl(fd,VIDIOCGAUDIO,&audios[i]))
	    perror("ioctl VIDIOCGCAUDIO") /* , exit(0) */ ;
	if (debug) {
	    fprintf(stderr,"    %d (%s): ",i,audios[i].name);
	    if (audios[i].flags & VIDEO_AUDIO_MUTABLE)
		fprintf(stderr,"muted=%s ",
			(audios[i].flags&VIDEO_AUDIO_MUTE) ? "yes":"no");
	    if (audios[i].flags & VIDEO_AUDIO_VOLUME)
		fprintf(stderr,"volume=%d ",audios[i].volume);
	    if (audios[i].flags & VIDEO_AUDIO_BASS)
		fprintf(stderr,"bass=%d ",audios[i].bass);
	    if (audios[i].flags & VIDEO_AUDIO_TREBLE)
		fprintf(stderr,"treble=%d ",audios[i].treble);
	    fprintf(stderr,"\n");
	}
    }
    if (audios[0].flags & VIDEO_AUDIO_VOLUME)
	grab_v4l.flags |= CAN_AUDIO_VOLUME;

    if (debug)
	fprintf(stderr,"  size    : %dx%d => %dx%d\n",
		capability.minwidth,capability.minheight,
		capability.maxwidth,capability.maxheight);

    /* tuner (more than one???) */
    if (-1 == ioctl(fd,VIDIOCGTUNER,&tuner))
	perror("ioctl VIDIOCGTUNER");
    if (debug)
	fprintf(stderr,"  tuner   : %s %lu-%lu",
		tuner.name,tuner.rangelow,tuner.rangehigh);
    for (i = 0; norms[i].str != NULL; i++) {
	if (tuner.flags & (1<<i)) {
	    if (debug)
		fprintf(stderr," %s",norms[i].str);
	} else
	    norms[i].nr = -1;
	if (tuner.mode == i && debug)
	    fprintf(stderr,"*");
    }
    if (debug)
	fprintf(stderr,"\n");
    
    /* frame buffer */
    if (-1 == ioctl(fd,VIDIOCGFBUF,&ov_fbuf))
	perror("ioctl VIDIOCGFBUF");
    if (debug)
	fprintf(stderr,"  fbuffer : base=0x%p size=%dx%d depth=%d bpl=%d\n",
		ov_fbuf.base, ov_fbuf.width, ov_fbuf.height,
		ov_fbuf.depth, ov_fbuf.bytesperline);

    /* picture parameters */
    if (-1 == ioctl(fd,VIDIOCGPICT,&pict))
	perror("ioctl VIDIOCGPICT");

    if (debug) {
	fprintf(stderr,
		"  picture : brightness=%d hue=%d colour=%d contrast=%d\n",
		pict.brightness, pict.hue, pict.colour, pict.contrast);
	fprintf(stderr,
		"  picture : whiteness=%d depth=%d palette=%s\n",
		pict.whiteness, pict.depth, device_pal[pict.palette]);
    }

    /* double-check settings */
    fprintf(stderr,"v4l: base=%p\n", ov_fbuf.base);
    if (ov_fbuf.base != base && have_dga) {
	fprintf(stderr,"v4l and dga disagree about the framebuffer address\n");
	fprintf(stderr,"you probably want to insmod the bttv module with "
		"\"vidmem=0x%03lx\"\n",(unsigned long)base >> 20);
	exit(1);
    }
    fprintf(stderr,"v4l: %d x %d x %d bit\n",
	    ov_fbuf.width, ov_fbuf.height,ov_fbuf.depth);
    if ((sw && ov_fbuf.width  != sw) ||
	(sh && ov_fbuf.height != sh)) {
	fprintf(stderr,"v4l and dga disagree about the screen size\n");
	fprintf(stderr,"RTFM, the README section about v4l-conf\n");
	exit(1);
    }
    if (format &&
	((format == VIDEO_GRAY  && ov_fbuf.depth !=  8) ||
	 (format == VIDEO_RGB08 && ov_fbuf.depth !=  8) ||
	 (format == VIDEO_RGB16 && ov_fbuf.depth != 16) ||
	 (format == VIDEO_RGB24 && ov_fbuf.depth != 24) ||
	 (format == VIDEO_RGB32 && ov_fbuf.depth != 32))) {
	fprintf(stderr,"v4l and dga disagree about the color depth\n");
	fprintf(stderr,"RTFM, the README section about v4l-conf\n");
	exit(1);
    }

    if (0 == strncmp(capability.name,"BT848",5)) {
	for (i = 0; i < sizeof(MEM_SIZE_try)/sizeof(int); i++) {
	    MEM_SIZE = MEM_SIZE_try[i];
	    map = mmap(0,MEM_SIZE*2,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
	    if (-1 != (int)map)
		break;
	}
	if ((char*)-1 == map) {
	    perror("mmap");
	} else {
	    fprintf(stderr,"v4l: mmap()'ed buffer size = 0x%x\n",MEM_SIZE);
	}
    }
    else
	map = (char*)-1;

    switch (pixmap) {
    case VIDEO_GRAY:
	gb_even.format = gb_odd.format = 0x66;  /* FIXME */
	pixmap_bytes = 1;
	break;
    case VIDEO_RGB15:
	gb_even.format = gb_odd.format = 0x33;  /* FIXME */
	pixmap_bytes = 2;
	break;
    case VIDEO_RGB16:
	gb_even.format = gb_odd.format = 0x22;  /* FIXME */
	pixmap_bytes = 2;
	break;
    case VIDEO_RGB24:
	gb_even.format = gb_odd.format = 0x11;  /* FIXME */
	pixmap_bytes = 3;
	break;
    case VIDEO_RGB32:
	gb_even.format = gb_odd.format = 0x11;  /* FIXME (RGB24 too) */
	pixmap_bytes = 4;
	break;
    }
    gb_even.frame = 0;
    gb_odd.frame  = 1;

    return 0;

err:
    if (fd != -1) {
	close(fd);
	fd = -1;
    }
    return -1;
}

static int
grab_close()
{
    if (gb_grab > gb_sync) {
	if (-1 == ioctl(fd,VIDIOCSYNC,0))
	    perror("ioctl VIDIOCSYNC");
	else
	    gb_sync++;
    }

    if ((char*)-1 != map)
	munmap(map,MEM_SIZE*2);

    if (-1 == fd)
	return 0;

    if (debug)
	fprintf(stderr, "v4l: close\n");

    close(fd);
    fd = -1;
    return 0;
}

/* ---------------------------------------------------------------------- */

static int
grab_overlay(int x, int y, int width, int height, int format,
	     struct OVERLAY_CLIP *oc, int count)
{
    int i;

    if (gb_grab > gb_sync) {
	if (-1 == ioctl(fd,VIDIOCSYNC,even ? &zero : &one))
	    perror("ioctl VIDIOCSYNC");
	else
	    gb_sync++;
    }

    if (width == 0 || height == 0) {
	if (debug)
	    fprintf(stderr,"v4l: overlay off\n");
	ioctl(fd, VIDIOCCAPTURE, &zero);
	overlay = 0;
	return 0;
    }

    ov_win.x          = x;
    ov_win.y          = y;
    ov_win.width      = width;
    ov_win.height     = height;
    ov_win.flags      = 0;

    if (capability.type & VID_TYPE_CLIPPING) {
	ov_win.clips      = ov_clips;
	ov_win.clipcount  = count;
	
	for (i = 0; i < count; i++) {
	    ov_clips[i].x      = oc[i].x1;
	    ov_clips[i].y      = oc[i].y1;
	    ov_clips[i].width  = oc[i].x2-oc[i].x1-1 /* XXX */;
	    ov_clips[i].height = oc[i].y2-oc[i].y1-1;
	    if (debug)
		fprintf(stderr,"v4l: clip=%dx%d+%d+%d\n",
			ov_clips[i].width,ov_clips[i].height,
			ov_clips[i].x,ov_clips[i].y);
	    if (ov_clips[i].x < 0 || ov_clips[i].y < 0 ||
		ov_clips[i].width < 0 || ov_clips[i].height < 0) {
		fprintf(stderr,"v4l: bug trap - overlay off\n");
		ioctl(fd, VIDIOCCAPTURE, &zero);
		overlay = 0;
		return 0;
	    }
	}
    }
    if (capability.type & VID_TYPE_CHROMAKEY) {
	ov_win.chromakey  = 0;    /* XXX */
    }
    if (-1 == ioctl(fd, VIDIOCSWIN, &ov_win))
	perror("ioctl VIDIOCSWIN");

    if (!overlay) {
	switch (format) {
	case VIDEO_GRAY:
	    pict.palette  = VIDEO_PALETTE_GREY;
	    break;
	case VIDEO_RGB08:
	    pict.palette  = VIDEO_PALETTE_HI240;
	    break;
	case VIDEO_RGB15:
	    pict.palette  = VIDEO_PALETTE_RGB555;
	    break;
	case VIDEO_RGB16:
	    pict.palette  = VIDEO_PALETTE_RGB565;
	    break;
	case VIDEO_RGB24:
	    pict.palette  = VIDEO_PALETTE_RGB24;
	    break;
	case VIDEO_RGB32:
	    pict.palette  = VIDEO_PALETTE_RGB32;
	    break;
	default:
	    TRAP("unsupported video format (overlay)");
	}
	if (-1 == ioctl(fd,VIDIOCSPICT,&pict))
	    perror("ioctl VIDIOCSPICT");
	if (-1 == ioctl(fd, VIDIOCCAPTURE, &one))
	    perror("ioctl VIDIOCCAPTURE");
	overlay = 1;
    }

    if (debug)
	fprintf(stderr,"v4l: overlay win=%dx%d+%d+%d, %d clips\n",
		width,height,x,y, count);

    return 0;
}

static void rgb_swap(char *mem, int n)
{
    char  c;
    char *p = mem;
    int   i = n;

    while (--i) {
	c = p[0]; p[0] = p[2]; p[2] = c;
	p += 3;
    }
}

void
rgb24_to_rgb32(void *d, void *s, int p)
{
    unsigned char  *dest = d;
    unsigned char  *src  = s;
    int             i    = p;

    while (i--) {
        *(dest++) = *(src++);
        *(dest++) = *(src++);
        *(dest++) = *(src++);
	*(dest++) = 0;
    }
}


static int
grab_queue(struct video_mmap *gb)
{
    if (debug)
	fprintf(stderr,"g%d",gb->frame);
    if (-1 == ioctl(fd,VIDIOCMCAPTURE,gb)) {
	perror("ioctl VIDIOCMCAPTURE");
	return -1;
    }
    if (debug)
	fprintf(stderr,"* ");
    gb_grab++;
    return 0;
}

static void
grab_wait(struct video_mmap *gb)
{
    static int vidiocsync=VIDIOCSYNC;

    if (debug)
	fprintf(stderr,"s%d",gb->frame);
retry:
    if (-1 == ioctl(fd,vidiocsync,&(gb->frame))) {
#ifdef VIDIOCSYNC_OLD
	if (errno == EINVAL && vidiocsync == VIDIOCSYNC) {
	    vidiocsync=VIDIOCSYNC_OLD;
	    goto retry;
	}
#endif
	perror("ioctl VIDIOCSYNC");
    } else
	gb_sync++;
    if (debug)
	fprintf(stderr,"* ");
}


static void*
grab_scr(void *dest, int width, int height, int single)
{
    void *buf;

    if ((char*)-1 == map)
	return NULL;
    if (!gb_even.format)
	return NULL;
    
    gb_even.width  = width;
    gb_even.height = height;
    gb_odd.width  = width;
    gb_odd.height = height;

    if (single) {
	if (gb_grab > gb_sync)
	    grab_wait(even ? &gb_even : &gb_odd);
    } else {
	if (gb_grab == gb_sync)
	    if (-1 == grab_queue(even ? &gb_even : &gb_odd))
		return NULL;
    }

    if (-1 == grab_queue(even ? &gb_odd : &gb_even))
	return NULL;

    if (gb_grab > gb_sync+1) {
	grab_wait(even ? &gb_even : &gb_odd);
	buf = even ? map : map + MEM_SIZE;
    } else {
	grab_wait(even ? &gb_odd : &gb_even);
	buf = even ? map + MEM_SIZE : map;
    }
    even = !even;

    if (pixmap_bytes == 4) {
	rgb24_to_rgb32(dest, buf, width*height);
    } else {
	memcpy(dest, buf, width*height*pixmap_bytes);
    }
    
    return dest;
}

static void*
grab_one(int width, int height)
{
    struct video_mmap gb;

    if ((char*)-1 == map)
	return NULL;
    
    if (gb_grab > gb_sync)
	grab_wait(even ? &gb_even : &gb_odd);
    
    gb.format = 0x11; /* FIXME: BT848_COLOR_FMT_RGB24 */
    gb.frame  = 0;
    gb.width  = width;
    gb.height = height;

    memset(map,0,width*height*3);
    if (-1 == grab_queue(&gb))
	return NULL;
    grab_wait(&gb);

    rgb_swap(map,width*height);
    return map;
}

/* ---------------------------------------------------------------------- */

static int
grab_tune(unsigned long freq)
{
    if (debug)
	fprintf(stderr,"v4l: freq: %.3f\n",(float)freq/16);
    if (-1 == ioctl(fd, VIDIOCSFREQ, &freq))
	perror("ioctl VIDIOCSFREQ");
    return 0;
}

static int
grab_tuned()
{
    /* (quick & dirty -- grabbing works with hsync only) */
    return grab_one(64,48) ? 1 : 0;
}

static int
grab_input(int input, int norm)
{
    if (-1 != input) {
	cur_input = input;
	if (debug)
	    fprintf(stderr,"v4l: input: %d\n",input);
	if (-1 == ioctl(fd, VIDIOCSCHAN, &input))
	    perror("ioctl VIDIOCSCHAN");
    }
    if (-1 != norm) {
	if (debug)
	    fprintf(stderr,"v4l: norm : %d\n",norm);
	tuner.mode = norm;
	if (-1 == ioctl(fd, VIDIOCSTUNER, &tuner))
	    perror("ioctl VIDIOCSTUNER");
    }
    return 0;
}

int
grab_picture(int color, int bright, int hue, int contrast)
{
    if (color != -1)
	pict.colour = color;
    if (contrast != -1)
	pict.contrast = contrast;
    if (bright != -1)
	pict.brightness = bright;
    if (hue != -1)
	pict.hue = hue;

    if (-1 == ioctl(fd,VIDIOCSPICT,&pict))
	perror("ioctl VIDIOCSPICT");

    return 0;
}

int
grab_audio(int mute, int volume, int *mode)
{
    if (mute != -1) {
	if (mute)
	    audios[0].flags |= VIDEO_AUDIO_MUTE;
	else
	    audios[0].flags &= ~VIDEO_AUDIO_MUTE;
    }
    if (volume != -1)
	audios[0].volume = volume;

    audios[0].audio = cur_input;
    audios[0].mode = mode ? *mode : 0;
    if (-1 == ioctl(fd,VIDIOCSAUDIO,&audios[0]))
	perror("ioctl VIDIOCSAUDIO");

    if (mode) {
	if (-1 == ioctl(fd,VIDIOCGAUDIO,&audios[0]))
	    perror("ioctl VIDIOCGAUDIO");
	*mode = audios[0].mode;
    }
    return 0;
}

Privacy Policy