blob: 95b3e142adca2004a8545483d2505a87c3d2b5bd [file] [log] [blame]
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001/**********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Sjoerd Mullendered59d201993-01-06 13:36:38 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Sjoerd Mullendered59d201993-01-06 13:36:38 +00009******************************************************************/
10
11/* SV module -- interface to the Indigo video board */
12
Guido van Rossumc88c9cb1996-12-17 20:43:55 +000013/* WARNING! This module is for hardware that we don't have any more,
14 so it hasn't been tested. It has been converted to the new coding
15 style, and it is possible that this conversion has broken something
16 -- user beware! */
17
Sjoerd Mullendered59d201993-01-06 13:36:38 +000018#include <sys/time.h>
19#include <svideo.h>
Barry Warsawf630f6b1996-12-13 01:24:29 +000020#include "Python.h"
Sjoerd Mullendered59d201993-01-06 13:36:38 +000021#include "compile.h"
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000022#include "yuv.h" /* for YUV conversion functions */
Sjoerd Mullendered59d201993-01-06 13:36:38 +000023
24typedef struct {
Barry Warsawf630f6b1996-12-13 01:24:29 +000025 PyObject_HEAD
Sjoerd Mullendered59d201993-01-06 13:36:38 +000026 SV_nodeP ob_svideo;
27 svCaptureInfo ob_info;
28} svobject;
29
30typedef struct {
Barry Warsawf630f6b1996-12-13 01:24:29 +000031 PyObject_HEAD
Sjoerd Mullendered59d201993-01-06 13:36:38 +000032 void *ob_capture;
33 int ob_mustunlock;
34 svCaptureInfo ob_info;
35 svobject *ob_svideo;
36} captureobject;
37
Barry Warsawf630f6b1996-12-13 01:24:29 +000038static PyObject *SvError; /* exception sv.error */
Sjoerd Mullendered59d201993-01-06 13:36:38 +000039
Tim Petersdbd9ba62000-07-09 03:09:57 +000040static PyObject *newcaptureobject(svobject *, void *, int);
Sjoerd Mullendered59d201993-01-06 13:36:38 +000041
42/* Set a SV-specific error from svideo_errno and return NULL */
Barry Warsawf630f6b1996-12-13 01:24:29 +000043static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000044sv_error(void)
Sjoerd Mullendered59d201993-01-06 13:36:38 +000045{
Barry Warsawf630f6b1996-12-13 01:24:29 +000046 PyErr_SetString(SvError, svStrerror(svideo_errno));
Sjoerd Mullendered59d201993-01-06 13:36:38 +000047 return NULL;
48}
49
Barry Warsawf630f6b1996-12-13 01:24:29 +000050static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +000051svc_conversion(captureobject *self, PyObject *args, void (*function)(), float factor)
Sjoerd Mullendered59d201993-01-06 13:36:38 +000052{
Barry Warsawf630f6b1996-12-13 01:24:29 +000053 PyObject *output;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000054 int invert;
Barry Warsawf630f6b1996-12-13 01:24:29 +000055 char* outstr;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000056
Barry Warsawf630f6b1996-12-13 01:24:29 +000057 if (!PyArg_Parse(args, "i", &invert))
Sjoerd Mullendered59d201993-01-06 13:36:38 +000058 return NULL;
59
Barry Warsawf630f6b1996-12-13 01:24:29 +000060 if (!(output = PyString_FromStringAndSize(
61 NULL,
62 (int)(self->ob_info.width * self->ob_info.height * factor))))
63 {
Sjoerd Mullendered59d201993-01-06 13:36:38 +000064 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +000065 }
66 if (!(outstr = PyString_AsString(output))) {
67 Py_DECREF(output);
68 return NULL;
69 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +000070
Barry Warsawf630f6b1996-12-13 01:24:29 +000071 (*function)((boolean)invert, self->ob_capture,
72 outstr,
Sjoerd Mullendered59d201993-01-06 13:36:38 +000073 self->ob_info.width, self->ob_info.height);
74
75 return output;
76}
77
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000078/*
79 * 3 functions to convert from Starter Video YUV 4:1:1 format to
80 * Compression Library 4:2:2 Duplicate Chroma format.
81 */
Barry Warsawf630f6b1996-12-13 01:24:29 +000082static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +000083svc_YUVtoYUV422DC(captureobject *self, PyObject *args)
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000084{
85 if (self->ob_info.format != SV_YUV411_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +000086 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000087 return NULL;
88 }
89 return svc_conversion(self, args, yuv_sv411_to_cl422dc, 2.0);
90}
91
Barry Warsawf630f6b1996-12-13 01:24:29 +000092static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +000093svc_YUVtoYUV422DC_quarter(captureobject *self, PyObject *args)
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000094{
95 if (self->ob_info.format != SV_YUV411_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +000096 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000097 return NULL;
98 }
Barry Warsawf630f6b1996-12-13 01:24:29 +000099 return svc_conversion(self, args,
100 yuv_sv411_to_cl422dc_quartersize, 0.5);
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000101}
102
Barry Warsawf630f6b1996-12-13 01:24:29 +0000103static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000104svc_YUVtoYUV422DC_sixteenth(captureobject *self, PyObject *args)
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000105{
106 if (self->ob_info.format != SV_YUV411_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000107 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000108 return NULL;
109 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000110 return svc_conversion(self, args,
111 yuv_sv411_to_cl422dc_sixteenthsize, 0.125);
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000112}
113
Barry Warsawf630f6b1996-12-13 01:24:29 +0000114static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000115svc_YUVtoRGB(captureobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000116{
117 switch (self->ob_info.format) {
118 case SV_YUV411_FRAMES:
119 case SV_YUV411_FRAMES_AND_BLANKING_BUFFER:
120 break;
121 default:
Barry Warsawf630f6b1996-12-13 01:24:29 +0000122 PyErr_SetString(SvError, "data had bad format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000123 return NULL;
124 }
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000125 return svc_conversion(self, args, svYUVtoRGB, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000126}
127
Barry Warsawf630f6b1996-12-13 01:24:29 +0000128static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000129svc_RGB8toRGB32(captureobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000130{
131 if (self->ob_info.format != SV_RGB8_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000132 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000133 return NULL;
134 }
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000135 return svc_conversion(self, args, svRGB8toRGB32, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000136}
137
Barry Warsawf630f6b1996-12-13 01:24:29 +0000138static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000139svc_InterleaveFields(captureobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000140{
141 if (self->ob_info.format != SV_RGB8_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000142 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000143 return NULL;
144 }
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000145 return svc_conversion(self, args, svInterleaveFields, 1.0);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000146}
147
Barry Warsawf630f6b1996-12-13 01:24:29 +0000148static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000149svc_GetFields(captureobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000150{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000151 PyObject *f1 = NULL;
152 PyObject *f2 = NULL;
153 PyObject *ret = NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000154 int fieldsize;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000155 char* obcapture;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000156
157 if (self->ob_info.format != SV_RGB8_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000158 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000159 return NULL;
160 }
161
162 fieldsize = self->ob_info.width * self->ob_info.height / 2;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000163 obcapture = (char*)self->ob_capture;
164
165 if (!(f1 = PyString_FromStringAndSize(obcapture, fieldsize)))
166 goto finally;
167 if (!(f2 = PyString_FromStringAndSize(obcapture + fieldsize,
168 fieldsize)))
169 goto finally;
170 ret = Py_BuildValue("(OO)", f1, f2);
171
172 finally:
173 Py_XDECREF(f1);
174 Py_XDECREF(f2);
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000175 return ret;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000176}
177
Barry Warsawf630f6b1996-12-13 01:24:29 +0000178static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000179svc_UnlockCaptureData(captureobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000180{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000181 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000182 return NULL;
183
184 if (!self->ob_mustunlock) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000185 PyErr_SetString(SvError, "buffer should not be unlocked");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000186 return NULL;
187 }
188
189 if (svUnlockCaptureData(self->ob_svideo->ob_svideo, self->ob_capture))
190 return sv_error();
191
192 self->ob_mustunlock = 0;
193
Barry Warsawf630f6b1996-12-13 01:24:29 +0000194 Py_INCREF(Py_None);
195 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000196}
197
198#ifdef USE_GL
199#include <gl.h>
200
Barry Warsawf630f6b1996-12-13 01:24:29 +0000201static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000202svc_lrectwrite(captureobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000203{
204 Screencoord x1, x2, y1, y2;
205
Barry Warsawf630f6b1996-12-13 01:24:29 +0000206 if (!PyArg_Parse(args, "(hhhh)", &x1, &x2, &y1, &y2))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000207 return NULL;
208
209 lrectwrite(x1, x2, y1, y2, (unsigned long *) self->ob_capture);
210
Barry Warsawf630f6b1996-12-13 01:24:29 +0000211 Py_INCREF(Py_None);
212 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000213}
214#endif
215
Barry Warsawf630f6b1996-12-13 01:24:29 +0000216static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000217svc_writefile(captureobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000218{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000219 PyObject *file;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000220 int size;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000221 FILE* fp;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000222
Barry Warsawf630f6b1996-12-13 01:24:29 +0000223 if (!PyArg_Parse(args, "O", &file))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000224 return NULL;
225
Barry Warsawf630f6b1996-12-13 01:24:29 +0000226 if (!PyFile_Check(file)) {
227 PyErr_SetString(SvError, "not a file object");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000228 return NULL;
229 }
230
Barry Warsawf630f6b1996-12-13 01:24:29 +0000231 if (!(fp = PyFile_AsFile(file)))
232 return NULL;
233
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000234 size = self->ob_info.width * self->ob_info.height;
235
Barry Warsawf630f6b1996-12-13 01:24:29 +0000236 if (fwrite(self->ob_capture, sizeof(long), size, fp) != size) {
237 PyErr_SetString(SvError, "writing failed");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000238 return NULL;
239 }
240
Barry Warsawf630f6b1996-12-13 01:24:29 +0000241 Py_INCREF(Py_None);
242 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000243}
244
Barry Warsawf630f6b1996-12-13 01:24:29 +0000245static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000246svc_FindVisibleRegion(captureobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000247{
248 void *visible;
249 int width;
250
Barry Warsawf630f6b1996-12-13 01:24:29 +0000251 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000252 return NULL;
253
Barry Warsawf630f6b1996-12-13 01:24:29 +0000254 if (svFindVisibleRegion(self->ob_svideo->ob_svideo,
255 self->ob_capture, &visible,
256 self->ob_info.width))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000257 return sv_error();
258
259 if (visible == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000260 PyErr_SetString(SvError, "data in wrong format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000261 return NULL;
262 }
263
264 return newcaptureobject(self->ob_svideo, visible, 0);
265}
266
Barry Warsawf630f6b1996-12-13 01:24:29 +0000267static PyMethodDef capture_methods[] = {
268 {"YUVtoRGB", (PyCFunction)svc_YUVtoRGB},
269 {"RGB8toRGB32", (PyCFunction)svc_RGB8toRGB32},
270 {"InterleaveFields", (PyCFunction)svc_InterleaveFields},
271 {"UnlockCaptureData", (PyCFunction)svc_UnlockCaptureData},
272 {"FindVisibleRegion", (PyCFunction)svc_FindVisibleRegion},
273 {"GetFields", (PyCFunction)svc_GetFields},
274 {"YUVtoYUV422DC", (PyCFunction)svc_YUVtoYUV422DC},
275 {"YUVtoYUV422DC_quarter",(PyCFunction)svc_YUVtoYUV422DC_quarter},
276 {"YUVtoYUV422DC_sixteenth",(PyCFunction)svc_YUVtoYUV422DC_sixteenth},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000277#ifdef USE_GL
Barry Warsawf630f6b1996-12-13 01:24:29 +0000278 {"lrectwrite", (PyCFunction)svc_lrectwrite},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000279#endif
Barry Warsawf630f6b1996-12-13 01:24:29 +0000280 {"writefile", (PyCFunction)svc_writefile},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000281 {NULL, NULL} /* sentinel */
282};
283
284static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000285capture_dealloc(captureobject *self)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000286{
287 if (self->ob_capture != NULL) {
288 if (self->ob_mustunlock)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000289 (void)svUnlockCaptureData(self->ob_svideo->ob_svideo,
290 self->ob_capture);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000291 self->ob_capture = NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000292 Py_DECREF(self->ob_svideo);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000293 self->ob_svideo = NULL;
294 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000295 PyObject_Del(self);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000296}
297
Barry Warsawf630f6b1996-12-13 01:24:29 +0000298static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000299capture_getattr(svobject *self, char *name)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000300{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000301 return Py_FindMethod(capture_methods, (PyObject *)self, name);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000302}
303
Barry Warsawf630f6b1996-12-13 01:24:29 +0000304PyTypeObject Capturetype = {
305 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000306 0, /*ob_size*/
307 "capture", /*tp_name*/
308 sizeof(captureobject), /*tp_size*/
309 0, /*tp_itemsize*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000310 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000311 (destructor)capture_dealloc, /*tp_dealloc*/
312 0, /*tp_print*/
313 (getattrfunc)capture_getattr, /*tp_getattr*/
314 0, /*tp_setattr*/
315 0, /*tp_compare*/
316 0, /*tp_repr*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000317};
318
Barry Warsawf630f6b1996-12-13 01:24:29 +0000319static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000320newcaptureobject(svobject *self, void *ptr, int mustunlock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000321{
322 captureobject *p;
323
Guido van Rossumb18618d2000-05-03 23:44:39 +0000324 p = PyObject_New(captureobject, &Capturetype);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000325 if (p == NULL)
326 return NULL;
327 p->ob_svideo = self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000328 Py_INCREF(self);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000329 p->ob_capture = ptr;
330 p->ob_mustunlock = mustunlock;
331 p->ob_info = self->ob_info;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000332 return (PyObject *) p;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000333}
334
Barry Warsawf630f6b1996-12-13 01:24:29 +0000335static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000336sv_GetCaptureData(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000337{
338 void *ptr;
339 long fieldID;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000340 PyObject *res, *c;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000341
Barry Warsawf630f6b1996-12-13 01:24:29 +0000342 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000343 return NULL;
344
345 if (svGetCaptureData(self->ob_svideo, &ptr, &fieldID))
346 return sv_error();
347
348 if (ptr == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000349 PyErr_SetString(SvError, "no data available");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000350 return NULL;
351 }
352
353 c = newcaptureobject(self, ptr, 1);
354 if (c == NULL)
355 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000356 res = Py_BuildValue("(Oi)", c, fieldID);
357 Py_DECREF(c);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000358 return res;
359}
360
Barry Warsawf630f6b1996-12-13 01:24:29 +0000361static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000362sv_BindGLWindow(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000363{
364 long wid;
365 int mode;
366
Barry Warsawf630f6b1996-12-13 01:24:29 +0000367 if (!PyArg_Parse(args, "(ii)", &wid, &mode))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000368 return NULL;
369
370 if (svBindGLWindow(self->ob_svideo, wid, mode))
371 return sv_error();
372
Barry Warsawf630f6b1996-12-13 01:24:29 +0000373 Py_INCREF(Py_None);
374 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000375}
376
Barry Warsawf630f6b1996-12-13 01:24:29 +0000377static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000378sv_EndContinuousCapture(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000379{
380
Barry Warsawf630f6b1996-12-13 01:24:29 +0000381 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000382 return NULL;
383
384 if (svEndContinuousCapture(self->ob_svideo))
385 return sv_error();
386
Barry Warsawf630f6b1996-12-13 01:24:29 +0000387 Py_INCREF(Py_None);
388 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000389}
390
Barry Warsawf630f6b1996-12-13 01:24:29 +0000391static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000392sv_IsVideoDisplayed(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000393{
394 int v;
395
Barry Warsawf630f6b1996-12-13 01:24:29 +0000396 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000397 return NULL;
398
399 v = svIsVideoDisplayed(self->ob_svideo);
400 if (v == -1)
401 return sv_error();
402
Barry Warsawf630f6b1996-12-13 01:24:29 +0000403 return PyInt_FromLong((long) v);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000404}
405
Barry Warsawf630f6b1996-12-13 01:24:29 +0000406static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000407sv_OutputOffset(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000408{
409 int x_offset;
410 int y_offset;
411
Barry Warsawf630f6b1996-12-13 01:24:29 +0000412 if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000413 return NULL;
414
415 if (svOutputOffset(self->ob_svideo, x_offset, y_offset))
416 return sv_error();
417
Barry Warsawf630f6b1996-12-13 01:24:29 +0000418 Py_INCREF(Py_None);
419 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000420}
421
Barry Warsawf630f6b1996-12-13 01:24:29 +0000422static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000423sv_PutFrame(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000424{
425 char *buffer;
426
Barry Warsawf630f6b1996-12-13 01:24:29 +0000427 if (!PyArg_Parse(args, "s", &buffer))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000428 return NULL;
429
430 if (svPutFrame(self->ob_svideo, buffer))
431 return sv_error();
432
Barry Warsawf630f6b1996-12-13 01:24:29 +0000433 Py_INCREF(Py_None);
434 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000435}
436
Barry Warsawf630f6b1996-12-13 01:24:29 +0000437static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000438sv_QuerySize(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000439{
440 int w;
441 int h;
442 int rw;
443 int rh;
444
Barry Warsawf630f6b1996-12-13 01:24:29 +0000445 if (!PyArg_Parse(args, "(ii)", &w, &h))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000446 return NULL;
447
448 if (svQuerySize(self->ob_svideo, w, h, &rw, &rh))
449 return sv_error();
450
Barry Warsawf630f6b1996-12-13 01:24:29 +0000451 return Py_BuildValue("(ii)", (long) rw, (long) rh);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000452}
453
Barry Warsawf630f6b1996-12-13 01:24:29 +0000454static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000455sv_SetSize(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000456{
457 int w;
458 int h;
459
Barry Warsawf630f6b1996-12-13 01:24:29 +0000460 if (!PyArg_Parse(args, "(ii)", &w, &h))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000461 return NULL;
462
463 if (svSetSize(self->ob_svideo, w, h))
464 return sv_error();
465
Barry Warsawf630f6b1996-12-13 01:24:29 +0000466 Py_INCREF(Py_None);
467 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000468}
469
Barry Warsawf630f6b1996-12-13 01:24:29 +0000470static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000471sv_SetStdDefaults(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000472{
473
Barry Warsawf630f6b1996-12-13 01:24:29 +0000474 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000475 return NULL;
476
477 if (svSetStdDefaults(self->ob_svideo))
478 return sv_error();
479
Barry Warsawf630f6b1996-12-13 01:24:29 +0000480 Py_INCREF(Py_None);
481 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000482}
483
Barry Warsawf630f6b1996-12-13 01:24:29 +0000484static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000485sv_UseExclusive(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000486{
487 boolean onoff;
488 int mode;
489
Barry Warsawf630f6b1996-12-13 01:24:29 +0000490 if (!PyArg_Parse(args, "(ii)", &onoff, &mode))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000491 return NULL;
492
493 if (svUseExclusive(self->ob_svideo, onoff, mode))
494 return sv_error();
495
Barry Warsawf630f6b1996-12-13 01:24:29 +0000496 Py_INCREF(Py_None);
497 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000498}
499
Barry Warsawf630f6b1996-12-13 01:24:29 +0000500static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000501sv_WindowOffset(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000502{
503 int x_offset;
504 int y_offset;
505
Barry Warsawf630f6b1996-12-13 01:24:29 +0000506 if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000507 return NULL;
508
509 if (svWindowOffset(self->ob_svideo, x_offset, y_offset))
510 return sv_error();
511
Barry Warsawf630f6b1996-12-13 01:24:29 +0000512 Py_INCREF(Py_None);
513 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000514}
515
Barry Warsawf630f6b1996-12-13 01:24:29 +0000516static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000517sv_CaptureBurst(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000518{
519 int bytes, i;
520 svCaptureInfo info;
521 void *bitvector = NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000522 PyObject *videodata = NULL;
523 PyObject *bitvecobj = NULL;
Guido van Rossumc88c9cb1996-12-17 20:43:55 +0000524 PyObject *res = NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000525 static PyObject *evenitem, *odditem;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000526
Barry Warsawf630f6b1996-12-13 01:24:29 +0000527 if (!PyArg_Parse(args, "(iiiii)", &info.format,
528 &info.width, &info.height,
529 &info.size, &info.samplingrate))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000530 return NULL;
531
532 switch (info.format) {
533 case SV_RGB8_FRAMES:
534 bitvector = malloc(SV_BITVEC_SIZE(info.size));
535 break;
536 case SV_YUV411_FRAMES_AND_BLANKING_BUFFER:
537 break;
538 default:
Barry Warsawf630f6b1996-12-13 01:24:29 +0000539 PyErr_SetString(SvError, "illegal format specified");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000540 return NULL;
541 }
542
543 if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes)) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000544 res = sv_error();
545 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000546 }
547
Barry Warsawf630f6b1996-12-13 01:24:29 +0000548 if (!(videodata = PyString_FromStringAndSize(NULL, bytes)))
549 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000550
551 /* XXX -- need to do something about the bitvector */
Barry Warsawf630f6b1996-12-13 01:24:29 +0000552 {
553 char* str = PyString_AsString(videodata);
554 if (!str)
555 goto finally;
556
557 if (svCaptureBurst(self->ob_svideo, &info, str, bitvector)) {
558 res = sv_error();
559 goto finally;
560 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000561 }
562
563 if (bitvector) {
564 if (evenitem == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000565 if (!(evenitem = PyInt_FromLong(0)))
566 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000567 }
568 if (odditem == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000569 if (!(odditem = PyInt_FromLong(1)))
570 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000571 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000572 if (!(bitvecobj = PyTuple_New(2 * info.size)))
573 goto finally;
574
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000575 for (i = 0; i < 2 * info.size; i++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000576 int sts;
577
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000578 if (SV_GET_FIELD(bitvector, i) == SV_EVEN_FIELD) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000579 Py_INCREF(evenitem);
580 sts = PyTuple_SetItem(bitvecobj, i, evenitem);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000581 } else {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000582 Py_INCREF(odditem);
583 sts = PyTuple_SetItem(bitvecobj, i, odditem);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000584 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000585 if (sts < 0)
586 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000587 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000588 } else {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000589 bitvecobj = Py_None;
590 Py_INCREF(Py_None);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000591 }
592
Barry Warsawf630f6b1996-12-13 01:24:29 +0000593 res = Py_BuildValue("((iiiii)OO)", info.format,
594 info.width, info.height,
595 info.size, info.samplingrate,
596 videodata, bitvecobj);
597
598 finally:
599 if (bitvector)
600 free(bitvector);
601
602 Py_XDECREF(videodata);
603 Py_XDECREF(bitvecobj);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000604 return res;
605}
606
Barry Warsawf630f6b1996-12-13 01:24:29 +0000607static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000608sv_CaptureOneFrame(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000609{
610 svCaptureInfo info;
611 int format, width, height;
612 int bytes;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000613 PyObject *videodata = NULL;
614 PyObject *res = NULL;
Guido van Rossumc88c9cb1996-12-17 20:43:55 +0000615 char *str;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000616
Barry Warsawf630f6b1996-12-13 01:24:29 +0000617 if (!PyArg_Parse(args, "(iii)", &format, &width, &height))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000618 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000619
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000620 info.format = format;
621 info.width = width;
622 info.height = height;
623 info.size = 0;
624 info.samplingrate = 0;
625 if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes))
626 return sv_error();
Barry Warsawf630f6b1996-12-13 01:24:29 +0000627
628 if (!(videodata = PyString_FromStringAndSize(NULL, bytes)))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000629 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000630
Guido van Rossumc88c9cb1996-12-17 20:43:55 +0000631 str = PyString_AsString(videodata);
632 if (!str)
633 goto finally;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000634
635 if (svCaptureOneFrame(self->ob_svideo, format, &width, &height, str)) {
636 res = sv_error();
637 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000638 }
639
Barry Warsawf630f6b1996-12-13 01:24:29 +0000640 res = Py_BuildValue("(iiO)", width, height, videodata);
641
642 finally:
643 Py_XDECREF(videodata);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000644 return res;
645}
646
Barry Warsawf630f6b1996-12-13 01:24:29 +0000647static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000648sv_InitContinuousCapture(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000649{
650 svCaptureInfo info;
651
Barry Warsawf630f6b1996-12-13 01:24:29 +0000652 if (!PyArg_Parse(args, "(iiiii)", &info.format,
653 &info.width, &info.height,
654 &info.size, &info.samplingrate))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000655 return NULL;
656
657 if (svInitContinuousCapture(self->ob_svideo, &info))
658 return sv_error();
659
660 self->ob_info = info;
661
Barry Warsawf630f6b1996-12-13 01:24:29 +0000662 return Py_BuildValue("(iiiii)", info.format, info.width, info.height,
663 info.size, info.samplingrate);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000664}
665
Barry Warsawf630f6b1996-12-13 01:24:29 +0000666static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000667sv_LoadMap(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000668{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000669 PyObject *rgb;
670 PyObject *res = NULL;
671 rgb_tuple *mapp = NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000672 int maptype;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000673 int i, j; /* indices */
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000674
Barry Warsawf630f6b1996-12-13 01:24:29 +0000675 if (!PyArg_Parse(args, "(iO)", &maptype, &rgb))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000676 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000677
678 if (!PyList_Check(rgb) || PyList_Size(rgb) != 256) {
679 PyErr_BadArgument();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000680 return NULL;
681 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000682
683 if (!(mapp = PyMem_NEW(rgb_tuple, 256)))
684 return PyErr_NoMemory();
685
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000686 for (i = 0; i < 256; i++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000687 PyObject* v = PyList_GetItem(rgb, i);
688 if (!v)
689 goto finally;
690
691 if (!PyTuple_Check(v) || PyTuple_Size(v) != 3) {
692 PyErr_BadArgument();
693 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000694 }
695 for (j = 0; j < 3; j++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000696 PyObject* cell = PyTuple_GetItem(v, j);
697 if (!cell)
698 goto finally;
699
700 if (!PyInt_Check(cell)) {
701 PyErr_BadArgument();
702 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000703 }
704 switch (j) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000705 case 0: mapp[i].red = PyInt_AsLong(cell); break;
706 case 1: mapp[i].blue = PyInt_AsLong(cell); break;
707 case 2: mapp[i].green = PyInt_AsLong(cell); break;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000708 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000709 if (PyErr_Occurred())
710 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000711 }
712 }
713
714 if (svLoadMap(self->ob_svideo, maptype, mapp)) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000715 res = sv_error();
716 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000717 }
718
Barry Warsawf630f6b1996-12-13 01:24:29 +0000719 Py_INCREF(Py_None);
720 res = Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000721
Barry Warsawf630f6b1996-12-13 01:24:29 +0000722 finally:
723 PyMem_DEL(mapp);
724 return res;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000725}
726
Barry Warsawf630f6b1996-12-13 01:24:29 +0000727static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000728sv_CloseVideo(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000729{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000730 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000731 return NULL;
732
733 if (svCloseVideo(self->ob_svideo))
734 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000735
Barry Warsawf630f6b1996-12-13 01:24:29 +0000736 self->ob_svideo = NULL;
737 Py_INCREF(Py_None);
738 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000739}
740
Barry Warsawf630f6b1996-12-13 01:24:29 +0000741static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000742doParams(svobject *self, PyObject *args,
743 int (*func)(SV_nodeP, long *, int), int modified)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000744{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000745 PyObject *list;
746 PyObject *res = NULL;
747 long *PVbuffer = NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000748 long length;
749 int i;
750
Barry Warsawf630f6b1996-12-13 01:24:29 +0000751 if (!PyArg_Parse(args, "O", &list))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000752 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000753
754 if (!PyList_Check(list)) {
755 PyErr_BadArgument();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000756 return NULL;
757 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000758
759 if ((length = PyList_Size(list)) < 0)
760 return NULL;
761
762 PVbuffer = PyMem_NEW(long, length);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000763 if (PVbuffer == NULL)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000764 return PyErr_NoMemory();
765
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000766 for (i = 0; i < length; i++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000767 PyObject *v = PyList_GetItem(list, i);
768 if (!v)
769 goto finally;
770
771 if (!PyInt_Check(v)) {
772 PyErr_BadArgument();
773 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000774 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000775 PVbuffer[i] = PyInt_AsLong(v);
776 /* can't just test the return value, because what if the
777 value was -1?!
778 */
779 if (PVbuffer[i] == -1 && PyErr_Occurred())
780 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000781 }
782
783 if ((*func)(self->ob_svideo, PVbuffer, length)) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000784 res = sv_error();
785 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000786 }
787
788 if (modified) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000789 for (i = 0; i < length; i++) {
790 PyObject* v = PyInt_FromLong(PVbuffer[i]);
791 if (!v || PyList_SetItem(list, i, v) < 0)
792 goto finally;
793 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000794 }
795
Barry Warsawf630f6b1996-12-13 01:24:29 +0000796 Py_INCREF(Py_None);
797 res = Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000798
Barry Warsawf630f6b1996-12-13 01:24:29 +0000799 finally:
800 PyMem_DEL(PVbuffer);
801 return res;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000802}
803
Barry Warsawf630f6b1996-12-13 01:24:29 +0000804static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000805sv_GetParam(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000806{
807 return doParams(self, args, svGetParam, 1);
808}
809
Barry Warsawf630f6b1996-12-13 01:24:29 +0000810static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000811sv_GetParamRange(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000812{
813 return doParams(self, args, svGetParamRange, 1);
814}
815
Barry Warsawf630f6b1996-12-13 01:24:29 +0000816static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000817sv_SetParam(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000818{
819 return doParams(self, args, svSetParam, 0);
820}
821
Barry Warsawf630f6b1996-12-13 01:24:29 +0000822static PyMethodDef svideo_methods[] = {
823 {"BindGLWindow", (PyCFunction)sv_BindGLWindow},
824 {"EndContinuousCapture",(PyCFunction)sv_EndContinuousCapture},
825 {"IsVideoDisplayed", (PyCFunction)sv_IsVideoDisplayed},
826 {"OutputOffset", (PyCFunction)sv_OutputOffset},
827 {"PutFrame", (PyCFunction)sv_PutFrame},
828 {"QuerySize", (PyCFunction)sv_QuerySize},
829 {"SetSize", (PyCFunction)sv_SetSize},
830 {"SetStdDefaults", (PyCFunction)sv_SetStdDefaults},
831 {"UseExclusive", (PyCFunction)sv_UseExclusive},
832 {"WindowOffset", (PyCFunction)sv_WindowOffset},
833 {"InitContinuousCapture",(PyCFunction)sv_InitContinuousCapture},
834 {"CaptureBurst", (PyCFunction)sv_CaptureBurst},
835 {"CaptureOneFrame", (PyCFunction)sv_CaptureOneFrame},
836 {"GetCaptureData", (PyCFunction)sv_GetCaptureData},
837 {"CloseVideo", (PyCFunction)sv_CloseVideo},
838 {"LoadMap", (PyCFunction)sv_LoadMap},
839 {"GetParam", (PyCFunction)sv_GetParam},
840 {"GetParamRange", (PyCFunction)sv_GetParamRange},
841 {"SetParam", (PyCFunction)sv_SetParam},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000842 {NULL, NULL} /* sentinel */
843};
844
Barry Warsawf630f6b1996-12-13 01:24:29 +0000845static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000846sv_conversion(PyObject *self, PyObject *args, void (*function)(),
847 int inputfactor, float factor)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000848{
849 int invert, width, height, inputlength;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000850 char *input, *str;
851 PyObject *output;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000852
Barry Warsawf630f6b1996-12-13 01:24:29 +0000853 if (!PyArg_Parse(args, "(is#ii)", &invert,
854 &input, &inputlength, &width, &height))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000855 return NULL;
856
857 if (width * height * inputfactor > inputlength) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000858 PyErr_SetString(SvError, "input buffer not long enough");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000859 return NULL;
860 }
861
Barry Warsawf630f6b1996-12-13 01:24:29 +0000862 if (!(output = PyString_FromStringAndSize(NULL,
863 (int)(width * height * factor))))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000864 return NULL;
865
Barry Warsawf630f6b1996-12-13 01:24:29 +0000866 str = PyString_AsString(output);
867 if (!str) {
868 Py_DECREF(output);
869 return NULL;
870 }
871 (*function)(invert, input, str, width, height);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000872
873 return output;
874}
875
Barry Warsawf630f6b1996-12-13 01:24:29 +0000876static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000877sv_InterleaveFields(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000878{
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000879 return sv_conversion(self, args, svInterleaveFields, 1, 1.0);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000880}
881
Barry Warsawf630f6b1996-12-13 01:24:29 +0000882static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000883sv_RGB8toRGB32(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000884{
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000885 return sv_conversion(self, args, svRGB8toRGB32, 1, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000886}
887
Barry Warsawf630f6b1996-12-13 01:24:29 +0000888static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000889sv_YUVtoRGB(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000890{
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000891 return sv_conversion(self, args, svYUVtoRGB, 2, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000892}
893
894static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000895svideo_dealloc(svobject *self)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000896{
897 if (self->ob_svideo != NULL)
898 (void) svCloseVideo(self->ob_svideo);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000899 PyObject_Del(self);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000900}
901
Barry Warsawf630f6b1996-12-13 01:24:29 +0000902static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000903svideo_getattr(svobject *self, char *name)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000904{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000905 return Py_FindMethod(svideo_methods, (PyObject *)self, name);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000906}
907
Barry Warsawf630f6b1996-12-13 01:24:29 +0000908PyTypeObject Svtype = {
909 PyObject_HEAD_INIT(&PyType_Type)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000910 0, /*ob_size*/
911 "sv", /*tp_name*/
912 sizeof(svobject), /*tp_size*/
913 0, /*tp_itemsize*/
914 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000915 (destructor)svideo_dealloc, /*tp_dealloc*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000916 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000917 (getattrfunc)svideo_getattr, /*tp_getattr*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000918 0, /*tp_setattr*/
919 0, /*tp_compare*/
920 0, /*tp_repr*/
921};
922
Barry Warsawf630f6b1996-12-13 01:24:29 +0000923static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000924newsvobject(SV_nodeP svp)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000925{
926 svobject *p;
927
Guido van Rossumb18618d2000-05-03 23:44:39 +0000928 p = PyObject_New(svobject, &Svtype);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000929 if (p == NULL)
930 return NULL;
931 p->ob_svideo = svp;
932 p->ob_info.format = 0;
933 p->ob_info.size = 0;
934 p->ob_info.width = 0;
935 p->ob_info.height = 0;
936 p->ob_info.samplingrate = 0;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000937 return (PyObject *) p;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000938}
939
Barry Warsawf630f6b1996-12-13 01:24:29 +0000940static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000941sv_OpenVideo(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000942{
943 SV_nodeP svp;
944
Barry Warsawf630f6b1996-12-13 01:24:29 +0000945 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000946 return NULL;
947
948 svp = svOpenVideo();
949 if (svp == NULL)
950 return sv_error();
951
952 return newsvobject(svp);
953}
954
Barry Warsawf630f6b1996-12-13 01:24:29 +0000955static PyMethodDef sv_methods[] = {
956 {"InterleaveFields", (PyCFunction)sv_InterleaveFields},
957 {"RGB8toRGB32", (PyCFunction)sv_RGB8toRGB32},
958 {"YUVtoRGB", (PyCFunction)sv_YUVtoRGB},
959 {"OpenVideo", (PyCFunction)sv_OpenVideo},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000960 {NULL, NULL} /* Sentinel */
961};
962
963void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000964initsv(void)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000965{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000966 PyObject *m, *d;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000967
Barry Warsawf630f6b1996-12-13 01:24:29 +0000968 m = Py_InitModule("sv", sv_methods);
969 d = PyModule_GetDict(m);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000970
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000971 SvError = PyErr_NewException("sv.error", NULL, NULL);
Barry Warsawf630f6b1996-12-13 01:24:29 +0000972 if (SvError == NULL || PyDict_SetItemString(d, "error", SvError) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000973 return;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000974}