File: grabber-xv.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 (433 lines) | stat: -rw-r--r-- 11,457 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
/*
 * Copyright (c) 1993-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.
 *
 * Derived from code written by Eric Anderson, UCSD Computer Systems Lab,
 * 4/92 and hacked on by Jon Kay, Vach Kompella, Kevin Fall.
 * Ported to vic 1/94, Steven McCanne (mccanne@ee.lbl.gov) March 1993.
 */

/*
 * (c) Copyright 1994 HEWLETT-PACKARD COMPANY
 * 
 * To anyone who acknowledges that this file is provided 
 * "AS IS" without any express or implied warranty:
 * permission to use, copy, modify, and distribute this 
 * file for any purpose is hereby granted without fee, 
 * provided that the above copyright notice and this 
 * notice appears in all copies, and that the name of 
 * Hewlett-Packard Company not be used in advertising or 
 * publicity pertaining to distribution of the software 
 * without specific, written prior permission.  Hewlett-
 * Packard Company makes no representations about the 
 * suitability of this software for any purpose.
 *
 */

/*
 * Apply this patch to the extensions/xv/lib/Xv.c file:
*** Xv.c~	Thu Mar 31 11:13:47 1994
--- Xv.c	Fri May 27 11:38:33 1994
***************
*** 192,197 ****
--- 192,198 ----
  	  return(XvBadAlloc);
  	}
        (void)strncpy(name, u.string, size);
+       name[size] = '\0';
        pa->name = name;
  
        u.buffer += (size + 3) & ~3;
***************
*** 351,356 ****
--- 352,358 ----
  	  return(XvBadAlloc);
  	}
        strncpy(name, u.string, size);
+       name[size] = '\0';
        pe->name = name;
        pe++;

 * It fixes a problem with strings NOT being null terminated.
 */

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

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

#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

extern "C" {
#ifdef _AIX
#include <Xvlib.h>
#else
#include <X11/extensions/Xvlib.h>
#endif
}

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

#define NTSC_WIDTH 640
#define NTSC_HEIGHT 480
#define PAL_WIDTH 768
#define PAL_HEIGHT 576
#define CIF_WIDTH 352
#define CIF_HEIGHT 288

class XvWindow : public CaptureWindow {
public:
	XvWindow(const char* name, XVisualInfo*, XvPortID);
	virtual ~XvWindow();
	virtual void setsize(int w, int h);
protected:
	XvPortID grabID_;
};

class XvGrabber : public Grabber {
 public:
	XvGrabber();
	virtual ~XvGrabber();
	virtual int command(int argc, const char*const* argv);
 protected:
	virtual void setsize();
	void setattr(Atom attr, int value);
	void setport(const char *port);
	int getattr(Atom attr);
	int getgrabber();
	virtual int grab();
	XvEncodingInfo *lookup_encoding_by_id(XvEncodingID id);    
	XvEncodingInfo *lookup_encoding_by_name(char *name);

	Visual* visual_;
	XvPortID grabID_;
	XvWindow* capwin_;
	
	char grabber_name[80];
	Atom XAbrightness, XAcontrast, XAhue, XAsaturation, XAencoding;
	u_int nencodings;
	XvEncodingInfo *encoding;

	int decimate_;
	XvEncodingID  encodingid_;
	XVisualInfo vinfo_;
};

class XvCIFGrabber : public XvGrabber {
protected:
	virtual void setsize();
};

class Xv411Grabber : public XvGrabber {
protected:
	virtual void setsize();
};

class XvDevice : public InputDevice {
 public:
	XvDevice(const char* s);
	int command(int argc, const char*const* argv);
};

static XvDevice xv_device("xv");

XvDevice::XvDevice(const char* s) : InputDevice(s)
{
	/*XXX port & 411*/
	attributes_ = "format { 422 411 } size { small large cif }";
}

int XvDevice::command(int argc, const char*const* argv)
{
#if 0
	printf("XvDevice::command: %d: ", argc);
	for (int i=0; i<argc; i++)
		printf("%s ", argv[i]);
	printf("\n");
#endif
	Tcl& tcl = Tcl::instance();
	if (argc == 3) {
		if (strcmp(argv[1], "open") == 0) {
			if (strcmp(argv[2], "422") == 0) {
				TclObject* o = new XvGrabber;
				tcl.result(o->name());
			} else if (strcmp(argv[2], "cif") == 0) {
				TclObject* o = new XvCIFGrabber;
				tcl.result(o->name());
			}
			return (TCL_OK);
		}
	}
	return (InputDevice::command(argc, argv));
}

XvWindow::XvWindow(const char* name, XVisualInfo* vinfo, XvPortID grabID)
	: CaptureWindow(name, vinfo), grabID_(grabID)
{
	/*
	 * Xv doesn't have a way to query the signal type, and
	 * hence the input video geometry.
	 * XXX should make this configurable.
	 */
#define NTSC_WIDTH 640	
#define NTSC_HEIGHT 480
	base_width_ = NTSC_WIDTH;
	base_height_ = NTSC_HEIGHT;
}

XvWindow::~XvWindow()
{
	if (image_ != 0)
		XvStopVideo(dpy_, grabID_, Tk_WindowId(tk_));
}

void XvWindow::setsize(int w, int h)
{
	CaptureWindow::setsize(w, h);
	/* Start the video display */
	Tk_MakeWindowExist(tk_);
	XvPutVideo(dpy_, grabID_, Tk_WindowId(tk_), gc_,
		   0, 0, base_width_, base_height_,
		   0, 0, width_, height_);
}

XvGrabber::XvGrabber() : capwin_(0), decimate_(0)
{
	Tcl& tcl = Tcl::instance();

	if (getgrabber() < 0) {
		status_ = -1;
		return;
	}
	Display* dpy = Tk_Display(Tcl::instance().tkmain());
	if (XvGrabPort(dpy, grabID_, CurrentTime) != Success) {
		tcl.result("Unable to reserve grabber.");
		fprintf(stderr, "Unable to grab port\n");
		status_ = -1;
		return;
	}
}

XvGrabber::~XvGrabber()
{
	if (status_ >= 0) {
		Display* dpy = Tk_Display(Tcl::instance().tkmain());
		XvUngrabPort(dpy, grabID_, CurrentTime);
	}
}

int XvGrabber::command(int argc, const char*const* argv)
{
	Tcl& tcl = Tcl::instance();
	if (argc == 3) {
		if (strcmp(argv[1], "decimate") == 0) {
			int dec = atoi(argv[2]);
			Tcl& tcl = Tcl::instance();
			if (dec <= 0) {
				tcl.resultf("%s: divide by zero", argv[0]);
				return (TCL_ERROR);
			}
			if (dec != decimate_) {
				decimate_ = dec;
				setsize();
			}
			return (TCL_OK);
		} else if (strcmp(argv[1], "port") == 0) {
			setport(argv[2]);
			return (TCL_OK);
		} else if (strcmp(argv[1], "contrast") == 0) {
			setattr(XAcontrast, atoi(argv[2]));
			return (TCL_OK);
		} else if (strcmp(argv[1], "brightness") == 0) {
			setattr(XAbrightness, atoi(argv[2]));
			return (TCL_OK);
		} else if (strcmp(argv[1], "hue") == 0) {
			setattr(XAhue, atoi(argv[2]));
			return (TCL_OK);
		} else if (strcmp(argv[1], "saturation") == 0) {
			setattr(XAsaturation, atoi(argv[2]));
			return (TCL_OK);
		} else if (strcmp(argv[1], "create-capwin") == 0) {
			capwin_ = new XvWindow(argv[2], &vinfo_, grabID_);
			tcl.result(capwin_->name());
			return (TCL_OK);
		}
	} else if (argc == 2) {
		if (strcmp(argv[1], "need-capwin") == 0) {
			tcl.result("1");
			return (TCL_OK);
		}
	}
	return (Grabber::command(argc, argv));
}

XvEncodingInfo *XvGrabber::lookup_encoding_by_id(XvEncodingID id)
{
	int i;
	for (i=0; i<nencodings; i++) {
		if (id == encoding[i].encoding_id)
			return &encoding[i];
	}
	return NULL;
}

XvEncodingInfo *XvGrabber::lookup_encoding_by_name(char *name)
{
	for (int i=0; i<nencodings; i++) {
		if (strncmp(name, encoding[i].name, strlen(name)) == 0)
			return &encoding[i];
	}
	return NULL;
}

void XvGrabber::setport(const char *port)
{
	printf("setport: %s\n", port);
}

void XvGrabber::setattr(Atom attr, int value)
{
	Display* dpy = Tk_Display(Tcl::instance().tkmain());
	XvSetPortAttribute(dpy, grabID_, attr, value);
}

int XvGrabber::getattr(Atom attr)
{
	int v;
	Display* dpy = Tk_Display(Tcl::instance().tkmain());
	XvGetPortAttribute(dpy, grabID_, attr, &v);
	return(v);
}

void XvGrabber::setsize()
{
	int w = capwin_->basewidth() / decimate_;
	int h = capwin_->baseheight() / decimate_;
	capwin_->setsize(w, h);
	capwin_->converter(RGB_Converter_422::instance());
	set_size_422(capwin_->width(), capwin_->height());
	allocref();
}

void XvCIFGrabber::setsize()
{
	int w = 2 * 352 / decimate_;
	int h = 2 * 288 / decimate_;
	capwin_->setsize(w, h);
	capwin_->converter(RGB_Converter_411::instance());
	set_size_411(w, h);
	allocref();
}

void Xv411Grabber::setsize()
{
	int w = capwin_->basewidth() / decimate_;
	int h = capwin_->baseheight() / decimate_;
	capwin_->setsize(w, h);
	capwin_->converter(RGB_Converter_411::instance());
	set_size_411(w, h);
	allocref();
}

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

int XvGrabber::getgrabber()
{
	Tk_Window tk = Tcl::instance().tkmain();
	Display* dpy = Tk_Display(tk);

	int majop, eventbase, errbase;
	if (XQueryExtension(dpy, "XVideo", &majop,
			    &eventbase, &errbase) == False)
		return (-1);

	Window root = DefaultRootWindow(dpy);
	u_int ngrabbers;
	XvAdaptorInfo* grabbers;
	if (XvQueryAdaptors(dpy, root, &ngrabbers, &grabbers) != Success ||
	    ngrabbers == 0)
		return (-1);
	if (ngrabbers > 1)
		fprintf(stderr, "vic: warning: more than one frame grabber\n");
		
	strncpy(grabber_name, grabbers[0].name, sizeof(grabber_name));
	grabID_ = grabbers[0].base_id;

	XvFreeAdaptorInfo(grabbers);

	XvQueryEncodings(dpy, grabID_, &nencodings, &encoding);
#if 0
	printf("Encodings(%d): ", nencodings);
	for (int i=0; i<nencodings; i++)
	  printf("%s %d %d\n", encoding[i].name,
		 encoding[i].width, encoding[i].height);
	printf("\n");
#endif

	XAbrightness = XInternAtom(dpy, "XV_BRIGHTNESS", False);
	XAcontrast = XInternAtom(dpy, "XV_CONTRAST", False);
	XAhue = XInternAtom(dpy, "XV_HUE", False);
	XAsaturation = XInternAtom(dpy, "XV_SATURATION", False);
	XAencoding = XInternAtom(dpy, "XV_ENCODING", False);

	XvGetPortAttribute(dpy, grabID_, XAencoding, (int *)&encodingid_);

	if (!XMatchVisualInfo(dpy, Tk_ScreenNumber(tk), 24,
			      TrueColor, &vinfo_))
		return (-1);

	return (0);
}