blob: 75f20238e412499c590a4dd1e6c71459efbb385a [file] [log] [blame]
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001/**********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Sjoerd Mullendered59d201993-01-06 13:36:38 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Sjoerd Mullendered59d201993-01-06 13:36:38 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Sjoerd Mullendered59d201993-01-06 13:36:38 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Sjoerd Mullendered59d201993-01-06 13:36:38 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Sjoerd Mullendered59d201993-01-06 13:36:38 +000029
30******************************************************************/
31
32/* SV module -- interface to the Indigo video board */
33
Guido van Rossumc88c9cb1996-12-17 20:43:55 +000034/* WARNING! This module is for hardware that we don't have any more,
35 so it hasn't been tested. It has been converted to the new coding
36 style, and it is possible that this conversion has broken something
37 -- user beware! */
38
Sjoerd Mullendered59d201993-01-06 13:36:38 +000039#include <sys/time.h>
40#include <svideo.h>
Barry Warsawf630f6b1996-12-13 01:24:29 +000041#include "Python.h"
Sjoerd Mullendered59d201993-01-06 13:36:38 +000042#include "compile.h"
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000043#include "yuv.h" /* for YUV conversion functions */
Sjoerd Mullendered59d201993-01-06 13:36:38 +000044
45typedef struct {
Barry Warsawf630f6b1996-12-13 01:24:29 +000046 PyObject_HEAD
Sjoerd Mullendered59d201993-01-06 13:36:38 +000047 SV_nodeP ob_svideo;
48 svCaptureInfo ob_info;
49} svobject;
50
51typedef struct {
Barry Warsawf630f6b1996-12-13 01:24:29 +000052 PyObject_HEAD
Sjoerd Mullendered59d201993-01-06 13:36:38 +000053 void *ob_capture;
54 int ob_mustunlock;
55 svCaptureInfo ob_info;
56 svobject *ob_svideo;
57} captureobject;
58
Barry Warsawf630f6b1996-12-13 01:24:29 +000059static PyObject *SvError; /* exception sv.error */
Sjoerd Mullendered59d201993-01-06 13:36:38 +000060
Barry Warsawf630f6b1996-12-13 01:24:29 +000061static PyObject *newcaptureobject Py_PROTO((svobject *, void *, int));
Sjoerd Mullendered59d201993-01-06 13:36:38 +000062
63/* Set a SV-specific error from svideo_errno and return NULL */
Barry Warsawf630f6b1996-12-13 01:24:29 +000064static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +000065sv_error()
66{
Barry Warsawf630f6b1996-12-13 01:24:29 +000067 PyErr_SetString(SvError, svStrerror(svideo_errno));
Sjoerd Mullendered59d201993-01-06 13:36:38 +000068 return NULL;
69}
70
Barry Warsawf630f6b1996-12-13 01:24:29 +000071static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +000072svc_conversion(self, args, function, factor)
73 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +000074 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000075 void (*function)();
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000076 float factor;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000077{
Barry Warsawf630f6b1996-12-13 01:24:29 +000078 PyObject *output;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000079 int invert;
Barry Warsawf630f6b1996-12-13 01:24:29 +000080 char* outstr;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000081
Barry Warsawf630f6b1996-12-13 01:24:29 +000082 if (!PyArg_Parse(args, "i", &invert))
Sjoerd Mullendered59d201993-01-06 13:36:38 +000083 return NULL;
84
Barry Warsawf630f6b1996-12-13 01:24:29 +000085 if (!(output = PyString_FromStringAndSize(
86 NULL,
87 (int)(self->ob_info.width * self->ob_info.height * factor))))
88 {
Sjoerd Mullendered59d201993-01-06 13:36:38 +000089 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +000090 }
91 if (!(outstr = PyString_AsString(output))) {
92 Py_DECREF(output);
93 return NULL;
94 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +000095
Barry Warsawf630f6b1996-12-13 01:24:29 +000096 (*function)((boolean)invert, self->ob_capture,
97 outstr,
Sjoerd Mullendered59d201993-01-06 13:36:38 +000098 self->ob_info.width, self->ob_info.height);
99
100 return output;
101}
102
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000103/*
104 * 3 functions to convert from Starter Video YUV 4:1:1 format to
105 * Compression Library 4:2:2 Duplicate Chroma format.
106 */
Barry Warsawf630f6b1996-12-13 01:24:29 +0000107static PyObject *
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000108svc_YUVtoYUV422DC(self, args)
109 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000110 PyObject *args;
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000111{
112 if (self->ob_info.format != SV_YUV411_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000113 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000114 return NULL;
115 }
116 return svc_conversion(self, args, yuv_sv411_to_cl422dc, 2.0);
117}
118
Barry Warsawf630f6b1996-12-13 01:24:29 +0000119static PyObject *
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000120svc_YUVtoYUV422DC_quarter(self, args)
121 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000122 PyObject *args;
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000123{
124 if (self->ob_info.format != SV_YUV411_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000125 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000126 return NULL;
127 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000128 return svc_conversion(self, args,
129 yuv_sv411_to_cl422dc_quartersize, 0.5);
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000130}
131
Barry Warsawf630f6b1996-12-13 01:24:29 +0000132static PyObject *
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000133svc_YUVtoYUV422DC_sixteenth(self, args)
134 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000135 PyObject *args;
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000136{
137 if (self->ob_info.format != SV_YUV411_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000138 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000139 return NULL;
140 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000141 return svc_conversion(self, args,
142 yuv_sv411_to_cl422dc_sixteenthsize, 0.125);
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000143}
144
Barry Warsawf630f6b1996-12-13 01:24:29 +0000145static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000146svc_YUVtoRGB(self, args)
147 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000148 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000149{
150 switch (self->ob_info.format) {
151 case SV_YUV411_FRAMES:
152 case SV_YUV411_FRAMES_AND_BLANKING_BUFFER:
153 break;
154 default:
Barry Warsawf630f6b1996-12-13 01:24:29 +0000155 PyErr_SetString(SvError, "data had bad format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000156 return NULL;
157 }
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000158 return svc_conversion(self, args, svYUVtoRGB, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000159}
160
Barry Warsawf630f6b1996-12-13 01:24:29 +0000161static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000162svc_RGB8toRGB32(self, args)
163 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000164 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000165{
166 if (self->ob_info.format != SV_RGB8_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000167 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000168 return NULL;
169 }
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000170 return svc_conversion(self, args, svRGB8toRGB32, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000171}
172
Barry Warsawf630f6b1996-12-13 01:24:29 +0000173static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000174svc_InterleaveFields(self, args)
175 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000176 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000177{
178 if (self->ob_info.format != SV_RGB8_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000179 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000180 return NULL;
181 }
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000182 return svc_conversion(self, args, svInterleaveFields, 1.0);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000183}
184
Barry Warsawf630f6b1996-12-13 01:24:29 +0000185static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000186svc_GetFields(self, args)
187 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000188 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000189{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000190 PyObject *f1 = NULL;
191 PyObject *f2 = NULL;
192 PyObject *ret = NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000193 int fieldsize;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000194 char* obcapture;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000195
196 if (self->ob_info.format != SV_RGB8_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000197 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000198 return NULL;
199 }
200
201 fieldsize = self->ob_info.width * self->ob_info.height / 2;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000202 obcapture = (char*)self->ob_capture;
203
204 if (!(f1 = PyString_FromStringAndSize(obcapture, fieldsize)))
205 goto finally;
206 if (!(f2 = PyString_FromStringAndSize(obcapture + fieldsize,
207 fieldsize)))
208 goto finally;
209 ret = Py_BuildValue("(OO)", f1, f2);
210
211 finally:
212 Py_XDECREF(f1);
213 Py_XDECREF(f2);
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000214 return ret;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000215}
216
Barry Warsawf630f6b1996-12-13 01:24:29 +0000217static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000218svc_UnlockCaptureData(self, args)
219 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000220 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000221{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000222 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000223 return NULL;
224
225 if (!self->ob_mustunlock) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000226 PyErr_SetString(SvError, "buffer should not be unlocked");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000227 return NULL;
228 }
229
230 if (svUnlockCaptureData(self->ob_svideo->ob_svideo, self->ob_capture))
231 return sv_error();
232
233 self->ob_mustunlock = 0;
234
Barry Warsawf630f6b1996-12-13 01:24:29 +0000235 Py_INCREF(Py_None);
236 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000237}
238
239#ifdef USE_GL
240#include <gl.h>
241
Barry Warsawf630f6b1996-12-13 01:24:29 +0000242static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000243svc_lrectwrite(self, args)
244 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000245 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000246{
247 Screencoord x1, x2, y1, y2;
248
Barry Warsawf630f6b1996-12-13 01:24:29 +0000249 if (!PyArg_Parse(args, "(hhhh)", &x1, &x2, &y1, &y2))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000250 return NULL;
251
252 lrectwrite(x1, x2, y1, y2, (unsigned long *) self->ob_capture);
253
Barry Warsawf630f6b1996-12-13 01:24:29 +0000254 Py_INCREF(Py_None);
255 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000256}
257#endif
258
Barry Warsawf630f6b1996-12-13 01:24:29 +0000259static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260svc_writefile(self, args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000261 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000262 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000263{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000264 PyObject *file;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000265 int size;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000266 FILE* fp;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000267
Barry Warsawf630f6b1996-12-13 01:24:29 +0000268 if (!PyArg_Parse(args, "O", &file))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000269 return NULL;
270
Barry Warsawf630f6b1996-12-13 01:24:29 +0000271 if (!PyFile_Check(file)) {
272 PyErr_SetString(SvError, "not a file object");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000273 return NULL;
274 }
275
Barry Warsawf630f6b1996-12-13 01:24:29 +0000276 if (!(fp = PyFile_AsFile(file)))
277 return NULL;
278
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000279 size = self->ob_info.width * self->ob_info.height;
280
Barry Warsawf630f6b1996-12-13 01:24:29 +0000281 if (fwrite(self->ob_capture, sizeof(long), size, fp) != size) {
282 PyErr_SetString(SvError, "writing failed");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000283 return NULL;
284 }
285
Barry Warsawf630f6b1996-12-13 01:24:29 +0000286 Py_INCREF(Py_None);
287 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000288}
289
Barry Warsawf630f6b1996-12-13 01:24:29 +0000290static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000291svc_FindVisibleRegion(self, args)
292 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000293 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000294{
295 void *visible;
296 int width;
297
Barry Warsawf630f6b1996-12-13 01:24:29 +0000298 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000299 return NULL;
300
Barry Warsawf630f6b1996-12-13 01:24:29 +0000301 if (svFindVisibleRegion(self->ob_svideo->ob_svideo,
302 self->ob_capture, &visible,
303 self->ob_info.width))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000304 return sv_error();
305
306 if (visible == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000307 PyErr_SetString(SvError, "data in wrong format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000308 return NULL;
309 }
310
311 return newcaptureobject(self->ob_svideo, visible, 0);
312}
313
Barry Warsawf630f6b1996-12-13 01:24:29 +0000314static PyMethodDef capture_methods[] = {
315 {"YUVtoRGB", (PyCFunction)svc_YUVtoRGB},
316 {"RGB8toRGB32", (PyCFunction)svc_RGB8toRGB32},
317 {"InterleaveFields", (PyCFunction)svc_InterleaveFields},
318 {"UnlockCaptureData", (PyCFunction)svc_UnlockCaptureData},
319 {"FindVisibleRegion", (PyCFunction)svc_FindVisibleRegion},
320 {"GetFields", (PyCFunction)svc_GetFields},
321 {"YUVtoYUV422DC", (PyCFunction)svc_YUVtoYUV422DC},
322 {"YUVtoYUV422DC_quarter",(PyCFunction)svc_YUVtoYUV422DC_quarter},
323 {"YUVtoYUV422DC_sixteenth",(PyCFunction)svc_YUVtoYUV422DC_sixteenth},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000324#ifdef USE_GL
Barry Warsawf630f6b1996-12-13 01:24:29 +0000325 {"lrectwrite", (PyCFunction)svc_lrectwrite},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000326#endif
Barry Warsawf630f6b1996-12-13 01:24:29 +0000327 {"writefile", (PyCFunction)svc_writefile},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000328 {NULL, NULL} /* sentinel */
329};
330
331static void
332capture_dealloc(self)
333 captureobject *self;
334{
335 if (self->ob_capture != NULL) {
336 if (self->ob_mustunlock)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000337 (void)svUnlockCaptureData(self->ob_svideo->ob_svideo,
338 self->ob_capture);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000339 self->ob_capture = NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000340 Py_DECREF(self->ob_svideo);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000341 self->ob_svideo = NULL;
342 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000343 PyMem_DEL(self);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000344}
345
Barry Warsawf630f6b1996-12-13 01:24:29 +0000346static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000347capture_getattr(self, name)
348 svobject *self;
349 char *name;
350{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000351 return Py_FindMethod(capture_methods, (PyObject *)self, name);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000352}
353
Barry Warsawf630f6b1996-12-13 01:24:29 +0000354PyTypeObject Capturetype = {
355 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000356 0, /*ob_size*/
357 "capture", /*tp_name*/
358 sizeof(captureobject), /*tp_size*/
359 0, /*tp_itemsize*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000360 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000361 (destructor)capture_dealloc, /*tp_dealloc*/
362 0, /*tp_print*/
363 (getattrfunc)capture_getattr, /*tp_getattr*/
364 0, /*tp_setattr*/
365 0, /*tp_compare*/
366 0, /*tp_repr*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000367};
368
Barry Warsawf630f6b1996-12-13 01:24:29 +0000369static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000370newcaptureobject(self, ptr, mustunlock)
371 svobject *self;
372 void *ptr;
373 int mustunlock;
374{
375 captureobject *p;
376
Barry Warsawf630f6b1996-12-13 01:24:29 +0000377 p = PyObject_NEW(captureobject, &Capturetype);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000378 if (p == NULL)
379 return NULL;
380 p->ob_svideo = self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000381 Py_INCREF(self);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000382 p->ob_capture = ptr;
383 p->ob_mustunlock = mustunlock;
384 p->ob_info = self->ob_info;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000385 return (PyObject *) p;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000386}
387
Barry Warsawf630f6b1996-12-13 01:24:29 +0000388static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000389sv_GetCaptureData(self, args)
390 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000391 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000392{
393 void *ptr;
394 long fieldID;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000395 PyObject *res, *c;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000396
Barry Warsawf630f6b1996-12-13 01:24:29 +0000397 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000398 return NULL;
399
400 if (svGetCaptureData(self->ob_svideo, &ptr, &fieldID))
401 return sv_error();
402
403 if (ptr == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000404 PyErr_SetString(SvError, "no data available");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000405 return NULL;
406 }
407
408 c = newcaptureobject(self, ptr, 1);
409 if (c == NULL)
410 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000411 res = Py_BuildValue("(Oi)", c, fieldID);
412 Py_DECREF(c);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000413 return res;
414}
415
Barry Warsawf630f6b1996-12-13 01:24:29 +0000416static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000417sv_BindGLWindow(self, args)
418 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000419 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000420{
421 long wid;
422 int mode;
423
Barry Warsawf630f6b1996-12-13 01:24:29 +0000424 if (!PyArg_Parse(args, "(ii)", &wid, &mode))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000425 return NULL;
426
427 if (svBindGLWindow(self->ob_svideo, wid, mode))
428 return sv_error();
429
Barry Warsawf630f6b1996-12-13 01:24:29 +0000430 Py_INCREF(Py_None);
431 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000432}
433
Barry Warsawf630f6b1996-12-13 01:24:29 +0000434static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000435sv_EndContinuousCapture(self, args)
436 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000437 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000438{
439
Barry Warsawf630f6b1996-12-13 01:24:29 +0000440 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000441 return NULL;
442
443 if (svEndContinuousCapture(self->ob_svideo))
444 return sv_error();
445
Barry Warsawf630f6b1996-12-13 01:24:29 +0000446 Py_INCREF(Py_None);
447 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000448}
449
Barry Warsawf630f6b1996-12-13 01:24:29 +0000450static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000451sv_IsVideoDisplayed(self, args)
452 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000453 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000454{
455 int v;
456
Barry Warsawf630f6b1996-12-13 01:24:29 +0000457 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000458 return NULL;
459
460 v = svIsVideoDisplayed(self->ob_svideo);
461 if (v == -1)
462 return sv_error();
463
Barry Warsawf630f6b1996-12-13 01:24:29 +0000464 return PyInt_FromLong((long) v);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000465}
466
Barry Warsawf630f6b1996-12-13 01:24:29 +0000467static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000468sv_OutputOffset(self, args)
469 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000470 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000471{
472 int x_offset;
473 int y_offset;
474
Barry Warsawf630f6b1996-12-13 01:24:29 +0000475 if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000476 return NULL;
477
478 if (svOutputOffset(self->ob_svideo, x_offset, y_offset))
479 return sv_error();
480
Barry Warsawf630f6b1996-12-13 01:24:29 +0000481 Py_INCREF(Py_None);
482 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000483}
484
Barry Warsawf630f6b1996-12-13 01:24:29 +0000485static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000486sv_PutFrame(self, args)
487 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000488 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000489{
490 char *buffer;
491
Barry Warsawf630f6b1996-12-13 01:24:29 +0000492 if (!PyArg_Parse(args, "s", &buffer))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000493 return NULL;
494
495 if (svPutFrame(self->ob_svideo, buffer))
496 return sv_error();
497
Barry Warsawf630f6b1996-12-13 01:24:29 +0000498 Py_INCREF(Py_None);
499 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000500}
501
Barry Warsawf630f6b1996-12-13 01:24:29 +0000502static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000503sv_QuerySize(self, args)
504 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000505 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000506{
507 int w;
508 int h;
509 int rw;
510 int rh;
511
Barry Warsawf630f6b1996-12-13 01:24:29 +0000512 if (!PyArg_Parse(args, "(ii)", &w, &h))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000513 return NULL;
514
515 if (svQuerySize(self->ob_svideo, w, h, &rw, &rh))
516 return sv_error();
517
Barry Warsawf630f6b1996-12-13 01:24:29 +0000518 return Py_BuildValue("(ii)", (long) rw, (long) rh);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000519}
520
Barry Warsawf630f6b1996-12-13 01:24:29 +0000521static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000522sv_SetSize(self, args)
523 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000524 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000525{
526 int w;
527 int h;
528
Barry Warsawf630f6b1996-12-13 01:24:29 +0000529 if (!PyArg_Parse(args, "(ii)", &w, &h))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000530 return NULL;
531
532 if (svSetSize(self->ob_svideo, w, h))
533 return sv_error();
534
Barry Warsawf630f6b1996-12-13 01:24:29 +0000535 Py_INCREF(Py_None);
536 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000537}
538
Barry Warsawf630f6b1996-12-13 01:24:29 +0000539static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000540sv_SetStdDefaults(self, args)
541 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000542 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000543{
544
Barry Warsawf630f6b1996-12-13 01:24:29 +0000545 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000546 return NULL;
547
548 if (svSetStdDefaults(self->ob_svideo))
549 return sv_error();
550
Barry Warsawf630f6b1996-12-13 01:24:29 +0000551 Py_INCREF(Py_None);
552 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000553}
554
Barry Warsawf630f6b1996-12-13 01:24:29 +0000555static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000556sv_UseExclusive(self, args)
557 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000558 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000559{
560 boolean onoff;
561 int mode;
562
Barry Warsawf630f6b1996-12-13 01:24:29 +0000563 if (!PyArg_Parse(args, "(ii)", &onoff, &mode))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000564 return NULL;
565
566 if (svUseExclusive(self->ob_svideo, onoff, mode))
567 return sv_error();
568
Barry Warsawf630f6b1996-12-13 01:24:29 +0000569 Py_INCREF(Py_None);
570 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000571}
572
Barry Warsawf630f6b1996-12-13 01:24:29 +0000573static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000574sv_WindowOffset(self, args)
575 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000576 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000577{
578 int x_offset;
579 int y_offset;
580
Barry Warsawf630f6b1996-12-13 01:24:29 +0000581 if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000582 return NULL;
583
584 if (svWindowOffset(self->ob_svideo, x_offset, y_offset))
585 return sv_error();
586
Barry Warsawf630f6b1996-12-13 01:24:29 +0000587 Py_INCREF(Py_None);
588 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000589}
590
Barry Warsawf630f6b1996-12-13 01:24:29 +0000591static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000592sv_CaptureBurst(self, args)
593 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000594 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000595{
596 int bytes, i;
597 svCaptureInfo info;
598 void *bitvector = NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000599 PyObject *videodata = NULL;
600 PyObject *bitvecobj = NULL;
Guido van Rossumc88c9cb1996-12-17 20:43:55 +0000601 PyObject *res = NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000602 static PyObject *evenitem, *odditem;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000603
Barry Warsawf630f6b1996-12-13 01:24:29 +0000604 if (!PyArg_Parse(args, "(iiiii)", &info.format,
605 &info.width, &info.height,
606 &info.size, &info.samplingrate))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000607 return NULL;
608
609 switch (info.format) {
610 case SV_RGB8_FRAMES:
611 bitvector = malloc(SV_BITVEC_SIZE(info.size));
612 break;
613 case SV_YUV411_FRAMES_AND_BLANKING_BUFFER:
614 break;
615 default:
Barry Warsawf630f6b1996-12-13 01:24:29 +0000616 PyErr_SetString(SvError, "illegal format specified");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000617 return NULL;
618 }
619
620 if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes)) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000621 res = sv_error();
622 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000623 }
624
Barry Warsawf630f6b1996-12-13 01:24:29 +0000625 if (!(videodata = PyString_FromStringAndSize(NULL, bytes)))
626 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000627
628 /* XXX -- need to do something about the bitvector */
Barry Warsawf630f6b1996-12-13 01:24:29 +0000629 {
630 char* str = PyString_AsString(videodata);
631 if (!str)
632 goto finally;
633
634 if (svCaptureBurst(self->ob_svideo, &info, str, bitvector)) {
635 res = sv_error();
636 goto finally;
637 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000638 }
639
640 if (bitvector) {
641 if (evenitem == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000642 if (!(evenitem = PyInt_FromLong(0)))
643 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000644 }
645 if (odditem == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000646 if (!(odditem = PyInt_FromLong(1)))
647 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000648 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000649 if (!(bitvecobj = PyTuple_New(2 * info.size)))
650 goto finally;
651
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000652 for (i = 0; i < 2 * info.size; i++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000653 int sts;
654
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000655 if (SV_GET_FIELD(bitvector, i) == SV_EVEN_FIELD) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000656 Py_INCREF(evenitem);
657 sts = PyTuple_SetItem(bitvecobj, i, evenitem);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000658 } else {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000659 Py_INCREF(odditem);
660 sts = PyTuple_SetItem(bitvecobj, i, odditem);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000661 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000662 if (sts < 0)
663 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000664 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000665 } else {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000666 bitvecobj = Py_None;
667 Py_INCREF(Py_None);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000668 }
669
Barry Warsawf630f6b1996-12-13 01:24:29 +0000670 res = Py_BuildValue("((iiiii)OO)", info.format,
671 info.width, info.height,
672 info.size, info.samplingrate,
673 videodata, bitvecobj);
674
675 finally:
676 if (bitvector)
677 free(bitvector);
678
679 Py_XDECREF(videodata);
680 Py_XDECREF(bitvecobj);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000681 return res;
682}
683
Barry Warsawf630f6b1996-12-13 01:24:29 +0000684static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000685sv_CaptureOneFrame(self, args)
686 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000687 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000688{
689 svCaptureInfo info;
690 int format, width, height;
691 int bytes;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000692 PyObject *videodata = NULL;
693 PyObject *res = NULL;
Guido van Rossumc88c9cb1996-12-17 20:43:55 +0000694 char *str;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000695
Barry Warsawf630f6b1996-12-13 01:24:29 +0000696 if (!PyArg_Parse(args, "(iii)", &format, &width, &height))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000697 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000698
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000699 info.format = format;
700 info.width = width;
701 info.height = height;
702 info.size = 0;
703 info.samplingrate = 0;
704 if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes))
705 return sv_error();
Barry Warsawf630f6b1996-12-13 01:24:29 +0000706
707 if (!(videodata = PyString_FromStringAndSize(NULL, bytes)))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000708 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000709
Guido van Rossumc88c9cb1996-12-17 20:43:55 +0000710 str = PyString_AsString(videodata);
711 if (!str)
712 goto finally;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000713
714 if (svCaptureOneFrame(self->ob_svideo, format, &width, &height, str)) {
715 res = sv_error();
716 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000717 }
718
Barry Warsawf630f6b1996-12-13 01:24:29 +0000719 res = Py_BuildValue("(iiO)", width, height, videodata);
720
721 finally:
722 Py_XDECREF(videodata);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000723 return res;
724}
725
Barry Warsawf630f6b1996-12-13 01:24:29 +0000726static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000727sv_InitContinuousCapture(self, args)
728 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000729 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000730{
731 svCaptureInfo info;
732
Barry Warsawf630f6b1996-12-13 01:24:29 +0000733 if (!PyArg_Parse(args, "(iiiii)", &info.format,
734 &info.width, &info.height,
735 &info.size, &info.samplingrate))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000736 return NULL;
737
738 if (svInitContinuousCapture(self->ob_svideo, &info))
739 return sv_error();
740
741 self->ob_info = info;
742
Barry Warsawf630f6b1996-12-13 01:24:29 +0000743 return Py_BuildValue("(iiiii)", info.format, info.width, info.height,
744 info.size, info.samplingrate);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000745}
746
Barry Warsawf630f6b1996-12-13 01:24:29 +0000747static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000748sv_LoadMap(self, args)
749 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000750 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000751{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000752 PyObject *rgb;
753 PyObject *res = NULL;
754 rgb_tuple *mapp = NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000755 int maptype;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000756 int i, j; /* indices */
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000757
Barry Warsawf630f6b1996-12-13 01:24:29 +0000758 if (!PyArg_Parse(args, "(iO)", &maptype, &rgb))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000759 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000760
761 if (!PyList_Check(rgb) || PyList_Size(rgb) != 256) {
762 PyErr_BadArgument();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000763 return NULL;
764 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000765
766 if (!(mapp = PyMem_NEW(rgb_tuple, 256)))
767 return PyErr_NoMemory();
768
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000769 for (i = 0; i < 256; i++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000770 PyObject* v = PyList_GetItem(rgb, i);
771 if (!v)
772 goto finally;
773
774 if (!PyTuple_Check(v) || PyTuple_Size(v) != 3) {
775 PyErr_BadArgument();
776 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000777 }
778 for (j = 0; j < 3; j++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000779 PyObject* cell = PyTuple_GetItem(v, j);
780 if (!cell)
781 goto finally;
782
783 if (!PyInt_Check(cell)) {
784 PyErr_BadArgument();
785 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000786 }
787 switch (j) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000788 case 0: mapp[i].red = PyInt_AsLong(cell); break;
789 case 1: mapp[i].blue = PyInt_AsLong(cell); break;
790 case 2: mapp[i].green = PyInt_AsLong(cell); break;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000791 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000792 if (PyErr_Occurred())
793 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000794 }
795 }
796
797 if (svLoadMap(self->ob_svideo, maptype, mapp)) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000798 res = sv_error();
799 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000800 }
801
Barry Warsawf630f6b1996-12-13 01:24:29 +0000802 Py_INCREF(Py_None);
803 res = Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000804
Barry Warsawf630f6b1996-12-13 01:24:29 +0000805 finally:
806 PyMem_DEL(mapp);
807 return res;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000808}
809
Barry Warsawf630f6b1996-12-13 01:24:29 +0000810static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000811sv_CloseVideo(self, args)
812 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000813 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000814{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000815 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000816 return NULL;
817
818 if (svCloseVideo(self->ob_svideo))
819 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000820
Barry Warsawf630f6b1996-12-13 01:24:29 +0000821 self->ob_svideo = NULL;
822 Py_INCREF(Py_None);
823 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000824}
825
Barry Warsawf630f6b1996-12-13 01:24:29 +0000826static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000827doParams(self, args, func, modified)
828 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000829 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000830 int (*func)(SV_nodeP, long *, int);
831 int modified;
832{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000833 PyObject *list;
834 PyObject *res = NULL;
835 long *PVbuffer = NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000836 long length;
837 int i;
838
Barry Warsawf630f6b1996-12-13 01:24:29 +0000839 if (!PyArg_Parse(args, "O", &list))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000840 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000841
842 if (!PyList_Check(list)) {
843 PyErr_BadArgument();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000844 return NULL;
845 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000846
847 if ((length = PyList_Size(list)) < 0)
848 return NULL;
849
850 PVbuffer = PyMem_NEW(long, length);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000851 if (PVbuffer == NULL)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000852 return PyErr_NoMemory();
853
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000854 for (i = 0; i < length; i++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000855 PyObject *v = PyList_GetItem(list, i);
856 if (!v)
857 goto finally;
858
859 if (!PyInt_Check(v)) {
860 PyErr_BadArgument();
861 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000862 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000863 PVbuffer[i] = PyInt_AsLong(v);
864 /* can't just test the return value, because what if the
865 value was -1?!
866 */
867 if (PVbuffer[i] == -1 && PyErr_Occurred())
868 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000869 }
870
871 if ((*func)(self->ob_svideo, PVbuffer, length)) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000872 res = sv_error();
873 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000874 }
875
876 if (modified) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000877 for (i = 0; i < length; i++) {
878 PyObject* v = PyInt_FromLong(PVbuffer[i]);
879 if (!v || PyList_SetItem(list, i, v) < 0)
880 goto finally;
881 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000882 }
883
Barry Warsawf630f6b1996-12-13 01:24:29 +0000884 Py_INCREF(Py_None);
885 res = Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000886
Barry Warsawf630f6b1996-12-13 01:24:29 +0000887 finally:
888 PyMem_DEL(PVbuffer);
889 return res;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000890}
891
Barry Warsawf630f6b1996-12-13 01:24:29 +0000892static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000893sv_GetParam(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000894 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000895{
896 return doParams(self, args, svGetParam, 1);
897}
898
Barry Warsawf630f6b1996-12-13 01:24:29 +0000899static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000900sv_GetParamRange(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000901 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000902{
903 return doParams(self, args, svGetParamRange, 1);
904}
905
Barry Warsawf630f6b1996-12-13 01:24:29 +0000906static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000907sv_SetParam(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000908 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000909{
910 return doParams(self, args, svSetParam, 0);
911}
912
Barry Warsawf630f6b1996-12-13 01:24:29 +0000913static PyMethodDef svideo_methods[] = {
914 {"BindGLWindow", (PyCFunction)sv_BindGLWindow},
915 {"EndContinuousCapture",(PyCFunction)sv_EndContinuousCapture},
916 {"IsVideoDisplayed", (PyCFunction)sv_IsVideoDisplayed},
917 {"OutputOffset", (PyCFunction)sv_OutputOffset},
918 {"PutFrame", (PyCFunction)sv_PutFrame},
919 {"QuerySize", (PyCFunction)sv_QuerySize},
920 {"SetSize", (PyCFunction)sv_SetSize},
921 {"SetStdDefaults", (PyCFunction)sv_SetStdDefaults},
922 {"UseExclusive", (PyCFunction)sv_UseExclusive},
923 {"WindowOffset", (PyCFunction)sv_WindowOffset},
924 {"InitContinuousCapture",(PyCFunction)sv_InitContinuousCapture},
925 {"CaptureBurst", (PyCFunction)sv_CaptureBurst},
926 {"CaptureOneFrame", (PyCFunction)sv_CaptureOneFrame},
927 {"GetCaptureData", (PyCFunction)sv_GetCaptureData},
928 {"CloseVideo", (PyCFunction)sv_CloseVideo},
929 {"LoadMap", (PyCFunction)sv_LoadMap},
930 {"GetParam", (PyCFunction)sv_GetParam},
931 {"GetParamRange", (PyCFunction)sv_GetParamRange},
932 {"SetParam", (PyCFunction)sv_SetParam},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000933 {NULL, NULL} /* sentinel */
934};
935
Barry Warsawf630f6b1996-12-13 01:24:29 +0000936static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000937sv_conversion(self, args, function, inputfactor, factor)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000938 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000939 void (*function)();
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000940 int inputfactor;
941 float factor;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000942{
943 int invert, width, height, inputlength;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000944 char *input, *str;
945 PyObject *output;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000946
Barry Warsawf630f6b1996-12-13 01:24:29 +0000947 if (!PyArg_Parse(args, "(is#ii)", &invert,
948 &input, &inputlength, &width, &height))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000949 return NULL;
950
951 if (width * height * inputfactor > inputlength) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000952 PyErr_SetString(SvError, "input buffer not long enough");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000953 return NULL;
954 }
955
Barry Warsawf630f6b1996-12-13 01:24:29 +0000956 if (!(output = PyString_FromStringAndSize(NULL,
957 (int)(width * height * factor))))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000958 return NULL;
959
Barry Warsawf630f6b1996-12-13 01:24:29 +0000960 str = PyString_AsString(output);
961 if (!str) {
962 Py_DECREF(output);
963 return NULL;
964 }
965 (*function)(invert, input, str, width, height);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000966
967 return output;
968}
969
Barry Warsawf630f6b1996-12-13 01:24:29 +0000970static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000971sv_InterleaveFields(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000972 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000973{
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000974 return sv_conversion(self, args, svInterleaveFields, 1, 1.0);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000975}
976
Barry Warsawf630f6b1996-12-13 01:24:29 +0000977static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000978sv_RGB8toRGB32(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000979 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000980{
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000981 return sv_conversion(self, args, svRGB8toRGB32, 1, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000982}
983
Barry Warsawf630f6b1996-12-13 01:24:29 +0000984static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000985sv_YUVtoRGB(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000986 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000987{
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000988 return sv_conversion(self, args, svYUVtoRGB, 2, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000989}
990
991static void
992svideo_dealloc(self)
993 svobject *self;
994{
995 if (self->ob_svideo != NULL)
996 (void) svCloseVideo(self->ob_svideo);
Barry Warsawf630f6b1996-12-13 01:24:29 +0000997 PyMem_DEL(self);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000998}
999
Barry Warsawf630f6b1996-12-13 01:24:29 +00001000static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001001svideo_getattr(self, name)
1002 svobject *self;
1003 char *name;
1004{
Barry Warsawf630f6b1996-12-13 01:24:29 +00001005 return Py_FindMethod(svideo_methods, (PyObject *)self, name);
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001006}
1007
Barry Warsawf630f6b1996-12-13 01:24:29 +00001008PyTypeObject Svtype = {
1009 PyObject_HEAD_INIT(&PyType_Type)
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001010 0, /*ob_size*/
1011 "sv", /*tp_name*/
1012 sizeof(svobject), /*tp_size*/
1013 0, /*tp_itemsize*/
1014 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001015 (destructor)svideo_dealloc, /*tp_dealloc*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001016 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001017 (getattrfunc)svideo_getattr, /*tp_getattr*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001018 0, /*tp_setattr*/
1019 0, /*tp_compare*/
1020 0, /*tp_repr*/
1021};
1022
Barry Warsawf630f6b1996-12-13 01:24:29 +00001023static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001024newsvobject(svp)
1025 SV_nodeP svp;
1026{
1027 svobject *p;
1028
Barry Warsawf630f6b1996-12-13 01:24:29 +00001029 p = PyObject_NEW(svobject, &Svtype);
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001030 if (p == NULL)
1031 return NULL;
1032 p->ob_svideo = svp;
1033 p->ob_info.format = 0;
1034 p->ob_info.size = 0;
1035 p->ob_info.width = 0;
1036 p->ob_info.height = 0;
1037 p->ob_info.samplingrate = 0;
Barry Warsawf630f6b1996-12-13 01:24:29 +00001038 return (PyObject *) p;
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001039}
1040
Barry Warsawf630f6b1996-12-13 01:24:29 +00001041static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001042sv_OpenVideo(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +00001043 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001044{
1045 SV_nodeP svp;
1046
Barry Warsawf630f6b1996-12-13 01:24:29 +00001047 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001048 return NULL;
1049
1050 svp = svOpenVideo();
1051 if (svp == NULL)
1052 return sv_error();
1053
1054 return newsvobject(svp);
1055}
1056
Barry Warsawf630f6b1996-12-13 01:24:29 +00001057static PyMethodDef sv_methods[] = {
1058 {"InterleaveFields", (PyCFunction)sv_InterleaveFields},
1059 {"RGB8toRGB32", (PyCFunction)sv_RGB8toRGB32},
1060 {"YUVtoRGB", (PyCFunction)sv_YUVtoRGB},
1061 {"OpenVideo", (PyCFunction)sv_OpenVideo},
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001062 {NULL, NULL} /* Sentinel */
1063};
1064
1065void
1066initsv()
1067{
Barry Warsawf630f6b1996-12-13 01:24:29 +00001068 PyObject *m, *d;
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001069
Barry Warsawf630f6b1996-12-13 01:24:29 +00001070 m = Py_InitModule("sv", sv_methods);
1071 d = PyModule_GetDict(m);
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001072
Guido van Rossum0cb96de1997-10-01 04:29:29 +00001073 SvError = PyErr_NewException("sv.error", NULL, NULL);
Barry Warsawf630f6b1996-12-13 01:24:29 +00001074 if (SvError == NULL || PyDict_SetItemString(d, "error", SvError) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00001075 return;
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001076}