File: grabber-plx.cc

package info (click to toggle)
vic 2.8ucl4-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,864 kB
  • ctags: 9,033
  • sloc: ansic: 56,989; cpp: 44,560; tcl: 5,550; sh: 1,382; perl: 1,329; makefile: 357
file content (601 lines) | stat: -rw-r--r-- 15,740 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
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
/*
 * Copyright 1995 Apple Computer, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * APPLE COMPUTER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Apple Computer shall not be
 * used in advertising or otherwise to promote the sale, use or other dealings
 * in this Software without prior written authorization from Apple Computer.
 */
/*
 * Copyright (c) 1995 The Regents of the University of California.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 * 	This product includes software developed by the Network Research
 * 	Group at Lawrence Berkeley National Laboratory.
 * 4. Neither the name of the University nor of the Laboratory may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
static char rcsid[] =
    "@(#) $Header: /cs/research/mice/starship/src/local/CVS_repository/vic/grabber-plx.cc,v 1.1.1.1 1998/02/18 18:03:29 ucacsva Exp $ (LBL)";
#endif

#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/param.h>

#ifdef USE_SHM
#include <sys/ipc.h>
#include <sys/shm.h>
#if !defined(__svr4__) && !defined(__hpux)
extern "C" {
#include <sys/shm.h>
int shmget(key_t, int, int);
char *shmat(int, char*, int);
int shmdt(char*);
int shmctl(int, int, struct shm_ds *);
}
#endif
#endif

#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifdef USE_SHM
extern "C" {
#include <X11/extensions/XShm.h>
}
#endif

/*XXX*/
#ifdef __svr4__
extern "C" {
#include <XPlxExt.h>
}
#else
#include "plxfuncs.h"
#endif

#include "grabber.h"
#include "module.h"
#include "vw.h"
#include "Tcl.h"
#include "device-input.h"
#include "transmitter.h"
#include "rgb-converter.h"

#define NTSC_WIDTH 640
#define NTSC_HEIGHT 480
#define PAL_WIDTH 768
#define PAL_HEIGHT 576

class PlxWindow : public BareWindow {
public:
	PlxWindow(const char* name, XVisualInfo*, int port);
	virtual ~PlxWindow();
	void capture(u_int8_t* frm);
	void setsize(int w, int h);

	void port(const char *s);
	inline void hue(int v) { hue_ = v; setctrl(); }
	inline void saturation(int v) { saturation_ = v; setctrl(); }
	inline void brightness(int v) { brightness_ = v; setctrl(); }
	inline void contrast(int v) { contrast_ = v; setctrl(); }

	void freeze_frame(sigset_t* ss);
	void release_frame(sigset_t* ss);
	virtual void redraw() { need_refresh_ = 1; }

	inline int basewidth() { return (format_width_); }
	inline int baseheight() { return (format_height_); }

	void setq(int q);
	inline GC gc() const { return (gc_); }
protected:
	GC gc_;
	int need_refresh_;
	u_int format_width_, format_height_, format_offset_;
	int hue_, saturation_, brightness_, contrast_;
	int port_;
	int format_;
	void setctrl();
};

class PlxGrabber : public Grabber {
protected:
	PlxGrabber();
	virtual ~PlxGrabber();
public:
	virtual int command(int argc, const char *const *argv);
	void init();
protected:
	int lookup_visual();
	virtual void setsize() = 0;
	int decimate_;
	PlxWindow* capwin_;
	XVisualInfo vinfo_;
};

class Plx422Grabber : public PlxGrabber {
public:
	Plx422Grabber();
	~Plx422Grabber();
	virtual int grab();
	virtual void setsize();
protected:
	void capture();
	StandardVideoImage* image_;
	RGB_Converter* converter_;
};

class Plx411Grabber : public Plx422Grabber {
public:
	virtual void setsize();
};

class PlxCIFGrabber : public Plx422Grabber {
public:
	virtual void setsize();
};

class PlxJpegGrabber : public PlxGrabber {
public:
	PlxJpegGrabber();
	~PlxJpegGrabber();
	virtual int grab();
	virtual void setsize();
	virtual int command(int argc, const char *const *argv);
protected:
	int capture(u_int8_t* bp);
	int q_;
	XPlxCImage *cimage_;
};

static class PlxDevice : public InputDevice {
 public:
	PlxDevice(const char*);
	virtual int command(int argc, const char*const* argv);
} plx_device("parallax");

PlxDevice::PlxDevice(const char *s) : InputDevice(s) 
{
	attributes_ = "\
format { 422 411 jpeg } \
size { small large cif } \
port { Input-1 Input-2 }";
}

int PlxDevice::command(int argc, const char*const* argv)
{
	Tcl& tcl = Tcl::instance();
	if (argc == 3 && strcmp(argv[1], "open") == 0) {
		const char* fmt = argv[2];
		TclObject* o = 0;
		if (strcmp(fmt, "jpeg") == 0)
			o = new PlxJpegGrabber;
		else if (strcmp(fmt, "422") == 0)
			o = new Plx422Grabber;
		else if (strcmp(fmt, "411") == 0)
			o = new Plx411Grabber;
		else if (strcmp(fmt, "cif") == 0)
			o = new PlxCIFGrabber;
		if (o != 0)
			Tcl::instance().result(o->name());
		return (TCL_OK);
	}
	return (InputDevice::command(argc, argv));
}

PlxWindow::PlxWindow(const char* name, XVisualInfo* vinfo, int port)
	: BareWindow(name, vinfo)
{
	gc_ = Tk_GetGC(tk_, 0, 0);
	
	/*
	 * query the server to get hardware/signal information.
	 */
	Tk_MakeWindowExist(tk_);
	Window window = Tk_WindowId(tk_);
	plx_IO* plxio = XPlxQueryConfig(dpy_, window, gc_);
	if (plxio->hardware != PLX_XVIDEO_24)
		/* shouldn't happen. */
		abort();

	/* go through the list of signals and find an active signal */

	plx_IO_list *il;
	/* find a pointer to channel one input */
	for (il = plxio->inputs; il->channel != port; il = il->next_IO)
		;

	plx_signal* sig = 0;
	plx_signal_list *sl;
	for (sl = il->signal_list; sl != 0; sl = sl->next_signal) {
		/*
		 * do an input select
		 * we are only using input 0 so it, and PLX_COMP are hard coded
		 * we also know that all the XVideo hardware can do is PLX_RGB24
		 */
		format_ = sl->signal->standard;
		XPlxVideoInputSelect(dpy_, window, gc_, port,
				     format_, PLX_COMP, PLX_RGB24);

		/* now do a query video to find out if there is sync */
		sig = XPlxQueryVideo(dpy_, window, gc_);
		if (sig->sync_ok)
			break;
	}
	if (sig == 0) {
		/*XXX don't exit!*/
		fprintf(stderr, "no video connected to input 0\n");
		exit(1);
	}
	port_ = port;
	/* maintain 4:3 aspect ratio */
	format_width_ = sig->w;
	format_height_ = (format_width_ * 3) / 4;
	format_offset_ = sig->h - format_height_ + 1;

	XPlxVideoTag(dpy_, window, gc_, PLX_VIDEO);
	XPlxVideoValueQuery(dpy_, window, gc_,
			    &saturation_, &contrast_, &hue_, &brightness_);
}

PlxWindow::~PlxWindow()
{
	Tk_FreeGC(dpy_, gc_);
}

void PlxWindow::setsize(int w, int h)
{
	BareWindow::setsize(w, h);
	/* Start the video display */
	Tcl::instance().evalc("update idletasks");/*XXX*/
	XPlxVideoSqueezeLive(dpy_, Tk_WindowId(tk_), gc_,
			     0, format_offset_, format_width_, format_height_,
			     0, 0, width_, height_);
}

void PlxWindow::setctrl()
{
	if (tk_ != 0)
		XPlxVideoValueLoad(dpy_, Tk_WindowId(tk_), gc_,
				   saturation_, contrast_, hue_, brightness_);
}

void PlxWindow::port(const char* s)
{
	port_ = (strcasecmp(s, "input-1") == 0) ? PLX_INPUT_0 : PLX_INPUT_1;
	/*
	 * XXX This won't work if we have different signal types
	 * on the different ports.
	 */
	XPlxVideoInputSelect(dpy_, Tk_WindowId(tk_), gc_,
			     port_, format_, PLX_COMP, PLX_RGB24);
}

void PlxWindow::freeze_frame(sigset_t* ss)
{
	sigset_t fullset;
	sigfillset(&fullset);
	sigprocmask(SIG_SETMASK, &fullset, ss);
#if GRAB_SERVER
	XGrabServer(dpy_);
#endif
#ifdef notdef
	/*XXX need toplevel window */
	XRaiseWindow(dpy_, Tk_WindowId(tk_));
#endif
	if (need_refresh_) {
		XPlxVideoTag(dpy_, Tk_WindowId(tk_), gc_, PLX_VIDEO);
		XPlxVideoSqueezeLive(dpy_, Tk_WindowId(tk_), gc_,
				     0, format_offset_,
				     format_width_, format_height_,
				     0, 0, width_, height_);
	}
#ifdef notdef
	XSync(dpy_, 0);
	XPlxVideoSqueezeStill(dpy_, Tk_WindowId(tk_), gc_,
		0, format_offset_, format_width_, format_height_,
		0, 0, width_, height_);
#endif
}

void PlxWindow::release_frame(sigset_t* ss)
{
	XUngrabServer(dpy_);
	sync();
	(void)sigprocmask(SIG_SETMASK, ss, 0);
	if (need_refresh_) {
		XPlxVideoTag(dpy_, Tk_WindowId(tk_), gc_, PLX_VIDEO_OVR);
		need_refresh_ = 0;
	}
}

extern int quality_to_qfactor(int);

void PlxWindow::setq(int q)
{
	char *table;
	int size = MakeQTables(quality_to_qfactor(q), (unsigned char**)&table);
	XPlxPutTable(dpy_, Tk_WindowId(tk_), gc_,
#ifdef __hpux
		     (short *)
#endif
		     table, size, 0 /* compress */);
	free(table);
}

PlxGrabber::PlxGrabber() : capwin_(0), decimate_(0)
{
	if (lookup_visual() < 0)
		status_ = -1;
}

PlxGrabber::~PlxGrabber()
{
}

int PlxGrabber::lookup_visual()
{
	Tk_Window tk = Tcl::instance().tkmain();
	Display* dpy = Tk_Display(tk);
	int majop, eventbase, errbase;
	if (XQueryExtension(dpy, "ParallaxVideo", &majop,
			    &eventbase, &errbase) &&
	    (XMatchVisualInfo(Tk_Display(tk), Tk_ScreenNumber(tk),
			      24, TrueColor, &vinfo_) ||
	     XMatchVisualInfo(Tk_Display(tk), Tk_ScreenNumber(tk),
			      32, TrueColor, &vinfo_))) {
		return (0);
	}
	return (-1);
}

int PlxGrabber::command(int argc, const char * const*argv)
{
	Tcl &tcl = Tcl::instance();
	if (argc == 2) {
		if (strcmp(argv[1], "need-capwin") == 0) {
			tcl.result("1");
			return (TCL_OK);
		}
	} else if (argc == 3) {
		if (strcmp(argv[1], "decimate") == 0) {
			int d = atoi(argv[2]);
			if (d <= 0) {
				Tcl_AppendResult(tcl.interp(),
					"%s: divide by zero",
					argv[0], 0);
				return TCL_ERROR;
			}
			if (d != decimate_) {
				decimate_ = d;
				setsize();
			}
			return (TCL_OK);
		} else if (strcmp(argv[1], "port") == 0) {
			capwin_->port(argv[2]);
			return (TCL_OK);
		} else if (strcmp(argv[1], "contrast") == 0) {
			capwin_->contrast(atoi(argv[2]));
			return (TCL_OK);
		} else if (strcmp(argv[1], "brightness") == 0) {
			capwin_->brightness(atoi(argv[2]));
			return (TCL_OK);
		} else if (strcmp(argv[1], "hue") == 0) {
			capwin_->hue(atoi(argv[2]));
			return (TCL_OK);
		} else if (strcmp(argv[1], "saturation") == 0) {
			capwin_->saturation(atoi(argv[2]));
			return (TCL_OK);
		} else if (strcmp(argv[1], "create-capwin") == 0) {
			/*XXX port*/
			capwin_ = new PlxWindow(argv[2], &vinfo_, PLX_INPUT_0);
			tcl.result(capwin_->name());
			return (TCL_OK);
		}
	}
	return (Grabber::command(argc, argv));
}

Plx422Grabber::Plx422Grabber() : image_(0), converter_(0)
{
}

Plx422Grabber::~Plx422Grabber()
{
	delete image_;
}

void Plx422Grabber::capture()
{
	/*
	 * Grab the frame.
	 */
	sigset_t ss;
	capwin_->freeze_frame(&ss);
	Tk_Window tk = capwin_->tkwin();
	/*
	 * The parallax X server doesn't seem to support xshmgetimage.
	 */
	XImage* image = image_->ximage();
#ifdef USE_SHM
	if (image->obdata != 0)
		XShmGetImage(Tk_Display(tk), Tk_WindowId(tk), image,
			     0, 0, AllPlanes);
	else
#endif
		XGetSubImage(Tk_Display(tk), Tk_WindowId(tk),
			     0, 0, image->width, image->height,
			     AllPlanes, ZPixmap, image, 0, 0);
	capwin_->release_frame(&ss);
	converter_->convert((u_int8_t*)image->data,
			    image->width, image->height, frame_);
}

void Plx422Grabber::setsize()
{
	int w = capwin_->basewidth() / decimate_;
	int h = capwin_->baseheight() / decimate_;
	capwin_->setsize(w, h);
	converter_ = RGB_Converter_422::instance();
	image_ = StandardVideoImage::allocate(capwin_->tkwin(), w, h);
	set_size_422(w, h);
	allocref();
}

int Plx422Grabber::grab()
{
	capture();
	suppress(frame_);
	saveblks(frame_);
	YuvFrame f(media_ts(), frame_, crvec_, outw_, outh_);
	return (target_->consume(&f));
}

void Plx411Grabber::setsize()
{
	int w = capwin_->basewidth() / decimate_;
	int h = capwin_->baseheight() / decimate_;
	capwin_->setsize(w, h);
	converter_ = RGB_Converter_411::instance();
	image_ = StandardVideoImage::allocate(capwin_->tkwin(), w, h);
	set_size_411(w, h);
	allocref();
}

void PlxCIFGrabber::setsize()
{
	int w = 2 * 352 / decimate_;
	int h = 2 * 288 / decimate_;
	capwin_->setsize(w, h);
	converter_ = RGB_Converter_411::instance();
	image_ = StandardVideoImage::allocate(capwin_->tkwin(), w, h);
	set_size_411(w, h);
	allocref();
}

PlxJpegGrabber::PlxJpegGrabber() : q_(50), cimage_(0)
{
}

PlxJpegGrabber::~PlxJpegGrabber()
{
	/*XXX free cimage_ */
}

void PlxJpegGrabber::setsize()
{
	int w = capwin_->basewidth() / decimate_;
	int h = capwin_->baseheight() / decimate_;
	capwin_->setsize(w, h);
	inw_ = outw_ = w;
	inh_ = outh_ = h;
#ifdef notdef
	Tk_Window tk = capwin_->tkwin();
	if (cimage_ != 0)
		(void)XPlxDestroyCImage(cimage_);
	cimage_ = XPlxCreateCImage(Tk_Display(tk), DATA, SIZE, w, h);
#endif
}

int PlxJpegGrabber::capture(u_int8_t* bp)
{
	sigset_t ss;
	capwin_->freeze_frame(&ss);
	XPlxCImage* p;
	Tk_Window tk = capwin_->tkwin();
	int w = capwin_->width();
	int h = capwin_->height();
#ifdef USE_SHM_notyet
	p = cimage_;
	XPlxShmGetCImage(Tk_Display(tk), Tk_WindowId(tk),
			 capwin_->gc(), cimage_, 0, 0, w, h);
#else
	p = XPlxGetCImage(Tk_Display(tk), Tk_WindowId(tk),
			  capwin_->gc(), 0, 0, w, h, w, h);
#endif
	capwin_->release_frame(&ss);
	/*XXX*/
	memcpy(bp, p->data, p->size);
	int size = p->size;

#ifndef USE_SHM_notyet
	XPlxDestroyCImage(p);
#endif

	return (size);
}

int PlxJpegGrabber::grab()
{
	/*
	 * Make sure the previous frame has been completely transmitted
	 * before we overwrite the buffer.
	 */
	tx_->flush();
	/*XXX*/
	static u_int32_t wrk[80*1024 / 4];
	int len = capture((u_int8_t*)wrk);

	JpegFrame f(media_ts(), (u_int8_t*)wrk, len, q_, 0, outw_, outh_);
	return (target_->consume(&f));
}

int PlxJpegGrabber::command(int argc, const char* const *argv)
{
	if (argc == 3) {
		if (strcmp(argv[1], "q") == 0) {
			/* assume value is in range */
			q_ = atoi(argv[2]);
			capwin_->setq(q_);
			return (TCL_OK);
		}
	}
	return (PlxGrabber::command(argc, argv));
}