aboutsummaryrefslogtreecommitdiffstats
path: root/grab-one.c
blob: 5d62e0b736c79497f72f317a5468df96a0b07c7f (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
#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 <asm/types.h>		/* XXX glibc */

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

#define DEVNAME "/dev/video"

#ifndef BT848_COLOR_FMT_RGB32
# define BT848_COLOR_FMT_RGB32       0x00
# define BT848_COLOR_FMT_RGB24       0x11
# define BT848_COLOR_FMT_RGB16       0x22
# define BT848_COLOR_FMT_RGB15       0x33
# define BT848_COLOR_FMT_YUY2        0x44
# define BT848_COLOR_FMT_BtYUV       0x55
# define BT848_COLOR_FMT_Y8          0x66
# define BT848_COLOR_FMT_RGB8        0x77
# define BT848_COLOR_FMT_YCrCb422    0x88
# define BT848_COLOR_FMT_YCrCb411    0x99
# define BT848_COLOR_FMT_RAW         0xee
#endif

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

#define POST_NOTHING     0
#define POST_RGB_SWAP    1
#define POST_UNPACK422   2
#define POST_UNPACK411   3
#define POST_RAW         4

struct GRAB_FORMAT {
    char *name;
    int  bt848;
    char ptype;
    int  mul,div;
    int  post;
};

static struct GRAB_FORMAT formats[] = {
    { "rgb",  BT848_COLOR_FMT_RGB24,     '6',   3, 1,  POST_RGB_SWAP  },
    { "gray", BT848_COLOR_FMT_Y8,        '5',   1, 1,  POST_NOTHING   },
    { "411",  BT848_COLOR_FMT_YUY2,      '5',   3, 2,  POST_UNPACK411 },
    { "422",  BT848_COLOR_FMT_YUY2,      '5',   2, 1,  POST_UNPACK422 },
    { "raw",  BT848_COLOR_FMT_RAW,       '5',   1, 1,  POST_RAW       },
    /* end of table */
    { NULL,   0,                        '\0',   0, 0,  0              },
};

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

static struct video_capability  capability;
static struct video_channel     channel;
static struct video_audio       audio;
static struct video_tuner       tuner;
static struct video_picture     pict;

static struct video_mmap        gb;
static char *map = NULL;

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

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;
    }
}

static void
packed422_to_planar422(char *dest, char *src, int width, int height)
{
    int i;
    char *s, *y,*u,*v;

    i = (width * height)/2;
    s = src;
    y = dest;
    u = y + width * height;
    v = u + width * height / 2;
    
    while (--i) {
	*(y++) = *(src++);
	*(u++) = *(src++);
	*(y++) = *(src++);
        *(v++) = *(src++);
    }
}

static void
packed422_to_planar411(char *dest, char *src, int width, int height)
{
    int  a,b;
    char *s, *y,*u,*v;

    s = src;
    y = dest;
    u = y + width * height;
    v = u + width * height / 4;

    for (a = height; a > 0; a -= 2) {
	for (b = width; b > 0; b -= 2) {
	    
	    *(y++) = *(src++);
	    *(u++) = *(src++);
	    *(y++) = *(src++);
	    *(v++) = *(src++);
	}
	for (b = width; b > 0; b -= 2) {
	    *(y++) = *(src++);
	    src++;
	    *(y++) = *(src++);
	    src++;
	}
    }
}

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

void
usage()
{
    fprintf(stderr,
	    "usage: grab-one [ options] > file\n"
	    "\n"
	    "grab-one grabs one image from a bt848 card and "
	    "dumps it to stdout\n"
	    "\n"
	    "options:\n"
	    "  -f format   specify output format\n"
	    "  -s size     specify size (default is full-size)\n"
	    "  -r          raw data (default is ppm/pgm)\n"
	    "\n"
	    "formats:\n"
	    "  rgb (default), gray, 411, 422\n"
	    "\n"
	    "examples:\n"
	    "  grab-one > image.ppm\n"
	    "  grab-one -f gray -s 320x240 > no-color.pgm\n"
	    "  grab-one -f 411 -r | display -size 768x576 yuv:-\n"
	    "\n"
	    "--\n"
	    "(c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>\n");
}

int
main(int argc, char **argv)
{
    int  format = 0, raw = 0;
    int  i,fd,width = 0, height = 0;
    char c,header[64],*buf;

    /* parse options */
    for (;;) {
	if (-1 == (c = getopt(argc, argv, "hrf:s:")))
	    break;
	switch (c) {
	case 'f':
	    for (i = 0; formats[i].name != NULL; i++)
		if (0 == strcasecmp(formats[i].name,optarg)) {
		    format = i;
		    break;
		}
	    if (formats[i].name == NULL) {
		fprintf(stderr,"unknown format %s (available:",optarg);
		for (i = 0; formats[i].name != NULL; i++)
		    fprintf(stderr," %s",formats[i].name);
		fprintf(stderr,")\n");
		exit(1);
	    }
	    break;
	case 's':
	    if (2 != sscanf(optarg,"%dx%d",&width,&height))
		width = height = 0;
	    break;
	case 'r':
	    raw = 1;
	    break;
	case 'h':
	default:
	    usage();
	    exit(1);
	}
    }

    /* open */
    if (-1 == (fd = open(DEVNAME,O_RDWR))) {
	perror("open " DEVNAME);
	exit(1);
    }

    /* get settings */
    if (-1 == ioctl(fd,VIDIOCGCAP,&capability)) {
	perror("ioctl VIDIOCGCAP");
	exit(1);
    }
    if (-1 == ioctl(fd,VIDIOCGCHAN,&channel))
	perror("ioctl VIDIOCGCHAN");
    if (-1 == ioctl(fd,VIDIOCGAUDIO,&audio))
	perror("ioctl VIDIOCGAUDIO");
    if (-1 == ioctl(fd,VIDIOCGTUNER,&tuner))
	perror("ioctl VIDIOCGTUNER");
    if (-1 == ioctl(fd,VIDIOCGPICT,&pict))
	perror("ioctl VIDIOCGPICT");

    /* mmap() buffer */
    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) {
	fprintf(stderr,"no mmap support\n");
	exit(1);
    } else {
	fprintf(stderr,"mmap()'ed buffer size = 0x%x\n",MEM_SIZE);
    }

    if (width == 0 || height == 0) {
	switch (tuner.mode) {
	case VIDEO_MODE_NTSC:
	    width  = 640;
	    height = 480;
	    break;
	default:
	    width  = 768;
	    height = 576;
	    break;
	}
    }

    gb.format = formats[format].bt848;
    gb.frame  = 0;
    gb.width  = width;
    gb.height = height;

    if (-1 == ioctl(fd,VIDIOCMCAPTURE,&gb)) {
	perror("ioctl VIDIOCMCAPTURE");
	exit(1);
    }
    if (-1 == ioctl(fd,VIDIOCSYNC,0)) {
	perror("ioctl VIDIOCSYNC");
	exit(1);
    }

    switch (formats[format].post) {
    case POST_RGB_SWAP:
	rgb_swap(map,width*height);
	buf = map;
	break;
    case POST_UNPACK422:
	buf = malloc(MEM_SIZE);
	packed422_to_planar422(buf,map,width,height);
	break;
    case POST_UNPACK411:
	buf = malloc(MEM_SIZE);
	packed422_to_planar411(buf,map,width,height);
	break;
    case POST_RAW:
	width  = 2270;
	height = 288;
	buf = map;
	break;
    default:
	buf = map;
	break;
    }

    sprintf(header,"P%c\n%d %d\n255\n",formats[format].ptype,width,height);
    if (!raw)
	write(1,header,strlen(header));
    write(1,buf,width*height*formats[format].mul/formats[format].div);

    /* done */
    close(fd);
    return 0;
}

Privacy Policy