blob: 1519065666b1b95a87261a9f371dafd566a021f0 [file] [log] [blame]
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001/* SV module -- interface to the Indigo video board */
2
Guido van Rossumc88c9cb1996-12-17 20:43:55 +00003/* WARNING! This module is for hardware that we don't have any more,
4 so it hasn't been tested. It has been converted to the new coding
5 style, and it is possible that this conversion has broken something
6 -- user beware! */
7
Sjoerd Mullendered59d201993-01-06 13:36:38 +00008#include <sys/time.h>
9#include <svideo.h>
Barry Warsawf630f6b1996-12-13 01:24:29 +000010#include "Python.h"
Sjoerd Mullendered59d201993-01-06 13:36:38 +000011#include "compile.h"
Antoine Pitrouc83ea132010-05-09 14:46:46 +000012#include "yuv.h" /* for YUV conversion functions */
Sjoerd Mullendered59d201993-01-06 13:36:38 +000013
14typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000015 PyObject_HEAD
16 SV_nodeP ob_svideo;
17 svCaptureInfo ob_info;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000018} svobject;
19
20typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000021 PyObject_HEAD
22 void *ob_capture;
23 int ob_mustunlock;
24 svCaptureInfo ob_info;
25 svobject *ob_svideo;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000026} captureobject;
27
Antoine Pitrouc83ea132010-05-09 14:46:46 +000028static PyObject *SvError; /* exception sv.error */
Sjoerd Mullendered59d201993-01-06 13:36:38 +000029
Tim Petersdbd9ba62000-07-09 03:09:57 +000030static PyObject *newcaptureobject(svobject *, void *, int);
Sjoerd Mullendered59d201993-01-06 13:36:38 +000031
32/* Set a SV-specific error from svideo_errno and return NULL */
Barry Warsawf630f6b1996-12-13 01:24:29 +000033static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000034sv_error(void)
Sjoerd Mullendered59d201993-01-06 13:36:38 +000035{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000036 PyErr_SetString(SvError, svStrerror(svideo_errno));
37 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000038}
39
Barry Warsawf630f6b1996-12-13 01:24:29 +000040static PyObject *
Antoine Pitrouc83ea132010-05-09 14:46:46 +000041svc_conversion(captureobject *self, PyObject *args, void (*function)(), float factor)
Sjoerd Mullendered59d201993-01-06 13:36:38 +000042{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000043 PyObject *output;
44 int invert;
45 char* outstr;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000046
Antoine Pitrouc83ea132010-05-09 14:46:46 +000047 if (!PyArg_Parse(args, "i", &invert))
48 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000049
Antoine Pitrouc83ea132010-05-09 14:46:46 +000050 if (!(output = PyString_FromStringAndSize(
51 NULL,
52 (int)(self->ob_info.width * self->ob_info.height * factor))))
53 {
54 return NULL;
55 }
56 if (!(outstr = PyString_AsString(output))) {
57 Py_DECREF(output);
58 return NULL;
59 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +000060
Antoine Pitrouc83ea132010-05-09 14:46:46 +000061 (*function)((boolean)invert, self->ob_capture,
62 outstr,
63 self->ob_info.width, self->ob_info.height);
Sjoerd Mullendered59d201993-01-06 13:36:38 +000064
Antoine Pitrouc83ea132010-05-09 14:46:46 +000065 return output;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000066}
67
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000068/*
69 * 3 functions to convert from Starter Video YUV 4:1:1 format to
70 * Compression Library 4:2:2 Duplicate Chroma format.
71 */
Barry Warsawf630f6b1996-12-13 01:24:29 +000072static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +000073svc_YUVtoYUV422DC(captureobject *self, PyObject *args)
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000074{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000075 if (self->ob_info.format != SV_YUV411_FRAMES) {
76 PyErr_SetString(SvError, "data has bad format");
77 return NULL;
78 }
79 return svc_conversion(self, args, yuv_sv411_to_cl422dc, 2.0);
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000080}
81
Barry Warsawf630f6b1996-12-13 01:24:29 +000082static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +000083svc_YUVtoYUV422DC_quarter(captureobject *self, PyObject *args)
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000084{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000085 if (self->ob_info.format != SV_YUV411_FRAMES) {
86 PyErr_SetString(SvError, "data has bad format");
87 return NULL;
88 }
89 return svc_conversion(self, args,
90 yuv_sv411_to_cl422dc_quartersize, 0.5);
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000091}
92
Barry Warsawf630f6b1996-12-13 01:24:29 +000093static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +000094svc_YUVtoYUV422DC_sixteenth(captureobject *self, PyObject *args)
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000095{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000096 if (self->ob_info.format != SV_YUV411_FRAMES) {
97 PyErr_SetString(SvError, "data has bad format");
98 return NULL;
99 }
100 return svc_conversion(self, args,
101 yuv_sv411_to_cl422dc_sixteenthsize, 0.125);
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000102}
103
Barry Warsawf630f6b1996-12-13 01:24:29 +0000104static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000105svc_YUVtoRGB(captureobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000106{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000107 switch (self->ob_info.format) {
108 case SV_YUV411_FRAMES:
109 case SV_YUV411_FRAMES_AND_BLANKING_BUFFER:
110 break;
111 default:
112 PyErr_SetString(SvError, "data had bad format");
113 return NULL;
114 }
115 return svc_conversion(self, args, svYUVtoRGB, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000116}
117
Barry Warsawf630f6b1996-12-13 01:24:29 +0000118static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000119svc_RGB8toRGB32(captureobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000120{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000121 if (self->ob_info.format != SV_RGB8_FRAMES) {
122 PyErr_SetString(SvError, "data has bad format");
123 return NULL;
124 }
125 return svc_conversion(self, args, svRGB8toRGB32, (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_InterleaveFields(captureobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000130{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000131 if (self->ob_info.format != SV_RGB8_FRAMES) {
132 PyErr_SetString(SvError, "data has bad format");
133 return NULL;
134 }
135 return svc_conversion(self, args, svInterleaveFields, 1.0);
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_GetFields(captureobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000140{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000141 PyObject *f1 = NULL;
142 PyObject *f2 = NULL;
143 PyObject *ret = NULL;
144 int fieldsize;
145 char* obcapture;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000146
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000147 if (self->ob_info.format != SV_RGB8_FRAMES) {
148 PyErr_SetString(SvError, "data has bad format");
149 return NULL;
150 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000151
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000152 fieldsize = self->ob_info.width * self->ob_info.height / 2;
153 obcapture = (char*)self->ob_capture;
154
155 if (!(f1 = PyString_FromStringAndSize(obcapture, fieldsize)))
156 goto finally;
157 if (!(f2 = PyString_FromStringAndSize(obcapture + fieldsize,
158 fieldsize)))
159 goto finally;
160 ret = PyTuple_Pack(2, f1, f2);
Barry Warsawf630f6b1996-12-13 01:24:29 +0000161
162 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000163 Py_XDECREF(f1);
164 Py_XDECREF(f2);
165 return ret;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000166}
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000167
Barry Warsawf630f6b1996-12-13 01:24:29 +0000168static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000169svc_UnlockCaptureData(captureobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000170{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000171 if (!PyArg_Parse(args, ""))
172 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000173
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000174 if (!self->ob_mustunlock) {
175 PyErr_SetString(SvError, "buffer should not be unlocked");
176 return NULL;
177 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000178
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000179 if (svUnlockCaptureData(self->ob_svideo->ob_svideo, self->ob_capture))
180 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000181
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000182 self->ob_mustunlock = 0;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000183
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000184 Py_INCREF(Py_None);
185 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000186}
187
188#ifdef USE_GL
189#include <gl.h>
190
Barry Warsawf630f6b1996-12-13 01:24:29 +0000191static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000192svc_lrectwrite(captureobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000193{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000194 Screencoord x1, x2, y1, y2;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000195
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000196 if (!PyArg_Parse(args, "(hhhh)", &x1, &x2, &y1, &y2))
197 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000198
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000199 lrectwrite(x1, x2, y1, y2, (unsigned long *) self->ob_capture);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000200
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000201 Py_INCREF(Py_None);
202 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000203}
204#endif
205
Barry Warsawf630f6b1996-12-13 01:24:29 +0000206static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000207svc_writefile(captureobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000208{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000209 PyObject *file;
210 int size;
211 FILE* fp;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000212
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000213 if (!PyArg_Parse(args, "O", &file))
214 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000215
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000216 if (!PyFile_Check(file)) {
217 PyErr_SetString(SvError, "not a file object");
218 return NULL;
219 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000220
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000221 if (!(fp = PyFile_AsFile(file)))
222 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000223
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000224 size = self->ob_info.width * self->ob_info.height;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000225
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000226 if (fwrite(self->ob_capture, sizeof(long), size, fp) != size) {
227 PyErr_SetString(SvError, "writing failed");
228 return NULL;
229 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000230
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000231 Py_INCREF(Py_None);
232 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000233}
234
Barry Warsawf630f6b1996-12-13 01:24:29 +0000235static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000236svc_FindVisibleRegion(captureobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000237{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000238 void *visible;
239 int width;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000240
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000241 if (!PyArg_Parse(args, ""))
242 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000243
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000244 if (svFindVisibleRegion(self->ob_svideo->ob_svideo,
245 self->ob_capture, &visible,
246 self->ob_info.width))
247 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000248
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000249 if (visible == NULL) {
250 PyErr_SetString(SvError, "data in wrong format");
251 return NULL;
252 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000253
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000254 return newcaptureobject(self->ob_svideo, visible, 0);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000255}
256
Barry Warsawf630f6b1996-12-13 01:24:29 +0000257static PyMethodDef capture_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000258 {"YUVtoRGB", (PyCFunction)svc_YUVtoRGB, METH_OLDARGS},
259 {"RGB8toRGB32", (PyCFunction)svc_RGB8toRGB32, METH_OLDARGS},
260 {"InterleaveFields", (PyCFunction)svc_InterleaveFields, METH_OLDARGS},
261 {"UnlockCaptureData", (PyCFunction)svc_UnlockCaptureData, METH_OLDARGS},
262 {"FindVisibleRegion", (PyCFunction)svc_FindVisibleRegion, METH_OLDARGS},
263 {"GetFields", (PyCFunction)svc_GetFields, METH_OLDARGS},
264 {"YUVtoYUV422DC", (PyCFunction)svc_YUVtoYUV422DC, METH_OLDARGS},
265 {"YUVtoYUV422DC_quarter",(PyCFunction)svc_YUVtoYUV422DC_quarter, METH_OLDARGS},
266 {"YUVtoYUV422DC_sixteenth",(PyCFunction)svc_YUVtoYUV422DC_sixteenth, METH_OLDARGS},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000267#ifdef USE_GL
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000268 {"lrectwrite", (PyCFunction)svc_lrectwrite, METH_OLDARGS},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000269#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000270 {"writefile", (PyCFunction)svc_writefile, METH_OLDARGS},
271 {NULL, NULL} /* sentinel */
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000272};
273
274static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000275capture_dealloc(captureobject *self)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000276{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000277 if (self->ob_capture != NULL) {
278 if (self->ob_mustunlock)
279 (void)svUnlockCaptureData(self->ob_svideo->ob_svideo,
280 self->ob_capture);
281 self->ob_capture = NULL;
Serhiy Storchaka98a97222014-02-09 13:14:04 +0200282 Py_CLEAR(self->ob_svideo);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000283 }
284 PyObject_Del(self);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000285}
286
Barry Warsawf630f6b1996-12-13 01:24:29 +0000287static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000288capture_getattr(svobject *self, char *name)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000289{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000290 return Py_FindMethod(capture_methods, (PyObject *)self, name);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000291}
292
Barry Warsawf630f6b1996-12-13 01:24:29 +0000293PyTypeObject Capturetype = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000294 PyObject_HEAD_INIT(&PyType_Type)
295 0, /*ob_size*/
296 "sv.capture", /*tp_name*/
297 sizeof(captureobject), /*tp_size*/
298 0, /*tp_itemsize*/
299 /* methods */
300 (destructor)capture_dealloc, /*tp_dealloc*/
301 0, /*tp_print*/
302 (getattrfunc)capture_getattr, /*tp_getattr*/
303 0, /*tp_setattr*/
304 0, /*tp_compare*/
305 0, /*tp_repr*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000306};
307
Barry Warsawf630f6b1996-12-13 01:24:29 +0000308static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000309newcaptureobject(svobject *self, void *ptr, int mustunlock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000310{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000311 captureobject *p;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000312
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000313 p = PyObject_New(captureobject, &Capturetype);
314 if (p == NULL)
315 return NULL;
316 p->ob_svideo = self;
317 Py_INCREF(self);
318 p->ob_capture = ptr;
319 p->ob_mustunlock = mustunlock;
320 p->ob_info = self->ob_info;
321 return (PyObject *) p;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000322}
323
Barry Warsawf630f6b1996-12-13 01:24:29 +0000324static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000325sv_GetCaptureData(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000326{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000327 void *ptr;
328 long fieldID;
329 PyObject *res, *c;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000330
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000331 if (!PyArg_Parse(args, ""))
332 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000333
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000334 if (svGetCaptureData(self->ob_svideo, &ptr, &fieldID))
335 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000336
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000337 if (ptr == NULL) {
338 PyErr_SetString(SvError, "no data available");
339 return NULL;
340 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000341
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000342 c = newcaptureobject(self, ptr, 1);
343 if (c == NULL)
344 return NULL;
345 res = Py_BuildValue("(Oi)", c, fieldID);
346 Py_DECREF(c);
347 return res;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000348}
349
Barry Warsawf630f6b1996-12-13 01:24:29 +0000350static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000351sv_BindGLWindow(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000352{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000353 long wid;
354 int mode;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000355
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000356 if (!PyArg_Parse(args, "(ii)", &wid, &mode))
357 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000358
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000359 if (svBindGLWindow(self->ob_svideo, wid, mode))
360 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000361
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000362 Py_INCREF(Py_None);
363 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000364}
365
Barry Warsawf630f6b1996-12-13 01:24:29 +0000366static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000367sv_EndContinuousCapture(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000368{
369
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000370 if (!PyArg_Parse(args, ""))
371 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000372
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000373 if (svEndContinuousCapture(self->ob_svideo))
374 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000375
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000376 Py_INCREF(Py_None);
377 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000378}
379
Barry Warsawf630f6b1996-12-13 01:24:29 +0000380static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000381sv_IsVideoDisplayed(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000382{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000383 int v;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000384
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000385 if (!PyArg_Parse(args, ""))
386 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000387
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000388 v = svIsVideoDisplayed(self->ob_svideo);
389 if (v == -1)
390 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000391
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000392 return PyInt_FromLong((long) v);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000393}
394
Barry Warsawf630f6b1996-12-13 01:24:29 +0000395static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000396sv_OutputOffset(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000397{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000398 int x_offset;
399 int y_offset;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000400
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000401 if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset))
402 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000403
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000404 if (svOutputOffset(self->ob_svideo, x_offset, y_offset))
405 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000406
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000407 Py_INCREF(Py_None);
408 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000409}
410
Barry Warsawf630f6b1996-12-13 01:24:29 +0000411static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000412sv_PutFrame(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000413{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000414 char *buffer;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000415
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000416 if (!PyArg_Parse(args, "s", &buffer))
417 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000418
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000419 if (svPutFrame(self->ob_svideo, buffer))
420 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000421
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000422 Py_INCREF(Py_None);
423 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000424}
425
Barry Warsawf630f6b1996-12-13 01:24:29 +0000426static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000427sv_QuerySize(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000428{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000429 int w;
430 int h;
431 int rw;
432 int rh;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000433
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000434 if (!PyArg_Parse(args, "(ii)", &w, &h))
435 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000436
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000437 if (svQuerySize(self->ob_svideo, w, h, &rw, &rh))
438 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000439
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000440 return Py_BuildValue("(ii)", (long) rw, (long) rh);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000441}
442
Barry Warsawf630f6b1996-12-13 01:24:29 +0000443static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000444sv_SetSize(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000445{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000446 int w;
447 int h;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000448
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000449 if (!PyArg_Parse(args, "(ii)", &w, &h))
450 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000451
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000452 if (svSetSize(self->ob_svideo, w, h))
453 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000454
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000455 Py_INCREF(Py_None);
456 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000457}
458
Barry Warsawf630f6b1996-12-13 01:24:29 +0000459static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000460sv_SetStdDefaults(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000461{
462
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000463 if (!PyArg_Parse(args, ""))
464 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000465
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000466 if (svSetStdDefaults(self->ob_svideo))
467 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000468
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000469 Py_INCREF(Py_None);
470 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000471}
472
Barry Warsawf630f6b1996-12-13 01:24:29 +0000473static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000474sv_UseExclusive(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000475{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000476 boolean onoff;
477 int mode;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000478
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000479 if (!PyArg_Parse(args, "(ii)", &onoff, &mode))
480 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000481
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000482 if (svUseExclusive(self->ob_svideo, onoff, mode))
483 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000484
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000485 Py_INCREF(Py_None);
486 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000487}
488
Barry Warsawf630f6b1996-12-13 01:24:29 +0000489static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000490sv_WindowOffset(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000491{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000492 int x_offset;
493 int y_offset;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000494
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000495 if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset))
496 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000497
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000498 if (svWindowOffset(self->ob_svideo, x_offset, y_offset))
499 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000500
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000501 Py_INCREF(Py_None);
502 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000503}
504
Barry Warsawf630f6b1996-12-13 01:24:29 +0000505static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000506sv_CaptureBurst(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000507{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000508 int bytes, i;
509 svCaptureInfo info;
510 void *bitvector = NULL;
511 PyObject *videodata = NULL;
512 PyObject *bitvecobj = NULL;
513 PyObject *res = NULL;
514 static PyObject *evenitem, *odditem;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000515
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000516 if (!PyArg_Parse(args, "(iiiii)", &info.format,
517 &info.width, &info.height,
518 &info.size, &info.samplingrate))
519 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000520
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000521 switch (info.format) {
522 case SV_RGB8_FRAMES:
523 bitvector = malloc(SV_BITVEC_SIZE(info.size));
524 break;
525 case SV_YUV411_FRAMES_AND_BLANKING_BUFFER:
526 break;
527 default:
528 PyErr_SetString(SvError, "illegal format specified");
529 return NULL;
530 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000531
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000532 if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes)) {
533 res = sv_error();
534 goto finally;
535 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000536
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000537 if (!(videodata = PyString_FromStringAndSize(NULL, bytes)))
538 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000539
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000540 /* XXX -- need to do something about the bitvector */
541 {
542 char* str = PyString_AsString(videodata);
543 if (!str)
544 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000545
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000546 if (svCaptureBurst(self->ob_svideo, &info, str, bitvector)) {
547 res = sv_error();
548 goto finally;
549 }
550 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000551
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000552 if (bitvector) {
553 if (evenitem == NULL) {
554 if (!(evenitem = PyInt_FromLong(0)))
555 goto finally;
556 }
557 if (odditem == NULL) {
558 if (!(odditem = PyInt_FromLong(1)))
559 goto finally;
560 }
561 if (!(bitvecobj = PyTuple_New(2 * info.size)))
562 goto finally;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000563
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000564 for (i = 0; i < 2 * info.size; i++) {
565 int sts;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000566
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000567 if (SV_GET_FIELD(bitvector, i) == SV_EVEN_FIELD) {
568 Py_INCREF(evenitem);
569 sts = PyTuple_SetItem(bitvecobj, i, evenitem);
570 } else {
571 Py_INCREF(odditem);
572 sts = PyTuple_SetItem(bitvecobj, i, odditem);
573 }
574 if (sts < 0)
575 goto finally;
576 }
577 } else {
578 bitvecobj = Py_None;
579 Py_INCREF(Py_None);
580 }
581
582 res = Py_BuildValue("((iiiii)OO)", info.format,
583 info.width, info.height,
584 info.size, info.samplingrate,
585 videodata, bitvecobj);
Barry Warsawf630f6b1996-12-13 01:24:29 +0000586
587 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000588 if (bitvector)
589 free(bitvector);
Barry Warsawf630f6b1996-12-13 01:24:29 +0000590
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000591 Py_XDECREF(videodata);
592 Py_XDECREF(bitvecobj);
593 return res;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000594}
595
Barry Warsawf630f6b1996-12-13 01:24:29 +0000596static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000597sv_CaptureOneFrame(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000598{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000599 svCaptureInfo info;
600 int format, width, height;
601 int bytes;
602 PyObject *videodata = NULL;
603 PyObject *res = NULL;
604 char *str;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000605
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000606 if (!PyArg_Parse(args, "(iii)", &format, &width, &height))
607 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000608
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000609 info.format = format;
610 info.width = width;
611 info.height = height;
612 info.size = 0;
613 info.samplingrate = 0;
614 if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes))
615 return sv_error();
Barry Warsawf630f6b1996-12-13 01:24:29 +0000616
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000617 if (!(videodata = PyString_FromStringAndSize(NULL, bytes)))
618 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000619
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000620 str = PyString_AsString(videodata);
621 if (!str)
622 goto finally;
623
624 if (svCaptureOneFrame(self->ob_svideo, format, &width, &height, str)) {
625 res = sv_error();
626 goto finally;
627 }
628
629 res = Py_BuildValue("(iiO)", width, height, videodata);
Barry Warsawf630f6b1996-12-13 01:24:29 +0000630
631 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000632 Py_XDECREF(videodata);
633 return res;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000634}
635
Barry Warsawf630f6b1996-12-13 01:24:29 +0000636static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000637sv_InitContinuousCapture(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000638{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000639 svCaptureInfo info;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000640
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000641 if (!PyArg_Parse(args, "(iiiii)", &info.format,
642 &info.width, &info.height,
643 &info.size, &info.samplingrate))
644 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000645
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000646 if (svInitContinuousCapture(self->ob_svideo, &info))
647 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000648
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000649 self->ob_info = info;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000650
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000651 return Py_BuildValue("(iiiii)", info.format, info.width, info.height,
652 info.size, info.samplingrate);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000653}
654
Barry Warsawf630f6b1996-12-13 01:24:29 +0000655static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000656sv_LoadMap(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000657{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000658 PyObject *rgb;
659 PyObject *res = NULL;
660 rgb_tuple *mapp = NULL;
661 int maptype;
662 int i, j; /* indices */
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000663
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000664 if (!PyArg_Parse(args, "(iO)", &maptype, &rgb))
665 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000666
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000667 if (!PyList_Check(rgb) || PyList_Size(rgb) != 256) {
668 PyErr_BadArgument();
669 return NULL;
670 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000671
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000672 if (!(mapp = PyMem_NEW(rgb_tuple, 256)))
673 return PyErr_NoMemory();
Barry Warsawf630f6b1996-12-13 01:24:29 +0000674
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000675 for (i = 0; i < 256; i++) {
676 PyObject* v = PyList_GetItem(rgb, i);
677 if (!v)
678 goto finally;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000679
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000680 if (!PyTuple_Check(v) || PyTuple_Size(v) != 3) {
681 PyErr_BadArgument();
682 goto finally;
683 }
684 for (j = 0; j < 3; j++) {
685 PyObject* cell = PyTuple_GetItem(v, j);
686 if (!cell)
687 goto finally;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000688
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000689 if (!PyInt_Check(cell)) {
690 PyErr_BadArgument();
691 goto finally;
692 }
693 switch (j) {
694 case 0: mapp[i].red = PyInt_AsLong(cell); break;
695 case 1: mapp[i].blue = PyInt_AsLong(cell); break;
696 case 2: mapp[i].green = PyInt_AsLong(cell); break;
697 }
698 if (PyErr_Occurred())
699 goto finally;
700 }
701 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000702
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000703 if (svLoadMap(self->ob_svideo, maptype, mapp)) {
704 res = sv_error();
705 goto finally;
706 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000707
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000708 Py_INCREF(Py_None);
709 res = Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000710
Barry Warsawf630f6b1996-12-13 01:24:29 +0000711 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000712 PyMem_DEL(mapp);
713 return res;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000714}
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000715
Barry Warsawf630f6b1996-12-13 01:24:29 +0000716static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000717sv_CloseVideo(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000718{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000719 if (!PyArg_Parse(args, ""))
720 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000721
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000722 if (svCloseVideo(self->ob_svideo))
723 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000724
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000725 self->ob_svideo = NULL;
726 Py_INCREF(Py_None);
727 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000728}
729
Barry Warsawf630f6b1996-12-13 01:24:29 +0000730static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000731doParams(svobject *self, PyObject *args,
732 int (*func)(SV_nodeP, long *, int), int modified)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000733{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000734 PyObject *list;
735 PyObject *res = NULL;
736 long *PVbuffer = NULL;
737 long length;
738 int i;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000739
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000740 if (!PyArg_Parse(args, "O", &list))
741 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000742
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000743 if (!PyList_Check(list)) {
744 PyErr_BadArgument();
745 return NULL;
746 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000747
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000748 if ((length = PyList_Size(list)) < 0)
749 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000750
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000751 PVbuffer = PyMem_NEW(long, length);
752 if (PVbuffer == NULL)
753 return PyErr_NoMemory();
Barry Warsawf630f6b1996-12-13 01:24:29 +0000754
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000755 for (i = 0; i < length; i++) {
756 PyObject *v = PyList_GetItem(list, i);
757 if (!v)
758 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000759
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000760 if (!PyInt_Check(v)) {
761 PyErr_BadArgument();
762 goto finally;
763 }
764 PVbuffer[i] = PyInt_AsLong(v);
765 /* can't just test the return value, because what if the
766 value was -1?!
767 */
768 if (PVbuffer[i] == -1 && PyErr_Occurred())
769 goto finally;
770 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000771
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000772 if ((*func)(self->ob_svideo, PVbuffer, length)) {
773 res = sv_error();
774 goto finally;
775 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000776
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000777 if (modified) {
778 for (i = 0; i < length; i++) {
779 PyObject* v = PyInt_FromLong(PVbuffer[i]);
780 if (!v || PyList_SetItem(list, i, v) < 0)
781 goto finally;
782 }
783 }
784
785 Py_INCREF(Py_None);
786 res = Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000787
Barry Warsawf630f6b1996-12-13 01:24:29 +0000788 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000789 PyMem_DEL(PVbuffer);
790 return res;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000791}
792
Barry Warsawf630f6b1996-12-13 01:24:29 +0000793static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000794sv_GetParam(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000795{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000796 return doParams(self, args, svGetParam, 1);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000797}
798
Barry Warsawf630f6b1996-12-13 01:24:29 +0000799static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000800sv_GetParamRange(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000801{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000802 return doParams(self, args, svGetParamRange, 1);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000803}
804
Barry Warsawf630f6b1996-12-13 01:24:29 +0000805static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000806sv_SetParam(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000807{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000808 return doParams(self, args, svSetParam, 0);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000809}
810
Barry Warsawf630f6b1996-12-13 01:24:29 +0000811static PyMethodDef svideo_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000812 {"BindGLWindow", (PyCFunction)sv_BindGLWindow, METH_OLDARGS},
813 {"EndContinuousCapture",(PyCFunction)sv_EndContinuousCapture, METH_OLDARGS},
814 {"IsVideoDisplayed", (PyCFunction)sv_IsVideoDisplayed, METH_OLDARGS},
815 {"OutputOffset", (PyCFunction)sv_OutputOffset, METH_OLDARGS},
816 {"PutFrame", (PyCFunction)sv_PutFrame, METH_OLDARGS},
817 {"QuerySize", (PyCFunction)sv_QuerySize, METH_OLDARGS},
818 {"SetSize", (PyCFunction)sv_SetSize, METH_OLDARGS},
819 {"SetStdDefaults", (PyCFunction)sv_SetStdDefaults, METH_OLDARGS},
820 {"UseExclusive", (PyCFunction)sv_UseExclusive, METH_OLDARGS},
821 {"WindowOffset", (PyCFunction)sv_WindowOffset, METH_OLDARGS},
822 {"InitContinuousCapture",(PyCFunction)sv_InitContinuousCapture, METH_OLDARGS},
823 {"CaptureBurst", (PyCFunction)sv_CaptureBurst, METH_OLDARGS},
824 {"CaptureOneFrame", (PyCFunction)sv_CaptureOneFrame, METH_OLDARGS},
825 {"GetCaptureData", (PyCFunction)sv_GetCaptureData, METH_OLDARGS},
826 {"CloseVideo", (PyCFunction)sv_CloseVideo, METH_OLDARGS},
827 {"LoadMap", (PyCFunction)sv_LoadMap, METH_OLDARGS},
828 {"GetParam", (PyCFunction)sv_GetParam, METH_OLDARGS},
829 {"GetParamRange", (PyCFunction)sv_GetParamRange, METH_OLDARGS},
830 {"SetParam", (PyCFunction)sv_SetParam, METH_OLDARGS},
831 {NULL, NULL} /* sentinel */
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000832};
833
Barry Warsawf630f6b1996-12-13 01:24:29 +0000834static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000835sv_conversion(PyObject *self, PyObject *args, void (*function)(),
836 int inputfactor, float factor)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000837{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000838 int invert, width, height, inputlength;
839 char *input, *str;
840 PyObject *output;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000841
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000842 if (!PyArg_Parse(args, "(is#ii)", &invert,
843 &input, &inputlength, &width, &height))
844 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000845
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000846 if (width * height * inputfactor > inputlength) {
847 PyErr_SetString(SvError, "input buffer not long enough");
848 return NULL;
849 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000850
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000851 if (!(output = PyString_FromStringAndSize(NULL,
852 (int)(width * height * factor))))
853 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000854
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000855 str = PyString_AsString(output);
856 if (!str) {
857 Py_DECREF(output);
858 return NULL;
859 }
860 (*function)(invert, input, str, width, height);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000861
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000862 return output;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000863}
864
Barry Warsawf630f6b1996-12-13 01:24:29 +0000865static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000866sv_InterleaveFields(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000867{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000868 return sv_conversion(self, args, svInterleaveFields, 1, 1.0);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000869}
870
Barry Warsawf630f6b1996-12-13 01:24:29 +0000871static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000872sv_RGB8toRGB32(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000873{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000874 return sv_conversion(self, args, svRGB8toRGB32, 1, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000875}
876
Barry Warsawf630f6b1996-12-13 01:24:29 +0000877static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000878sv_YUVtoRGB(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000879{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000880 return sv_conversion(self, args, svYUVtoRGB, 2, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000881}
882
883static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000884svideo_dealloc(svobject *self)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000885{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000886 if (self->ob_svideo != NULL)
887 (void) svCloseVideo(self->ob_svideo);
888 PyObject_Del(self);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000889}
890
Barry Warsawf630f6b1996-12-13 01:24:29 +0000891static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000892svideo_getattr(svobject *self, char *name)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000893{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000894 return Py_FindMethod(svideo_methods, (PyObject *)self, name);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000895}
896
Barry Warsawf630f6b1996-12-13 01:24:29 +0000897PyTypeObject Svtype = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000898 PyObject_HEAD_INIT(&PyType_Type)
899 0, /*ob_size*/
900 "sv.sv", /*tp_name*/
901 sizeof(svobject), /*tp_size*/
902 0, /*tp_itemsize*/
903 /* methods */
904 (destructor)svideo_dealloc, /*tp_dealloc*/
905 0, /*tp_print*/
906 (getattrfunc)svideo_getattr, /*tp_getattr*/
907 0, /*tp_setattr*/
908 0, /*tp_compare*/
909 0, /*tp_repr*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000910};
911
Barry Warsawf630f6b1996-12-13 01:24:29 +0000912static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000913newsvobject(SV_nodeP svp)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000914{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000915 svobject *p;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000916
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000917 p = PyObject_New(svobject, &Svtype);
918 if (p == NULL)
919 return NULL;
920 p->ob_svideo = svp;
921 p->ob_info.format = 0;
922 p->ob_info.size = 0;
923 p->ob_info.width = 0;
924 p->ob_info.height = 0;
925 p->ob_info.samplingrate = 0;
926 return (PyObject *) p;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000927}
928
Barry Warsawf630f6b1996-12-13 01:24:29 +0000929static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000930sv_OpenVideo(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000931{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000932 SV_nodeP svp;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000933
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000934 if (!PyArg_Parse(args, ""))
935 return NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000936
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000937 svp = svOpenVideo();
938 if (svp == NULL)
939 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000940
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000941 return newsvobject(svp);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000942}
943
Barry Warsawf630f6b1996-12-13 01:24:29 +0000944static PyMethodDef sv_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000945 {"InterleaveFields", (PyCFunction)sv_InterleaveFields, METH_OLDARGS},
946 {"RGB8toRGB32", (PyCFunction)sv_RGB8toRGB32, METH_OLDARGS},
947 {"YUVtoRGB", (PyCFunction)sv_YUVtoRGB, METH_OLDARGS},
948 {"OpenVideo", (PyCFunction)sv_OpenVideo, METH_OLDARGS},
949 {NULL, NULL} /* Sentinel */
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000950};
951
952void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000953initsv(void)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000954{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000955 PyObject *m, *d;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000956
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000957 if (PyErr_WarnPy3k("the sv module has been removed in "
958 "Python 3.0", 2) < 0)
959 return;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000960
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000961 m = Py_InitModule("sv", sv_methods);
962 if (m == NULL)
963 return;
964 d = PyModule_GetDict(m);
965
966 SvError = PyErr_NewException("sv.error", NULL, NULL);
967 if (SvError == NULL || PyDict_SetItemString(d, "error", SvError) != 0)
968 return;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000969}