blob: 4133ae39dd63c1305f042d115cb0e3d740feaed6 [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 *
Sjoerd Mullendered59d201993-01-06 13:36:38 +000044sv_error()
45{
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 *
Sjoerd Mullendered59d201993-01-06 13:36:38 +000051svc_conversion(self, args, function, factor)
52 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +000053 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000054 void (*function)();
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000055 float factor;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000056{
Barry Warsawf630f6b1996-12-13 01:24:29 +000057 PyObject *output;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000058 int invert;
Barry Warsawf630f6b1996-12-13 01:24:29 +000059 char* outstr;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000060
Barry Warsawf630f6b1996-12-13 01:24:29 +000061 if (!PyArg_Parse(args, "i", &invert))
Sjoerd Mullendered59d201993-01-06 13:36:38 +000062 return NULL;
63
Barry Warsawf630f6b1996-12-13 01:24:29 +000064 if (!(output = PyString_FromStringAndSize(
65 NULL,
66 (int)(self->ob_info.width * self->ob_info.height * factor))))
67 {
Sjoerd Mullendered59d201993-01-06 13:36:38 +000068 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +000069 }
70 if (!(outstr = PyString_AsString(output))) {
71 Py_DECREF(output);
72 return NULL;
73 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +000074
Barry Warsawf630f6b1996-12-13 01:24:29 +000075 (*function)((boolean)invert, self->ob_capture,
76 outstr,
Sjoerd Mullendered59d201993-01-06 13:36:38 +000077 self->ob_info.width, self->ob_info.height);
78
79 return output;
80}
81
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000082/*
83 * 3 functions to convert from Starter Video YUV 4:1:1 format to
84 * Compression Library 4:2:2 Duplicate Chroma format.
85 */
Barry Warsawf630f6b1996-12-13 01:24:29 +000086static PyObject *
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000087svc_YUVtoYUV422DC(self, args)
88 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +000089 PyObject *args;
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000090{
91 if (self->ob_info.format != SV_YUV411_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +000092 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000093 return NULL;
94 }
95 return svc_conversion(self, args, yuv_sv411_to_cl422dc, 2.0);
96}
97
Barry Warsawf630f6b1996-12-13 01:24:29 +000098static PyObject *
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000099svc_YUVtoYUV422DC_quarter(self, args)
100 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000101 PyObject *args;
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000102{
103 if (self->ob_info.format != SV_YUV411_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000104 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000105 return NULL;
106 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000107 return svc_conversion(self, args,
108 yuv_sv411_to_cl422dc_quartersize, 0.5);
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000109}
110
Barry Warsawf630f6b1996-12-13 01:24:29 +0000111static PyObject *
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000112svc_YUVtoYUV422DC_sixteenth(self, args)
113 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000114 PyObject *args;
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000115{
116 if (self->ob_info.format != SV_YUV411_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000117 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000118 return NULL;
119 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000120 return svc_conversion(self, args,
121 yuv_sv411_to_cl422dc_sixteenthsize, 0.125);
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000122}
123
Barry Warsawf630f6b1996-12-13 01:24:29 +0000124static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000125svc_YUVtoRGB(self, args)
126 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000127 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000128{
129 switch (self->ob_info.format) {
130 case SV_YUV411_FRAMES:
131 case SV_YUV411_FRAMES_AND_BLANKING_BUFFER:
132 break;
133 default:
Barry Warsawf630f6b1996-12-13 01:24:29 +0000134 PyErr_SetString(SvError, "data had bad format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000135 return NULL;
136 }
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000137 return svc_conversion(self, args, svYUVtoRGB, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000138}
139
Barry Warsawf630f6b1996-12-13 01:24:29 +0000140static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000141svc_RGB8toRGB32(self, args)
142 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000143 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000144{
145 if (self->ob_info.format != SV_RGB8_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000146 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000147 return NULL;
148 }
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000149 return svc_conversion(self, args, svRGB8toRGB32, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000150}
151
Barry Warsawf630f6b1996-12-13 01:24:29 +0000152static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000153svc_InterleaveFields(self, args)
154 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000155 PyObject *args;
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 }
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000161 return svc_conversion(self, args, svInterleaveFields, 1.0);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000162}
163
Barry Warsawf630f6b1996-12-13 01:24:29 +0000164static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000165svc_GetFields(self, args)
166 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000167 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000168{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000169 PyObject *f1 = NULL;
170 PyObject *f2 = NULL;
171 PyObject *ret = NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000172 int fieldsize;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000173 char* obcapture;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000174
175 if (self->ob_info.format != SV_RGB8_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000176 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000177 return NULL;
178 }
179
180 fieldsize = self->ob_info.width * self->ob_info.height / 2;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000181 obcapture = (char*)self->ob_capture;
182
183 if (!(f1 = PyString_FromStringAndSize(obcapture, fieldsize)))
184 goto finally;
185 if (!(f2 = PyString_FromStringAndSize(obcapture + fieldsize,
186 fieldsize)))
187 goto finally;
188 ret = Py_BuildValue("(OO)", f1, f2);
189
190 finally:
191 Py_XDECREF(f1);
192 Py_XDECREF(f2);
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000193 return ret;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000194}
195
Barry Warsawf630f6b1996-12-13 01:24:29 +0000196static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000197svc_UnlockCaptureData(self, args)
198 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000199 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000200{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000201 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000202 return NULL;
203
204 if (!self->ob_mustunlock) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000205 PyErr_SetString(SvError, "buffer should not be unlocked");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000206 return NULL;
207 }
208
209 if (svUnlockCaptureData(self->ob_svideo->ob_svideo, self->ob_capture))
210 return sv_error();
211
212 self->ob_mustunlock = 0;
213
Barry Warsawf630f6b1996-12-13 01:24:29 +0000214 Py_INCREF(Py_None);
215 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000216}
217
218#ifdef USE_GL
219#include <gl.h>
220
Barry Warsawf630f6b1996-12-13 01:24:29 +0000221static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000222svc_lrectwrite(self, args)
223 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000224 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000225{
226 Screencoord x1, x2, y1, y2;
227
Barry Warsawf630f6b1996-12-13 01:24:29 +0000228 if (!PyArg_Parse(args, "(hhhh)", &x1, &x2, &y1, &y2))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000229 return NULL;
230
231 lrectwrite(x1, x2, y1, y2, (unsigned long *) self->ob_capture);
232
Barry Warsawf630f6b1996-12-13 01:24:29 +0000233 Py_INCREF(Py_None);
234 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000235}
236#endif
237
Barry Warsawf630f6b1996-12-13 01:24:29 +0000238static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239svc_writefile(self, args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000240 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000241 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000242{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000243 PyObject *file;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000244 int size;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000245 FILE* fp;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000246
Barry Warsawf630f6b1996-12-13 01:24:29 +0000247 if (!PyArg_Parse(args, "O", &file))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000248 return NULL;
249
Barry Warsawf630f6b1996-12-13 01:24:29 +0000250 if (!PyFile_Check(file)) {
251 PyErr_SetString(SvError, "not a file object");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000252 return NULL;
253 }
254
Barry Warsawf630f6b1996-12-13 01:24:29 +0000255 if (!(fp = PyFile_AsFile(file)))
256 return NULL;
257
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000258 size = self->ob_info.width * self->ob_info.height;
259
Barry Warsawf630f6b1996-12-13 01:24:29 +0000260 if (fwrite(self->ob_capture, sizeof(long), size, fp) != size) {
261 PyErr_SetString(SvError, "writing failed");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000262 return NULL;
263 }
264
Barry Warsawf630f6b1996-12-13 01:24:29 +0000265 Py_INCREF(Py_None);
266 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000267}
268
Barry Warsawf630f6b1996-12-13 01:24:29 +0000269static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000270svc_FindVisibleRegion(self, args)
271 captureobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000272 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000273{
274 void *visible;
275 int width;
276
Barry Warsawf630f6b1996-12-13 01:24:29 +0000277 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000278 return NULL;
279
Barry Warsawf630f6b1996-12-13 01:24:29 +0000280 if (svFindVisibleRegion(self->ob_svideo->ob_svideo,
281 self->ob_capture, &visible,
282 self->ob_info.width))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000283 return sv_error();
284
285 if (visible == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000286 PyErr_SetString(SvError, "data in wrong format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000287 return NULL;
288 }
289
290 return newcaptureobject(self->ob_svideo, visible, 0);
291}
292
Barry Warsawf630f6b1996-12-13 01:24:29 +0000293static PyMethodDef capture_methods[] = {
294 {"YUVtoRGB", (PyCFunction)svc_YUVtoRGB},
295 {"RGB8toRGB32", (PyCFunction)svc_RGB8toRGB32},
296 {"InterleaveFields", (PyCFunction)svc_InterleaveFields},
297 {"UnlockCaptureData", (PyCFunction)svc_UnlockCaptureData},
298 {"FindVisibleRegion", (PyCFunction)svc_FindVisibleRegion},
299 {"GetFields", (PyCFunction)svc_GetFields},
300 {"YUVtoYUV422DC", (PyCFunction)svc_YUVtoYUV422DC},
301 {"YUVtoYUV422DC_quarter",(PyCFunction)svc_YUVtoYUV422DC_quarter},
302 {"YUVtoYUV422DC_sixteenth",(PyCFunction)svc_YUVtoYUV422DC_sixteenth},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000303#ifdef USE_GL
Barry Warsawf630f6b1996-12-13 01:24:29 +0000304 {"lrectwrite", (PyCFunction)svc_lrectwrite},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000305#endif
Barry Warsawf630f6b1996-12-13 01:24:29 +0000306 {"writefile", (PyCFunction)svc_writefile},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000307 {NULL, NULL} /* sentinel */
308};
309
310static void
311capture_dealloc(self)
312 captureobject *self;
313{
314 if (self->ob_capture != NULL) {
315 if (self->ob_mustunlock)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000316 (void)svUnlockCaptureData(self->ob_svideo->ob_svideo,
317 self->ob_capture);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000318 self->ob_capture = NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000319 Py_DECREF(self->ob_svideo);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000320 self->ob_svideo = NULL;
321 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000322 PyObject_Del(self);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000323}
324
Barry Warsawf630f6b1996-12-13 01:24:29 +0000325static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000326capture_getattr(self, name)
327 svobject *self;
328 char *name;
329{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000330 return Py_FindMethod(capture_methods, (PyObject *)self, name);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000331}
332
Barry Warsawf630f6b1996-12-13 01:24:29 +0000333PyTypeObject Capturetype = {
334 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000335 0, /*ob_size*/
336 "capture", /*tp_name*/
337 sizeof(captureobject), /*tp_size*/
338 0, /*tp_itemsize*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000339 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000340 (destructor)capture_dealloc, /*tp_dealloc*/
341 0, /*tp_print*/
342 (getattrfunc)capture_getattr, /*tp_getattr*/
343 0, /*tp_setattr*/
344 0, /*tp_compare*/
345 0, /*tp_repr*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000346};
347
Barry Warsawf630f6b1996-12-13 01:24:29 +0000348static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000349newcaptureobject(self, ptr, mustunlock)
350 svobject *self;
351 void *ptr;
352 int mustunlock;
353{
354 captureobject *p;
355
Guido van Rossumb18618d2000-05-03 23:44:39 +0000356 p = PyObject_New(captureobject, &Capturetype);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000357 if (p == NULL)
358 return NULL;
359 p->ob_svideo = self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000360 Py_INCREF(self);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000361 p->ob_capture = ptr;
362 p->ob_mustunlock = mustunlock;
363 p->ob_info = self->ob_info;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000364 return (PyObject *) p;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000365}
366
Barry Warsawf630f6b1996-12-13 01:24:29 +0000367static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000368sv_GetCaptureData(self, args)
369 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000370 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000371{
372 void *ptr;
373 long fieldID;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000374 PyObject *res, *c;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000375
Barry Warsawf630f6b1996-12-13 01:24:29 +0000376 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000377 return NULL;
378
379 if (svGetCaptureData(self->ob_svideo, &ptr, &fieldID))
380 return sv_error();
381
382 if (ptr == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000383 PyErr_SetString(SvError, "no data available");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000384 return NULL;
385 }
386
387 c = newcaptureobject(self, ptr, 1);
388 if (c == NULL)
389 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000390 res = Py_BuildValue("(Oi)", c, fieldID);
391 Py_DECREF(c);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000392 return res;
393}
394
Barry Warsawf630f6b1996-12-13 01:24:29 +0000395static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000396sv_BindGLWindow(self, args)
397 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000398 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000399{
400 long wid;
401 int mode;
402
Barry Warsawf630f6b1996-12-13 01:24:29 +0000403 if (!PyArg_Parse(args, "(ii)", &wid, &mode))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000404 return NULL;
405
406 if (svBindGLWindow(self->ob_svideo, wid, mode))
407 return sv_error();
408
Barry Warsawf630f6b1996-12-13 01:24:29 +0000409 Py_INCREF(Py_None);
410 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000411}
412
Barry Warsawf630f6b1996-12-13 01:24:29 +0000413static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000414sv_EndContinuousCapture(self, args)
415 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000416 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000417{
418
Barry Warsawf630f6b1996-12-13 01:24:29 +0000419 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000420 return NULL;
421
422 if (svEndContinuousCapture(self->ob_svideo))
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_IsVideoDisplayed(self, args)
431 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000432 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000433{
434 int v;
435
Barry Warsawf630f6b1996-12-13 01:24:29 +0000436 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000437 return NULL;
438
439 v = svIsVideoDisplayed(self->ob_svideo);
440 if (v == -1)
441 return sv_error();
442
Barry Warsawf630f6b1996-12-13 01:24:29 +0000443 return PyInt_FromLong((long) v);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000444}
445
Barry Warsawf630f6b1996-12-13 01:24:29 +0000446static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000447sv_OutputOffset(self, args)
448 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000449 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000450{
451 int x_offset;
452 int y_offset;
453
Barry Warsawf630f6b1996-12-13 01:24:29 +0000454 if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000455 return NULL;
456
457 if (svOutputOffset(self->ob_svideo, x_offset, y_offset))
458 return sv_error();
459
Barry Warsawf630f6b1996-12-13 01:24:29 +0000460 Py_INCREF(Py_None);
461 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000462}
463
Barry Warsawf630f6b1996-12-13 01:24:29 +0000464static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000465sv_PutFrame(self, args)
466 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000467 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000468{
469 char *buffer;
470
Barry Warsawf630f6b1996-12-13 01:24:29 +0000471 if (!PyArg_Parse(args, "s", &buffer))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000472 return NULL;
473
474 if (svPutFrame(self->ob_svideo, buffer))
475 return sv_error();
476
Barry Warsawf630f6b1996-12-13 01:24:29 +0000477 Py_INCREF(Py_None);
478 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000479}
480
Barry Warsawf630f6b1996-12-13 01:24:29 +0000481static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000482sv_QuerySize(self, args)
483 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000484 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000485{
486 int w;
487 int h;
488 int rw;
489 int rh;
490
Barry Warsawf630f6b1996-12-13 01:24:29 +0000491 if (!PyArg_Parse(args, "(ii)", &w, &h))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000492 return NULL;
493
494 if (svQuerySize(self->ob_svideo, w, h, &rw, &rh))
495 return sv_error();
496
Barry Warsawf630f6b1996-12-13 01:24:29 +0000497 return Py_BuildValue("(ii)", (long) rw, (long) rh);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000498}
499
Barry Warsawf630f6b1996-12-13 01:24:29 +0000500static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000501sv_SetSize(self, args)
502 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000503 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000504{
505 int w;
506 int h;
507
Barry Warsawf630f6b1996-12-13 01:24:29 +0000508 if (!PyArg_Parse(args, "(ii)", &w, &h))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000509 return NULL;
510
511 if (svSetSize(self->ob_svideo, w, h))
512 return sv_error();
513
Barry Warsawf630f6b1996-12-13 01:24:29 +0000514 Py_INCREF(Py_None);
515 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000516}
517
Barry Warsawf630f6b1996-12-13 01:24:29 +0000518static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000519sv_SetStdDefaults(self, args)
520 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000521 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000522{
523
Barry Warsawf630f6b1996-12-13 01:24:29 +0000524 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000525 return NULL;
526
527 if (svSetStdDefaults(self->ob_svideo))
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_UseExclusive(self, args)
536 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000537 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000538{
539 boolean onoff;
540 int mode;
541
Barry Warsawf630f6b1996-12-13 01:24:29 +0000542 if (!PyArg_Parse(args, "(ii)", &onoff, &mode))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000543 return NULL;
544
545 if (svUseExclusive(self->ob_svideo, onoff, mode))
546 return sv_error();
547
Barry Warsawf630f6b1996-12-13 01:24:29 +0000548 Py_INCREF(Py_None);
549 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000550}
551
Barry Warsawf630f6b1996-12-13 01:24:29 +0000552static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000553sv_WindowOffset(self, args)
554 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000555 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000556{
557 int x_offset;
558 int y_offset;
559
Barry Warsawf630f6b1996-12-13 01:24:29 +0000560 if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000561 return NULL;
562
563 if (svWindowOffset(self->ob_svideo, x_offset, y_offset))
564 return sv_error();
565
Barry Warsawf630f6b1996-12-13 01:24:29 +0000566 Py_INCREF(Py_None);
567 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000568}
569
Barry Warsawf630f6b1996-12-13 01:24:29 +0000570static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000571sv_CaptureBurst(self, args)
572 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000573 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000574{
575 int bytes, i;
576 svCaptureInfo info;
577 void *bitvector = NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000578 PyObject *videodata = NULL;
579 PyObject *bitvecobj = NULL;
Guido van Rossumc88c9cb1996-12-17 20:43:55 +0000580 PyObject *res = NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000581 static PyObject *evenitem, *odditem;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000582
Barry Warsawf630f6b1996-12-13 01:24:29 +0000583 if (!PyArg_Parse(args, "(iiiii)", &info.format,
584 &info.width, &info.height,
585 &info.size, &info.samplingrate))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000586 return NULL;
587
588 switch (info.format) {
589 case SV_RGB8_FRAMES:
590 bitvector = malloc(SV_BITVEC_SIZE(info.size));
591 break;
592 case SV_YUV411_FRAMES_AND_BLANKING_BUFFER:
593 break;
594 default:
Barry Warsawf630f6b1996-12-13 01:24:29 +0000595 PyErr_SetString(SvError, "illegal format specified");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000596 return NULL;
597 }
598
599 if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes)) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000600 res = sv_error();
601 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000602 }
603
Barry Warsawf630f6b1996-12-13 01:24:29 +0000604 if (!(videodata = PyString_FromStringAndSize(NULL, bytes)))
605 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000606
607 /* XXX -- need to do something about the bitvector */
Barry Warsawf630f6b1996-12-13 01:24:29 +0000608 {
609 char* str = PyString_AsString(videodata);
610 if (!str)
611 goto finally;
612
613 if (svCaptureBurst(self->ob_svideo, &info, str, bitvector)) {
614 res = sv_error();
615 goto finally;
616 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000617 }
618
619 if (bitvector) {
620 if (evenitem == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000621 if (!(evenitem = PyInt_FromLong(0)))
622 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000623 }
624 if (odditem == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000625 if (!(odditem = PyInt_FromLong(1)))
626 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000627 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000628 if (!(bitvecobj = PyTuple_New(2 * info.size)))
629 goto finally;
630
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000631 for (i = 0; i < 2 * info.size; i++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000632 int sts;
633
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000634 if (SV_GET_FIELD(bitvector, i) == SV_EVEN_FIELD) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000635 Py_INCREF(evenitem);
636 sts = PyTuple_SetItem(bitvecobj, i, evenitem);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000637 } else {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000638 Py_INCREF(odditem);
639 sts = PyTuple_SetItem(bitvecobj, i, odditem);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000640 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000641 if (sts < 0)
642 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000643 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000644 } else {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000645 bitvecobj = Py_None;
646 Py_INCREF(Py_None);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000647 }
648
Barry Warsawf630f6b1996-12-13 01:24:29 +0000649 res = Py_BuildValue("((iiiii)OO)", info.format,
650 info.width, info.height,
651 info.size, info.samplingrate,
652 videodata, bitvecobj);
653
654 finally:
655 if (bitvector)
656 free(bitvector);
657
658 Py_XDECREF(videodata);
659 Py_XDECREF(bitvecobj);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000660 return res;
661}
662
Barry Warsawf630f6b1996-12-13 01:24:29 +0000663static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000664sv_CaptureOneFrame(self, args)
665 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000666 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000667{
668 svCaptureInfo info;
669 int format, width, height;
670 int bytes;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000671 PyObject *videodata = NULL;
672 PyObject *res = NULL;
Guido van Rossumc88c9cb1996-12-17 20:43:55 +0000673 char *str;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000674
Barry Warsawf630f6b1996-12-13 01:24:29 +0000675 if (!PyArg_Parse(args, "(iii)", &format, &width, &height))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000676 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000677
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000678 info.format = format;
679 info.width = width;
680 info.height = height;
681 info.size = 0;
682 info.samplingrate = 0;
683 if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes))
684 return sv_error();
Barry Warsawf630f6b1996-12-13 01:24:29 +0000685
686 if (!(videodata = PyString_FromStringAndSize(NULL, bytes)))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000687 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000688
Guido van Rossumc88c9cb1996-12-17 20:43:55 +0000689 str = PyString_AsString(videodata);
690 if (!str)
691 goto finally;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000692
693 if (svCaptureOneFrame(self->ob_svideo, format, &width, &height, str)) {
694 res = sv_error();
695 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000696 }
697
Barry Warsawf630f6b1996-12-13 01:24:29 +0000698 res = Py_BuildValue("(iiO)", width, height, videodata);
699
700 finally:
701 Py_XDECREF(videodata);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000702 return res;
703}
704
Barry Warsawf630f6b1996-12-13 01:24:29 +0000705static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000706sv_InitContinuousCapture(self, args)
707 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000708 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000709{
710 svCaptureInfo info;
711
Barry Warsawf630f6b1996-12-13 01:24:29 +0000712 if (!PyArg_Parse(args, "(iiiii)", &info.format,
713 &info.width, &info.height,
714 &info.size, &info.samplingrate))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000715 return NULL;
716
717 if (svInitContinuousCapture(self->ob_svideo, &info))
718 return sv_error();
719
720 self->ob_info = info;
721
Barry Warsawf630f6b1996-12-13 01:24:29 +0000722 return Py_BuildValue("(iiiii)", info.format, info.width, info.height,
723 info.size, info.samplingrate);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000724}
725
Barry Warsawf630f6b1996-12-13 01:24:29 +0000726static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000727sv_LoadMap(self, args)
728 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000729 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000730{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000731 PyObject *rgb;
732 PyObject *res = NULL;
733 rgb_tuple *mapp = NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000734 int maptype;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000735 int i, j; /* indices */
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000736
Barry Warsawf630f6b1996-12-13 01:24:29 +0000737 if (!PyArg_Parse(args, "(iO)", &maptype, &rgb))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000738 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000739
740 if (!PyList_Check(rgb) || PyList_Size(rgb) != 256) {
741 PyErr_BadArgument();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000742 return NULL;
743 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000744
745 if (!(mapp = PyMem_NEW(rgb_tuple, 256)))
746 return PyErr_NoMemory();
747
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000748 for (i = 0; i < 256; i++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000749 PyObject* v = PyList_GetItem(rgb, i);
750 if (!v)
751 goto finally;
752
753 if (!PyTuple_Check(v) || PyTuple_Size(v) != 3) {
754 PyErr_BadArgument();
755 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000756 }
757 for (j = 0; j < 3; j++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000758 PyObject* cell = PyTuple_GetItem(v, j);
759 if (!cell)
760 goto finally;
761
762 if (!PyInt_Check(cell)) {
763 PyErr_BadArgument();
764 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000765 }
766 switch (j) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000767 case 0: mapp[i].red = PyInt_AsLong(cell); break;
768 case 1: mapp[i].blue = PyInt_AsLong(cell); break;
769 case 2: mapp[i].green = PyInt_AsLong(cell); break;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000770 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000771 if (PyErr_Occurred())
772 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000773 }
774 }
775
776 if (svLoadMap(self->ob_svideo, maptype, mapp)) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000777 res = sv_error();
778 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000779 }
780
Barry Warsawf630f6b1996-12-13 01:24:29 +0000781 Py_INCREF(Py_None);
782 res = Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000783
Barry Warsawf630f6b1996-12-13 01:24:29 +0000784 finally:
785 PyMem_DEL(mapp);
786 return res;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000787}
788
Barry Warsawf630f6b1996-12-13 01:24:29 +0000789static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000790sv_CloseVideo(self, args)
791 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000792 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000793{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000794 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000795 return NULL;
796
797 if (svCloseVideo(self->ob_svideo))
798 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000799
Barry Warsawf630f6b1996-12-13 01:24:29 +0000800 self->ob_svideo = NULL;
801 Py_INCREF(Py_None);
802 return Py_None;
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 +0000806doParams(self, args, func, modified)
807 svobject *self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000808 PyObject *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000809 int (*func)(SV_nodeP, long *, int);
810 int modified;
811{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000812 PyObject *list;
813 PyObject *res = NULL;
814 long *PVbuffer = NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000815 long length;
816 int i;
817
Barry Warsawf630f6b1996-12-13 01:24:29 +0000818 if (!PyArg_Parse(args, "O", &list))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000819 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000820
821 if (!PyList_Check(list)) {
822 PyErr_BadArgument();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000823 return NULL;
824 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000825
826 if ((length = PyList_Size(list)) < 0)
827 return NULL;
828
829 PVbuffer = PyMem_NEW(long, length);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000830 if (PVbuffer == NULL)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000831 return PyErr_NoMemory();
832
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000833 for (i = 0; i < length; i++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000834 PyObject *v = PyList_GetItem(list, i);
835 if (!v)
836 goto finally;
837
838 if (!PyInt_Check(v)) {
839 PyErr_BadArgument();
840 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000841 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000842 PVbuffer[i] = PyInt_AsLong(v);
843 /* can't just test the return value, because what if the
844 value was -1?!
845 */
846 if (PVbuffer[i] == -1 && PyErr_Occurred())
847 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000848 }
849
850 if ((*func)(self->ob_svideo, PVbuffer, length)) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000851 res = sv_error();
852 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000853 }
854
855 if (modified) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000856 for (i = 0; i < length; i++) {
857 PyObject* v = PyInt_FromLong(PVbuffer[i]);
858 if (!v || PyList_SetItem(list, i, v) < 0)
859 goto finally;
860 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000861 }
862
Barry Warsawf630f6b1996-12-13 01:24:29 +0000863 Py_INCREF(Py_None);
864 res = Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000865
Barry Warsawf630f6b1996-12-13 01:24:29 +0000866 finally:
867 PyMem_DEL(PVbuffer);
868 return res;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000869}
870
Barry Warsawf630f6b1996-12-13 01:24:29 +0000871static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000872sv_GetParam(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000873 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000874{
875 return doParams(self, args, svGetParam, 1);
876}
877
Barry Warsawf630f6b1996-12-13 01:24:29 +0000878static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000879sv_GetParamRange(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000880 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000881{
882 return doParams(self, args, svGetParamRange, 1);
883}
884
Barry Warsawf630f6b1996-12-13 01:24:29 +0000885static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000886sv_SetParam(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000887 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000888{
889 return doParams(self, args, svSetParam, 0);
890}
891
Barry Warsawf630f6b1996-12-13 01:24:29 +0000892static PyMethodDef svideo_methods[] = {
893 {"BindGLWindow", (PyCFunction)sv_BindGLWindow},
894 {"EndContinuousCapture",(PyCFunction)sv_EndContinuousCapture},
895 {"IsVideoDisplayed", (PyCFunction)sv_IsVideoDisplayed},
896 {"OutputOffset", (PyCFunction)sv_OutputOffset},
897 {"PutFrame", (PyCFunction)sv_PutFrame},
898 {"QuerySize", (PyCFunction)sv_QuerySize},
899 {"SetSize", (PyCFunction)sv_SetSize},
900 {"SetStdDefaults", (PyCFunction)sv_SetStdDefaults},
901 {"UseExclusive", (PyCFunction)sv_UseExclusive},
902 {"WindowOffset", (PyCFunction)sv_WindowOffset},
903 {"InitContinuousCapture",(PyCFunction)sv_InitContinuousCapture},
904 {"CaptureBurst", (PyCFunction)sv_CaptureBurst},
905 {"CaptureOneFrame", (PyCFunction)sv_CaptureOneFrame},
906 {"GetCaptureData", (PyCFunction)sv_GetCaptureData},
907 {"CloseVideo", (PyCFunction)sv_CloseVideo},
908 {"LoadMap", (PyCFunction)sv_LoadMap},
909 {"GetParam", (PyCFunction)sv_GetParam},
910 {"GetParamRange", (PyCFunction)sv_GetParamRange},
911 {"SetParam", (PyCFunction)sv_SetParam},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000912 {NULL, NULL} /* sentinel */
913};
914
Barry Warsawf630f6b1996-12-13 01:24:29 +0000915static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000916sv_conversion(self, args, function, inputfactor, factor)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000917 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000918 void (*function)();
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000919 int inputfactor;
920 float factor;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000921{
922 int invert, width, height, inputlength;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000923 char *input, *str;
924 PyObject *output;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000925
Barry Warsawf630f6b1996-12-13 01:24:29 +0000926 if (!PyArg_Parse(args, "(is#ii)", &invert,
927 &input, &inputlength, &width, &height))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000928 return NULL;
929
930 if (width * height * inputfactor > inputlength) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000931 PyErr_SetString(SvError, "input buffer not long enough");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000932 return NULL;
933 }
934
Barry Warsawf630f6b1996-12-13 01:24:29 +0000935 if (!(output = PyString_FromStringAndSize(NULL,
936 (int)(width * height * factor))))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000937 return NULL;
938
Barry Warsawf630f6b1996-12-13 01:24:29 +0000939 str = PyString_AsString(output);
940 if (!str) {
941 Py_DECREF(output);
942 return NULL;
943 }
944 (*function)(invert, input, str, width, height);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000945
946 return output;
947}
948
Barry Warsawf630f6b1996-12-13 01:24:29 +0000949static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000950sv_InterleaveFields(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000951 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000952{
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000953 return sv_conversion(self, args, svInterleaveFields, 1, 1.0);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000954}
955
Barry Warsawf630f6b1996-12-13 01:24:29 +0000956static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000957sv_RGB8toRGB32(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000958 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000959{
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000960 return sv_conversion(self, args, svRGB8toRGB32, 1, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000961}
962
Barry Warsawf630f6b1996-12-13 01:24:29 +0000963static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000964sv_YUVtoRGB(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000965 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000966{
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000967 return sv_conversion(self, args, svYUVtoRGB, 2, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000968}
969
970static void
971svideo_dealloc(self)
972 svobject *self;
973{
974 if (self->ob_svideo != NULL)
975 (void) svCloseVideo(self->ob_svideo);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000976 PyObject_Del(self);
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 +0000980svideo_getattr(self, name)
981 svobject *self;
982 char *name;
983{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000984 return Py_FindMethod(svideo_methods, (PyObject *)self, name);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000985}
986
Barry Warsawf630f6b1996-12-13 01:24:29 +0000987PyTypeObject Svtype = {
988 PyObject_HEAD_INIT(&PyType_Type)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000989 0, /*ob_size*/
990 "sv", /*tp_name*/
991 sizeof(svobject), /*tp_size*/
992 0, /*tp_itemsize*/
993 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000994 (destructor)svideo_dealloc, /*tp_dealloc*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000995 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000996 (getattrfunc)svideo_getattr, /*tp_getattr*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000997 0, /*tp_setattr*/
998 0, /*tp_compare*/
999 0, /*tp_repr*/
1000};
1001
Barry Warsawf630f6b1996-12-13 01:24:29 +00001002static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001003newsvobject(svp)
1004 SV_nodeP svp;
1005{
1006 svobject *p;
1007
Guido van Rossumb18618d2000-05-03 23:44:39 +00001008 p = PyObject_New(svobject, &Svtype);
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001009 if (p == NULL)
1010 return NULL;
1011 p->ob_svideo = svp;
1012 p->ob_info.format = 0;
1013 p->ob_info.size = 0;
1014 p->ob_info.width = 0;
1015 p->ob_info.height = 0;
1016 p->ob_info.samplingrate = 0;
Barry Warsawf630f6b1996-12-13 01:24:29 +00001017 return (PyObject *) p;
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001018}
1019
Barry Warsawf630f6b1996-12-13 01:24:29 +00001020static PyObject *
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001021sv_OpenVideo(self, args)
Barry Warsawf630f6b1996-12-13 01:24:29 +00001022 PyObject *self, *args;
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001023{
1024 SV_nodeP svp;
1025
Barry Warsawf630f6b1996-12-13 01:24:29 +00001026 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001027 return NULL;
1028
1029 svp = svOpenVideo();
1030 if (svp == NULL)
1031 return sv_error();
1032
1033 return newsvobject(svp);
1034}
1035
Barry Warsawf630f6b1996-12-13 01:24:29 +00001036static PyMethodDef sv_methods[] = {
1037 {"InterleaveFields", (PyCFunction)sv_InterleaveFields},
1038 {"RGB8toRGB32", (PyCFunction)sv_RGB8toRGB32},
1039 {"YUVtoRGB", (PyCFunction)sv_YUVtoRGB},
1040 {"OpenVideo", (PyCFunction)sv_OpenVideo},
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001041 {NULL, NULL} /* Sentinel */
1042};
1043
1044void
1045initsv()
1046{
Barry Warsawf630f6b1996-12-13 01:24:29 +00001047 PyObject *m, *d;
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001048
Barry Warsawf630f6b1996-12-13 01:24:29 +00001049 m = Py_InitModule("sv", sv_methods);
1050 d = PyModule_GetDict(m);
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001051
Guido van Rossum0cb96de1997-10-01 04:29:29 +00001052 SvError = PyErr_NewException("sv.error", NULL, NULL);
Barry Warsawf630f6b1996-12-13 01:24:29 +00001053 if (SvError == NULL || PyDict_SetItemString(d, "error", SvError) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00001054 return;
Sjoerd Mullendered59d201993-01-06 13:36:38 +00001055}