blob: f4e66df133bd06b0d92d5e9f9cf749361f69ad42 [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
34#include <sys/time.h>
35#include <svideo.h>
Barry Warsawf630f6b1996-12-13 01:24:29 +000036#include "Python.h"
Sjoerd Mullendered59d201993-01-06 13:36:38 +000037#include "compile.h"
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000038#include "yuv.h" /* for YUV conversion functions */
Sjoerd Mullendered59d201993-01-06 13:36:38 +000039
40typedef struct {
Barry Warsawf630f6b1996-12-13 01:24:29 +000041 PyObject_HEAD
Sjoerd Mullendered59d201993-01-06 13:36:38 +000042 SV_nodeP ob_svideo;
43 svCaptureInfo ob_info;
44} svobject;
45
46typedef struct {
Barry Warsawf630f6b1996-12-13 01:24:29 +000047 PyObject_HEAD
Sjoerd Mullendered59d201993-01-06 13:36:38 +000048 void *ob_capture;
49 int ob_mustunlock;
50 svCaptureInfo ob_info;
51 svobject *ob_svideo;
52} captureobject;
53
Barry Warsawf630f6b1996-12-13 01:24:29 +000054static PyObject *SvError; /* exception sv.error */
Sjoerd Mullendered59d201993-01-06 13:36:38 +000055
Barry Warsawf630f6b1996-12-13 01:24:29 +000056static PyObject *newcaptureobject Py_PROTO((svobject *, void *, int));
Sjoerd Mullendered59d201993-01-06 13:36:38 +000057
58/* Set a SV-specific error from svideo_errno and return NULL */
Barry Warsawf630f6b1996-12-13 01:24:29 +000059static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +000060sv_error()
61{
Barry Warsawf630f6b1996-12-13 01:24:29 +000062 PyErr_SetString(SvError, svStrerror(svideo_errno));
Sjoerd Mullendered59d201993-01-06 13:36:38 +000063 return NULL;
64}
65
Barry Warsawf630f6b1996-12-13 01:24:29 +000066static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +000067svc_conversion(self, args, function, factor)
68 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +000069 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000070 void (*function)();
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000071 float factor;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000072{
Barry Warsawf630f6b1996-12-13 01:24:29 +000073 PyObject *output;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000074 int invert;
Barry Warsawf630f6b1996-12-13 01:24:29 +000075 char* outstr;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000076
Barry Warsawf630f6b1996-12-13 01:24:29 +000077 if (!PyArg_Parse(args, "i", &invert))
Sjoerd Mullendered59d201993-01-06 13:36:38 +000078 return NULL;
79
Barry Warsawf630f6b1996-12-13 01:24:29 +000080 if (!(output = PyString_FromStringAndSize(
81 NULL,
82 (int)(self->ob_info.width * self->ob_info.height * factor))))
83 {
Sjoerd Mullendered59d201993-01-06 13:36:38 +000084 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +000085 }
86 if (!(outstr = PyString_AsString(output))) {
87 Py_DECREF(output);
88 return NULL;
89 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +000090
Barry Warsawf630f6b1996-12-13 01:24:29 +000091 (*function)((boolean)invert, self->ob_capture,
92 outstr,
Sjoerd Mullendered59d201993-01-06 13:36:38 +000093 self->ob_info.width, self->ob_info.height);
94
95 return output;
96}
97
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000098/*
99 * 3 functions to convert from Starter Video YUV 4:1:1 format to
100 * Compression Library 4:2:2 Duplicate Chroma format.
101 */
Barry Warsawf630f6b1996-12-13 01:24:29 +0000102static PyObject *
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000103svc_YUVtoYUV422DC(self, args)
104 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000105 PyObject *args;
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000106{
107 if (self->ob_info.format != SV_YUV411_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000108 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000109 return NULL;
110 }
111 return svc_conversion(self, args, yuv_sv411_to_cl422dc, 2.0);
112}
113
Barry Warsawf630f6b1996-12-13 01:24:29 +0000114static PyObject *
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000115svc_YUVtoYUV422DC_quarter(self, args)
116 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000117 PyObject *args;
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000118{
119 if (self->ob_info.format != SV_YUV411_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000120 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000121 return NULL;
122 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000123 return svc_conversion(self, args,
124 yuv_sv411_to_cl422dc_quartersize, 0.5);
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000125}
126
Barry Warsawf630f6b1996-12-13 01:24:29 +0000127static PyObject *
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000128svc_YUVtoYUV422DC_sixteenth(self, args)
129 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000130 PyObject *args;
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000131{
132 if (self->ob_info.format != SV_YUV411_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000133 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000134 return NULL;
135 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000136 return svc_conversion(self, args,
137 yuv_sv411_to_cl422dc_sixteenthsize, 0.125);
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000138}
139
Barry Warsawf630f6b1996-12-13 01:24:29 +0000140static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000141svc_YUVtoRGB(self, args)
142 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000143 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000144{
145 switch (self->ob_info.format) {
146 case SV_YUV411_FRAMES:
147 case SV_YUV411_FRAMES_AND_BLANKING_BUFFER:
148 break;
149 default:
Barry Warsawf630f6b1996-12-13 01:24:29 +0000150 PyErr_SetString(SvError, "data had bad format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000151 return NULL;
152 }
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000153 return svc_conversion(self, args, svYUVtoRGB, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000154}
155
Barry Warsawf630f6b1996-12-13 01:24:29 +0000156static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000157svc_RGB8toRGB32(self, args)
158 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000159 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000160{
161 if (self->ob_info.format != SV_RGB8_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000162 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000163 return NULL;
164 }
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000165 return svc_conversion(self, args, svRGB8toRGB32, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000166}
167
Barry Warsawf630f6b1996-12-13 01:24:29 +0000168static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000169svc_InterleaveFields(self, args)
170 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000171 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000172{
173 if (self->ob_info.format != SV_RGB8_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000174 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000175 return NULL;
176 }
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000177 return svc_conversion(self, args, svInterleaveFields, 1.0);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000178}
179
Barry Warsawf630f6b1996-12-13 01:24:29 +0000180static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000181svc_GetFields(self, args)
182 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000183 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000184{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000185 PyObject *f1 = NULL;
186 PyObject *f2 = NULL;
187 PyObject *ret = NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000188 int fieldsize;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000189 char* obcapture;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000190
191 if (self->ob_info.format != SV_RGB8_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000192 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000193 return NULL;
194 }
195
196 fieldsize = self->ob_info.width * self->ob_info.height / 2;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000197 obcapture = (char*)self->ob_capture;
198
199 if (!(f1 = PyString_FromStringAndSize(obcapture, fieldsize)))
200 goto finally;
201 if (!(f2 = PyString_FromStringAndSize(obcapture + fieldsize,
202 fieldsize)))
203 goto finally;
204 ret = Py_BuildValue("(OO)", f1, f2);
205
206 finally:
207 Py_XDECREF(f1);
208 Py_XDECREF(f2);
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000209 return ret;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000210}
211
Barry Warsawf630f6b1996-12-13 01:24:29 +0000212static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000213svc_UnlockCaptureData(self, args)
214 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000215 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000216{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000217 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000218 return NULL;
219
220 if (!self->ob_mustunlock) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000221 PyErr_SetString(SvError, "buffer should not be unlocked");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000222 return NULL;
223 }
224
225 if (svUnlockCaptureData(self->ob_svideo->ob_svideo, self->ob_capture))
226 return sv_error();
227
228 self->ob_mustunlock = 0;
229
Barry Warsawf630f6b1996-12-13 01:24:29 +0000230 Py_INCREF(Py_None);
231 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000232}
233
234#ifdef USE_GL
235#include <gl.h>
236
Barry Warsawf630f6b1996-12-13 01:24:29 +0000237static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000238svc_lrectwrite(self, args)
239 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000240 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000241{
242 Screencoord x1, x2, y1, y2;
243
Barry Warsawf630f6b1996-12-13 01:24:29 +0000244 if (!PyArg_Parse(args, "(hhhh)", &x1, &x2, &y1, &y2))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000245 return NULL;
246
247 lrectwrite(x1, x2, y1, y2, (unsigned long *) self->ob_capture);
248
Barry Warsawf630f6b1996-12-13 01:24:29 +0000249 Py_INCREF(Py_None);
250 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000251}
252#endif
253
Barry Warsawf630f6b1996-12-13 01:24:29 +0000254static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255svc_writefile(self, args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000256 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000257 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000258{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000259 PyObject *file;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000260 int size;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000261 FILE* fp;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000262
Barry Warsawf630f6b1996-12-13 01:24:29 +0000263 if (!PyArg_Parse(args, "O", &file))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000264 return NULL;
265
Barry Warsawf630f6b1996-12-13 01:24:29 +0000266 if (!PyFile_Check(file)) {
267 PyErr_SetString(SvError, "not a file object");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000268 return NULL;
269 }
270
Barry Warsawf630f6b1996-12-13 01:24:29 +0000271 if (!(fp = PyFile_AsFile(file)))
272 return NULL;
273
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000274 size = self->ob_info.width * self->ob_info.height;
275
Barry Warsawf630f6b1996-12-13 01:24:29 +0000276 if (fwrite(self->ob_capture, sizeof(long), size, fp) != size) {
277 PyErr_SetString(SvError, "writing failed");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000278 return NULL;
279 }
280
Barry Warsawf630f6b1996-12-13 01:24:29 +0000281 Py_INCREF(Py_None);
282 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000283}
284
Barry Warsawf630f6b1996-12-13 01:24:29 +0000285static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000286svc_FindVisibleRegion(self, args)
287 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000288 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000289{
290 void *visible;
291 int width;
292
Barry Warsawf630f6b1996-12-13 01:24:29 +0000293 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000294 return NULL;
295
Barry Warsawf630f6b1996-12-13 01:24:29 +0000296 if (svFindVisibleRegion(self->ob_svideo->ob_svideo,
297 self->ob_capture, &visible,
298 self->ob_info.width))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000299 return sv_error();
300
301 if (visible == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000302 PyErr_SetString(SvError, "data in wrong format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000303 return NULL;
304 }
305
306 return newcaptureobject(self->ob_svideo, visible, 0);
307}
308
Barry Warsawf630f6b1996-12-13 01:24:29 +0000309static PyMethodDef capture_methods[] = {
310 {"YUVtoRGB", (PyCFunction)svc_YUVtoRGB},
311 {"RGB8toRGB32", (PyCFunction)svc_RGB8toRGB32},
312 {"InterleaveFields", (PyCFunction)svc_InterleaveFields},
313 {"UnlockCaptureData", (PyCFunction)svc_UnlockCaptureData},
314 {"FindVisibleRegion", (PyCFunction)svc_FindVisibleRegion},
315 {"GetFields", (PyCFunction)svc_GetFields},
316 {"YUVtoYUV422DC", (PyCFunction)svc_YUVtoYUV422DC},
317 {"YUVtoYUV422DC_quarter",(PyCFunction)svc_YUVtoYUV422DC_quarter},
318 {"YUVtoYUV422DC_sixteenth",(PyCFunction)svc_YUVtoYUV422DC_sixteenth},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000319#ifdef USE_GL
Barry Warsawf630f6b1996-12-13 01:24:29 +0000320 {"lrectwrite", (PyCFunction)svc_lrectwrite},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000321#endif
Barry Warsawf630f6b1996-12-13 01:24:29 +0000322 {"writefile", (PyCFunction)svc_writefile},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000323 {NULL, NULL} /* sentinel */
324};
325
326static void
327capture_dealloc(self)
328 captureobject *self;
329{
330 if (self->ob_capture != NULL) {
331 if (self->ob_mustunlock)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000332 (void)svUnlockCaptureData(self->ob_svideo->ob_svideo,
333 self->ob_capture);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000334 self->ob_capture = NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000335 Py_DECREF(self->ob_svideo);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000336 self->ob_svideo = NULL;
337 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000338 PyMem_DEL(self);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000339}
340
Barry Warsawf630f6b1996-12-13 01:24:29 +0000341static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000342capture_getattr(self, name)
343 svobject *self;
344 char *name;
345{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000346 return Py_FindMethod(capture_methods, (PyObject *)self, name);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000347}
348
Barry Warsawf630f6b1996-12-13 01:24:29 +0000349PyTypeObject Capturetype = {
350 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000351 0, /*ob_size*/
352 "capture", /*tp_name*/
353 sizeof(captureobject), /*tp_size*/
354 0, /*tp_itemsize*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000355 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000356 (destructor)capture_dealloc, /*tp_dealloc*/
357 0, /*tp_print*/
358 (getattrfunc)capture_getattr, /*tp_getattr*/
359 0, /*tp_setattr*/
360 0, /*tp_compare*/
361 0, /*tp_repr*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000362};
363
Barry Warsawf630f6b1996-12-13 01:24:29 +0000364static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000365newcaptureobject(self, ptr, mustunlock)
366 svobject *self;
367 void *ptr;
368 int mustunlock;
369{
370 captureobject *p;
371
Barry Warsawf630f6b1996-12-13 01:24:29 +0000372 p = PyObject_NEW(captureobject, &Capturetype);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000373 if (p == NULL)
374 return NULL;
375 p->ob_svideo = self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000376 Py_INCREF(self);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000377 p->ob_capture = ptr;
378 p->ob_mustunlock = mustunlock;
379 p->ob_info = self->ob_info;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000380 return (PyObject *) p;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000381}
382
Barry Warsawf630f6b1996-12-13 01:24:29 +0000383static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000384sv_GetCaptureData(self, args)
385 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000386 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000387{
388 void *ptr;
389 long fieldID;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000390 PyObject *res, *c;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000391
Barry Warsawf630f6b1996-12-13 01:24:29 +0000392 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000393 return NULL;
394
395 if (svGetCaptureData(self->ob_svideo, &ptr, &fieldID))
396 return sv_error();
397
398 if (ptr == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000399 PyErr_SetString(SvError, "no data available");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000400 return NULL;
401 }
402
403 c = newcaptureobject(self, ptr, 1);
404 if (c == NULL)
405 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000406 res = Py_BuildValue("(Oi)", c, fieldID);
407 Py_DECREF(c);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000408 return res;
409}
410
Barry Warsawf630f6b1996-12-13 01:24:29 +0000411static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000412sv_BindGLWindow(self, args)
413 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000414 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000415{
416 long wid;
417 int mode;
418
Barry Warsawf630f6b1996-12-13 01:24:29 +0000419 if (!PyArg_Parse(args, "(ii)", &wid, &mode))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000420 return NULL;
421
422 if (svBindGLWindow(self->ob_svideo, wid, mode))
423 return sv_error();
424
Barry Warsawf630f6b1996-12-13 01:24:29 +0000425 Py_INCREF(Py_None);
426 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000427}
428
Barry Warsawf630f6b1996-12-13 01:24:29 +0000429static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000430sv_EndContinuousCapture(self, args)
431 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000432 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000433{
434
Barry Warsawf630f6b1996-12-13 01:24:29 +0000435 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000436 return NULL;
437
438 if (svEndContinuousCapture(self->ob_svideo))
439 return sv_error();
440
Barry Warsawf630f6b1996-12-13 01:24:29 +0000441 Py_INCREF(Py_None);
442 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000443}
444
Barry Warsawf630f6b1996-12-13 01:24:29 +0000445static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000446sv_IsVideoDisplayed(self, args)
447 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000448 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000449{
450 int v;
451
Barry Warsawf630f6b1996-12-13 01:24:29 +0000452 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000453 return NULL;
454
455 v = svIsVideoDisplayed(self->ob_svideo);
456 if (v == -1)
457 return sv_error();
458
Barry Warsawf630f6b1996-12-13 01:24:29 +0000459 return PyInt_FromLong((long) v);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000460}
461
Barry Warsawf630f6b1996-12-13 01:24:29 +0000462static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000463sv_OutputOffset(self, args)
464 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000465 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000466{
467 int x_offset;
468 int y_offset;
469
Barry Warsawf630f6b1996-12-13 01:24:29 +0000470 if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000471 return NULL;
472
473 if (svOutputOffset(self->ob_svideo, x_offset, y_offset))
474 return sv_error();
475
Barry Warsawf630f6b1996-12-13 01:24:29 +0000476 Py_INCREF(Py_None);
477 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000478}
479
Barry Warsawf630f6b1996-12-13 01:24:29 +0000480static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000481sv_PutFrame(self, args)
482 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000483 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000484{
485 char *buffer;
486
Barry Warsawf630f6b1996-12-13 01:24:29 +0000487 if (!PyArg_Parse(args, "s", &buffer))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000488 return NULL;
489
490 if (svPutFrame(self->ob_svideo, buffer))
491 return sv_error();
492
Barry Warsawf630f6b1996-12-13 01:24:29 +0000493 Py_INCREF(Py_None);
494 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000495}
496
Barry Warsawf630f6b1996-12-13 01:24:29 +0000497static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000498sv_QuerySize(self, args)
499 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000500 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000501{
502 int w;
503 int h;
504 int rw;
505 int rh;
506
Barry Warsawf630f6b1996-12-13 01:24:29 +0000507 if (!PyArg_Parse(args, "(ii)", &w, &h))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000508 return NULL;
509
510 if (svQuerySize(self->ob_svideo, w, h, &rw, &rh))
511 return sv_error();
512
Barry Warsawf630f6b1996-12-13 01:24:29 +0000513 return Py_BuildValue("(ii)", (long) rw, (long) rh);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000514}
515
Barry Warsawf630f6b1996-12-13 01:24:29 +0000516static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000517sv_SetSize(self, args)
518 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000519 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000520{
521 int w;
522 int h;
523
Barry Warsawf630f6b1996-12-13 01:24:29 +0000524 if (!PyArg_Parse(args, "(ii)", &w, &h))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000525 return NULL;
526
527 if (svSetSize(self->ob_svideo, w, h))
528 return sv_error();
529
Barry Warsawf630f6b1996-12-13 01:24:29 +0000530 Py_INCREF(Py_None);
531 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000532}
533
Barry Warsawf630f6b1996-12-13 01:24:29 +0000534static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000535sv_SetStdDefaults(self, args)
536 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000537 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000538{
539
Barry Warsawf630f6b1996-12-13 01:24:29 +0000540 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000541 return NULL;
542
543 if (svSetStdDefaults(self->ob_svideo))
544 return sv_error();
545
Barry Warsawf630f6b1996-12-13 01:24:29 +0000546 Py_INCREF(Py_None);
547 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000548}
549
Barry Warsawf630f6b1996-12-13 01:24:29 +0000550static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000551sv_UseExclusive(self, args)
552 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000553 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000554{
555 boolean onoff;
556 int mode;
557
Barry Warsawf630f6b1996-12-13 01:24:29 +0000558 if (!PyArg_Parse(args, "(ii)", &onoff, &mode))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000559 return NULL;
560
561 if (svUseExclusive(self->ob_svideo, onoff, mode))
562 return sv_error();
563
Barry Warsawf630f6b1996-12-13 01:24:29 +0000564 Py_INCREF(Py_None);
565 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000566}
567
Barry Warsawf630f6b1996-12-13 01:24:29 +0000568static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000569sv_WindowOffset(self, args)
570 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000571 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000572{
573 int x_offset;
574 int y_offset;
575
Barry Warsawf630f6b1996-12-13 01:24:29 +0000576 if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000577 return NULL;
578
579 if (svWindowOffset(self->ob_svideo, x_offset, y_offset))
580 return sv_error();
581
Barry Warsawf630f6b1996-12-13 01:24:29 +0000582 Py_INCREF(Py_None);
583 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000584}
585
Barry Warsawf630f6b1996-12-13 01:24:29 +0000586static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000587sv_CaptureBurst(self, args)
588 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000589 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000590{
591 int bytes, i;
592 svCaptureInfo info;
593 void *bitvector = NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000594 PyObject *videodata = NULL;
595 PyObject *bitvecobj = NULL;
596 PyObject* *res = NULL;
597 static PyObject *evenitem, *odditem;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000598
Barry Warsawf630f6b1996-12-13 01:24:29 +0000599 if (!PyArg_Parse(args, "(iiiii)", &info.format,
600 &info.width, &info.height,
601 &info.size, &info.samplingrate))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000602 return NULL;
603
604 switch (info.format) {
605 case SV_RGB8_FRAMES:
606 bitvector = malloc(SV_BITVEC_SIZE(info.size));
607 break;
608 case SV_YUV411_FRAMES_AND_BLANKING_BUFFER:
609 break;
610 default:
Barry Warsawf630f6b1996-12-13 01:24:29 +0000611 PyErr_SetString(SvError, "illegal format specified");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000612 return NULL;
613 }
614
615 if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes)) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000616 res = sv_error();
617 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000618 }
619
Barry Warsawf630f6b1996-12-13 01:24:29 +0000620 if (!(videodata = PyString_FromStringAndSize(NULL, bytes)))
621 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000622
623 /* XXX -- need to do something about the bitvector */
Barry Warsawf630f6b1996-12-13 01:24:29 +0000624 {
625 char* str = PyString_AsString(videodata);
626 if (!str)
627 goto finally;
628
629 if (svCaptureBurst(self->ob_svideo, &info, str, bitvector)) {
630 res = sv_error();
631 goto finally;
632 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000633 }
634
635 if (bitvector) {
636 if (evenitem == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000637 if (!(evenitem = PyInt_FromLong(0)))
638 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000639 }
640 if (odditem == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000641 if (!(odditem = PyInt_FromLong(1)))
642 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000643 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000644 if (!(bitvecobj = PyTuple_New(2 * info.size)))
645 goto finally;
646
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000647 for (i = 0; i < 2 * info.size; i++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000648 int sts;
649
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000650 if (SV_GET_FIELD(bitvector, i) == SV_EVEN_FIELD) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000651 Py_INCREF(evenitem);
652 sts = PyTuple_SetItem(bitvecobj, i, evenitem);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000653 } else {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000654 Py_INCREF(odditem);
655 sts = PyTuple_SetItem(bitvecobj, i, odditem);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000656 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000657 if (sts < 0)
658 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000659 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000660 } else {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000661 bitvecobj = Py_None;
662 Py_INCREF(Py_None);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000663 }
664
Barry Warsawf630f6b1996-12-13 01:24:29 +0000665 res = Py_BuildValue("((iiiii)OO)", info.format,
666 info.width, info.height,
667 info.size, info.samplingrate,
668 videodata, bitvecobj);
669
670 finally:
671 if (bitvector)
672 free(bitvector);
673
674 Py_XDECREF(videodata);
675 Py_XDECREF(bitvecobj);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000676 return res;
677}
678
Barry Warsawf630f6b1996-12-13 01:24:29 +0000679static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000680sv_CaptureOneFrame(self, args)
681 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000682 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000683{
684 svCaptureInfo info;
685 int format, width, height;
686 int bytes;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000687 PyObject *videodata = NULL;
688 PyObject *res = NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000689
Barry Warsawf630f6b1996-12-13 01:24:29 +0000690 if (!PyArg_Parse(args, "(iii)", &format, &width, &height))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000691 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000692
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000693 info.format = format;
694 info.width = width;
695 info.height = height;
696 info.size = 0;
697 info.samplingrate = 0;
698 if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes))
699 return sv_error();
Barry Warsawf630f6b1996-12-13 01:24:29 +0000700
701 if (!(videodata = PyString_FromStringAndSize(NULL, bytes)))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000702 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000703
704 {
705 char* str = PyString_AsString(videodata);
706 if (!str)
707 goto finally;
708
709 if (svCaptureOneFrame(self->ob_svideo, format, &width, &height, str)) {
710 res = sv_error();
711 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000712 }
713
Barry Warsawf630f6b1996-12-13 01:24:29 +0000714 res = Py_BuildValue("(iiO)", width, height, videodata);
715
716 finally:
717 Py_XDECREF(videodata);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000718 return res;
719}
720
Barry Warsawf630f6b1996-12-13 01:24:29 +0000721static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000722sv_InitContinuousCapture(self, args)
723 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000724 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000725{
726 svCaptureInfo info;
727
Barry Warsawf630f6b1996-12-13 01:24:29 +0000728 if (!PyArg_Parse(args, "(iiiii)", &info.format,
729 &info.width, &info.height,
730 &info.size, &info.samplingrate))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000731 return NULL;
732
733 if (svInitContinuousCapture(self->ob_svideo, &info))
734 return sv_error();
735
736 self->ob_info = info;
737
Barry Warsawf630f6b1996-12-13 01:24:29 +0000738 return Py_BuildValue("(iiiii)", info.format, info.width, info.height,
739 info.size, info.samplingrate);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000740}
741
Barry Warsawf630f6b1996-12-13 01:24:29 +0000742static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000743sv_LoadMap(self, args)
744 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000745 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000746{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000747 PyObject *rgb;
748 PyObject *res = NULL;
749 rgb_tuple *mapp = NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000750 int maptype;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000751 int i, j; /* indices */
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000752
Barry Warsawf630f6b1996-12-13 01:24:29 +0000753 if (!PyArg_Parse(args, "(iO)", &maptype, &rgb))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000754 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000755
756 if (!PyList_Check(rgb) || PyList_Size(rgb) != 256) {
757 PyErr_BadArgument();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000758 return NULL;
759 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000760
761 if (!(mapp = PyMem_NEW(rgb_tuple, 256)))
762 return PyErr_NoMemory();
763
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000764 for (i = 0; i < 256; i++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000765 PyObject* v = PyList_GetItem(rgb, i);
766 if (!v)
767 goto finally;
768
769 if (!PyTuple_Check(v) || PyTuple_Size(v) != 3) {
770 PyErr_BadArgument();
771 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000772 }
773 for (j = 0; j < 3; j++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000774 PyObject* cell = PyTuple_GetItem(v, j);
775 if (!cell)
776 goto finally;
777
778 if (!PyInt_Check(cell)) {
779 PyErr_BadArgument();
780 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000781 }
782 switch (j) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000783 case 0: mapp[i].red = PyInt_AsLong(cell); break;
784 case 1: mapp[i].blue = PyInt_AsLong(cell); break;
785 case 2: mapp[i].green = PyInt_AsLong(cell); break;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000786 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000787 if (PyErr_Occurred())
788 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000789 }
790 }
791
792 if (svLoadMap(self->ob_svideo, maptype, mapp)) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000793 res = sv_error();
794 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000795 }
796
Barry Warsawf630f6b1996-12-13 01:24:29 +0000797 Py_INCREF(Py_None);
798 res = Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000799
Barry Warsawf630f6b1996-12-13 01:24:29 +0000800 finally:
801 PyMem_DEL(mapp);
802 return res;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000803}
804
Barry Warsawf630f6b1996-12-13 01:24:29 +0000805static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000806sv_CloseVideo(self, args)
807 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000808 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000809{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000810 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000811 return NULL;
812
813 if (svCloseVideo(self->ob_svideo))
814 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000815
Barry Warsawf630f6b1996-12-13 01:24:29 +0000816 self->ob_svideo = NULL;
817 Py_INCREF(Py_None);
818 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000819}
820
Barry Warsawf630f6b1996-12-13 01:24:29 +0000821static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000822doParams(self, args, func, modified)
823 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000824 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000825 int (*func)(SV_nodeP, long *, int);
826 int modified;
827{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000828 PyObject *list;
829 PyObject *res = NULL;
830 long *PVbuffer = NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000831 long length;
832 int i;
833
Barry Warsawf630f6b1996-12-13 01:24:29 +0000834 if (!PyArg_Parse(args, "O", &list))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000835 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000836
837 if (!PyList_Check(list)) {
838 PyErr_BadArgument();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000839 return NULL;
840 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000841
842 if ((length = PyList_Size(list)) < 0)
843 return NULL;
844
845 PVbuffer = PyMem_NEW(long, length);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000846 if (PVbuffer == NULL)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000847 return PyErr_NoMemory();
848
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000849 for (i = 0; i < length; i++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000850 PyObject *v = PyList_GetItem(list, i);
851 if (!v)
852 goto finally;
853
854 if (!PyInt_Check(v)) {
855 PyErr_BadArgument();
856 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000857 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000858 PVbuffer[i] = PyInt_AsLong(v);
859 /* can't just test the return value, because what if the
860 value was -1?!
861 */
862 if (PVbuffer[i] == -1 && PyErr_Occurred())
863 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000864 }
865
866 if ((*func)(self->ob_svideo, PVbuffer, length)) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000867 res = sv_error();
868 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000869 }
870
871 if (modified) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000872 for (i = 0; i < length; i++) {
873 PyObject* v = PyInt_FromLong(PVbuffer[i]);
874 if (!v || PyList_SetItem(list, i, v) < 0)
875 goto finally;
876 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000877 }
878
Barry Warsawf630f6b1996-12-13 01:24:29 +0000879 Py_INCREF(Py_None);
880 res = Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000881
Barry Warsawf630f6b1996-12-13 01:24:29 +0000882 finally:
883 PyMem_DEL(PVbuffer);
884 return res;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000885}
886
Barry Warsawf630f6b1996-12-13 01:24:29 +0000887static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000888sv_GetParam(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000889 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000890{
891 return doParams(self, args, svGetParam, 1);
892}
893
Barry Warsawf630f6b1996-12-13 01:24:29 +0000894static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000895sv_GetParamRange(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000896 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000897{
898 return doParams(self, args, svGetParamRange, 1);
899}
900
Barry Warsawf630f6b1996-12-13 01:24:29 +0000901static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000902sv_SetParam(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000903 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000904{
905 return doParams(self, args, svSetParam, 0);
906}
907
Barry Warsawf630f6b1996-12-13 01:24:29 +0000908static PyMethodDef svideo_methods[] = {
909 {"BindGLWindow", (PyCFunction)sv_BindGLWindow},
910 {"EndContinuousCapture",(PyCFunction)sv_EndContinuousCapture},
911 {"IsVideoDisplayed", (PyCFunction)sv_IsVideoDisplayed},
912 {"OutputOffset", (PyCFunction)sv_OutputOffset},
913 {"PutFrame", (PyCFunction)sv_PutFrame},
914 {"QuerySize", (PyCFunction)sv_QuerySize},
915 {"SetSize", (PyCFunction)sv_SetSize},
916 {"SetStdDefaults", (PyCFunction)sv_SetStdDefaults},
917 {"UseExclusive", (PyCFunction)sv_UseExclusive},
918 {"WindowOffset", (PyCFunction)sv_WindowOffset},
919 {"InitContinuousCapture",(PyCFunction)sv_InitContinuousCapture},
920 {"CaptureBurst", (PyCFunction)sv_CaptureBurst},
921 {"CaptureOneFrame", (PyCFunction)sv_CaptureOneFrame},
922 {"GetCaptureData", (PyCFunction)sv_GetCaptureData},
923 {"CloseVideo", (PyCFunction)sv_CloseVideo},
924 {"LoadMap", (PyCFunction)sv_LoadMap},
925 {"GetParam", (PyCFunction)sv_GetParam},
926 {"GetParamRange", (PyCFunction)sv_GetParamRange},
927 {"SetParam", (PyCFunction)sv_SetParam},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000928 {NULL, NULL} /* sentinel */
929};
930
Barry Warsawf630f6b1996-12-13 01:24:29 +0000931static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000932sv_conversion(self, args, function, inputfactor, factor)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000933 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000934 void (*function)();
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000935 int inputfactor;
936 float factor;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000937{
938 int invert, width, height, inputlength;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000939 char *input, *str;
940 PyObject *output;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000941
Barry Warsawf630f6b1996-12-13 01:24:29 +0000942 if (!PyArg_Parse(args, "(is#ii)", &invert,
943 &input, &inputlength, &width, &height))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000944 return NULL;
945
946 if (width * height * inputfactor > inputlength) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000947 PyErr_SetString(SvError, "input buffer not long enough");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000948 return NULL;
949 }
950
Barry Warsawf630f6b1996-12-13 01:24:29 +0000951 if (!(output = PyString_FromStringAndSize(NULL,
952 (int)(width * height * factor))))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000953 return NULL;
954
Barry Warsawf630f6b1996-12-13 01:24:29 +0000955 str = PyString_AsString(output);
956 if (!str) {
957 Py_DECREF(output);
958 return NULL;
959 }
960 (*function)(invert, input, str, width, height);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000961
962 return output;
963}
964
Barry Warsawf630f6b1996-12-13 01:24:29 +0000965static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000966sv_InterleaveFields(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000967 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000968{
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000969 return sv_conversion(self, args, svInterleaveFields, 1, 1.0);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000970}
971
Barry Warsawf630f6b1996-12-13 01:24:29 +0000972static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000973sv_RGB8toRGB32(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000974 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000975{
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000976 return sv_conversion(self, args, svRGB8toRGB32, 1, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000977}
978
Barry Warsawf630f6b1996-12-13 01:24:29 +0000979static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000980sv_YUVtoRGB(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000981 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000982{
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000983 return sv_conversion(self, args, svYUVtoRGB, 2, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000984}
985
986static void
987svideo_dealloc(self)
988 svobject *self;
989{
990 if (self->ob_svideo != NULL)
991 (void) svCloseVideo(self->ob_svideo);
Barry Warsawf630f6b1996-12-13 01:24:29 +0000992 PyMem_DEL(self);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000993}
994
Barry Warsawf630f6b1996-12-13 01:24:29 +0000995static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000996svideo_getattr(self, name)
997 svobject *self;
998 char *name;
999{
Barry Warsawf630f6b1996-12-13 01:24:29 +00001000 return Py_FindMethod(svideo_methods, (PyObject *)self, name);
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001001}
1002
Barry Warsawf630f6b1996-12-13 01:24:29 +00001003PyTypeObject Svtype = {
1004 PyObject_HEAD_INIT(&PyType_Type)
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001005 0, /*ob_size*/
1006 "sv", /*tp_name*/
1007 sizeof(svobject), /*tp_size*/
1008 0, /*tp_itemsize*/
1009 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001010 (destructor)svideo_dealloc, /*tp_dealloc*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001011 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001012 (getattrfunc)svideo_getattr, /*tp_getattr*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001013 0, /*tp_setattr*/
1014 0, /*tp_compare*/
1015 0, /*tp_repr*/
1016};
1017
Barry Warsawf630f6b1996-12-13 01:24:29 +00001018static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001019newsvobject(svp)
1020 SV_nodeP svp;
1021{
1022 svobject *p;
1023
Barry Warsawf630f6b1996-12-13 01:24:29 +00001024 p = PyObject_NEW(svobject, &Svtype);
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001025 if (p == NULL)
1026 return NULL;
1027 p->ob_svideo = svp;
1028 p->ob_info.format = 0;
1029 p->ob_info.size = 0;
1030 p->ob_info.width = 0;
1031 p->ob_info.height = 0;
1032 p->ob_info.samplingrate = 0;
Barry Warsawf630f6b1996-12-13 01:24:29 +00001033 return (PyObject *) p;
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001034}
1035
Barry Warsawf630f6b1996-12-13 01:24:29 +00001036static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001037sv_OpenVideo(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +00001038 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001039{
1040 SV_nodeP svp;
1041
Barry Warsawf630f6b1996-12-13 01:24:29 +00001042 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001043 return NULL;
1044
1045 svp = svOpenVideo();
1046 if (svp == NULL)
1047 return sv_error();
1048
1049 return newsvobject(svp);
1050}
1051
Barry Warsawf630f6b1996-12-13 01:24:29 +00001052static PyMethodDef sv_methods[] = {
1053 {"InterleaveFields", (PyCFunction)sv_InterleaveFields},
1054 {"RGB8toRGB32", (PyCFunction)sv_RGB8toRGB32},
1055 {"YUVtoRGB", (PyCFunction)sv_YUVtoRGB},
1056 {"OpenVideo", (PyCFunction)sv_OpenVideo},
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001057 {NULL, NULL} /* Sentinel */
1058};
1059
1060void
1061initsv()
1062{
Barry Warsawf630f6b1996-12-13 01:24:29 +00001063 PyObject *m, *d;
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001064
Barry Warsawf630f6b1996-12-13 01:24:29 +00001065 m = Py_InitModule("sv", sv_methods);
1066 d = PyModule_GetDict(m);
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001067
Barry Warsawf630f6b1996-12-13 01:24:29 +00001068 SvError = PyString_FromString("sv.error");
1069 if (SvError == NULL || PyDict_SetItemString(d, "error", SvError) != 0)
1070 Py_FatalError("can't define sv.error");
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001071}