blob: fb58f19cc3517b09fd9db7b1faa3666c25223e8d [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"
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000012#include "yuv.h" /* for YUV conversion functions */
Sjoerd Mullendered59d201993-01-06 13:36:38 +000013
14typedef struct {
Barry Warsawf630f6b1996-12-13 01:24:29 +000015 PyObject_HEAD
Sjoerd Mullendered59d201993-01-06 13:36:38 +000016 SV_nodeP ob_svideo;
17 svCaptureInfo ob_info;
18} svobject;
19
20typedef struct {
Barry Warsawf630f6b1996-12-13 01:24:29 +000021 PyObject_HEAD
Sjoerd Mullendered59d201993-01-06 13:36:38 +000022 void *ob_capture;
23 int ob_mustunlock;
24 svCaptureInfo ob_info;
25 svobject *ob_svideo;
26} captureobject;
27
Barry Warsawf630f6b1996-12-13 01:24:29 +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{
Barry Warsawf630f6b1996-12-13 01:24:29 +000036 PyErr_SetString(SvError, svStrerror(svideo_errno));
Sjoerd Mullendered59d201993-01-06 13:36:38 +000037 return NULL;
38}
39
Barry Warsawf630f6b1996-12-13 01:24:29 +000040static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +000041svc_conversion(captureobject *self, PyObject *args, void (*function)(), float factor)
Sjoerd Mullendered59d201993-01-06 13:36:38 +000042{
Barry Warsawf630f6b1996-12-13 01:24:29 +000043 PyObject *output;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000044 int invert;
Barry Warsawf630f6b1996-12-13 01:24:29 +000045 char* outstr;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000046
Barry Warsawf630f6b1996-12-13 01:24:29 +000047 if (!PyArg_Parse(args, "i", &invert))
Sjoerd Mullendered59d201993-01-06 13:36:38 +000048 return NULL;
49
Barry Warsawf630f6b1996-12-13 01:24:29 +000050 if (!(output = PyString_FromStringAndSize(
51 NULL,
52 (int)(self->ob_info.width * self->ob_info.height * factor))))
53 {
Sjoerd Mullendered59d201993-01-06 13:36:38 +000054 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +000055 }
56 if (!(outstr = PyString_AsString(output))) {
57 Py_DECREF(output);
58 return NULL;
59 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +000060
Barry Warsawf630f6b1996-12-13 01:24:29 +000061 (*function)((boolean)invert, self->ob_capture,
62 outstr,
Sjoerd Mullendered59d201993-01-06 13:36:38 +000063 self->ob_info.width, self->ob_info.height);
64
65 return output;
66}
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{
75 if (self->ob_info.format != SV_YUV411_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +000076 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000077 return NULL;
78 }
79 return svc_conversion(self, args, yuv_sv411_to_cl422dc, 2.0);
80}
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{
85 if (self->ob_info.format != SV_YUV411_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +000086 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000087 return NULL;
88 }
Barry Warsawf630f6b1996-12-13 01:24:29 +000089 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{
96 if (self->ob_info.format != SV_YUV411_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +000097 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +000098 return NULL;
99 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000100 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{
107 switch (self->ob_info.format) {
108 case SV_YUV411_FRAMES:
109 case SV_YUV411_FRAMES_AND_BLANKING_BUFFER:
110 break;
111 default:
Barry Warsawf630f6b1996-12-13 01:24:29 +0000112 PyErr_SetString(SvError, "data had bad format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000113 return NULL;
114 }
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000115 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{
121 if (self->ob_info.format != SV_RGB8_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000122 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000123 return NULL;
124 }
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000125 return svc_conversion(self, args, 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{
131 if (self->ob_info.format != SV_RGB8_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000132 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000133 return NULL;
134 }
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000135 return svc_conversion(self, args, 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{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000141 PyObject *f1 = NULL;
142 PyObject *f2 = NULL;
143 PyObject *ret = NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000144 int fieldsize;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000145 char* obcapture;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000146
147 if (self->ob_info.format != SV_RGB8_FRAMES) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000148 PyErr_SetString(SvError, "data has bad format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000149 return NULL;
150 }
151
152 fieldsize = self->ob_info.width * self->ob_info.height / 2;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000153 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;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000160 ret = PyTuple_Pack(2, f1, f2);
Barry Warsawf630f6b1996-12-13 01:24:29 +0000161
162 finally:
163 Py_XDECREF(f1);
164 Py_XDECREF(f2);
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000165 return ret;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000166}
167
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{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000171 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000172 return NULL;
173
174 if (!self->ob_mustunlock) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000175 PyErr_SetString(SvError, "buffer should not be unlocked");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000176 return NULL;
177 }
178
179 if (svUnlockCaptureData(self->ob_svideo->ob_svideo, self->ob_capture))
180 return sv_error();
181
182 self->ob_mustunlock = 0;
183
Barry Warsawf630f6b1996-12-13 01:24:29 +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{
194 Screencoord x1, x2, y1, y2;
195
Barry Warsawf630f6b1996-12-13 01:24:29 +0000196 if (!PyArg_Parse(args, "(hhhh)", &x1, &x2, &y1, &y2))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000197 return NULL;
198
199 lrectwrite(x1, x2, y1, y2, (unsigned long *) self->ob_capture);
200
Barry Warsawf630f6b1996-12-13 01:24:29 +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{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000209 PyObject *file;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000210 int size;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000211 FILE* fp;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000212
Barry Warsawf630f6b1996-12-13 01:24:29 +0000213 if (!PyArg_Parse(args, "O", &file))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000214 return NULL;
215
Barry Warsawf630f6b1996-12-13 01:24:29 +0000216 if (!PyFile_Check(file)) {
217 PyErr_SetString(SvError, "not a file object");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000218 return NULL;
219 }
220
Barry Warsawf630f6b1996-12-13 01:24:29 +0000221 if (!(fp = PyFile_AsFile(file)))
222 return NULL;
223
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000224 size = self->ob_info.width * self->ob_info.height;
225
Barry Warsawf630f6b1996-12-13 01:24:29 +0000226 if (fwrite(self->ob_capture, sizeof(long), size, fp) != size) {
227 PyErr_SetString(SvError, "writing failed");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000228 return NULL;
229 }
230
Barry Warsawf630f6b1996-12-13 01:24:29 +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{
238 void *visible;
239 int width;
240
Barry Warsawf630f6b1996-12-13 01:24:29 +0000241 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000242 return NULL;
243
Barry Warsawf630f6b1996-12-13 01:24:29 +0000244 if (svFindVisibleRegion(self->ob_svideo->ob_svideo,
245 self->ob_capture, &visible,
246 self->ob_info.width))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000247 return sv_error();
248
249 if (visible == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000250 PyErr_SetString(SvError, "data in wrong format");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000251 return NULL;
252 }
253
254 return newcaptureobject(self->ob_svideo, visible, 0);
255}
256
Barry Warsawf630f6b1996-12-13 01:24:29 +0000257static PyMethodDef capture_methods[] = {
Martin v. Löwis43b936d2002-01-17 23:15:58 +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
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000268 {"lrectwrite", (PyCFunction)svc_lrectwrite, METH_OLDARGS},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000269#endif
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000270 {"writefile", (PyCFunction)svc_writefile, METH_OLDARGS},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000271 {NULL, NULL} /* sentinel */
272};
273
274static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000275capture_dealloc(captureobject *self)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000276{
277 if (self->ob_capture != NULL) {
278 if (self->ob_mustunlock)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000279 (void)svUnlockCaptureData(self->ob_svideo->ob_svideo,
280 self->ob_capture);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000281 self->ob_capture = NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000282 Py_DECREF(self->ob_svideo);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000283 self->ob_svideo = NULL;
284 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000285 PyObject_Del(self);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000286}
287
Barry Warsawf630f6b1996-12-13 01:24:29 +0000288static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000289capture_getattr(svobject *self, char *name)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000290{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000291 return Py_FindMethod(capture_methods, (PyObject *)self, name);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000292}
293
Barry Warsawf630f6b1996-12-13 01:24:29 +0000294PyTypeObject Capturetype = {
295 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000296 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000297 "sv.capture", /*tp_name*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000298 sizeof(captureobject), /*tp_size*/
299 0, /*tp_itemsize*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000300 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000301 (destructor)capture_dealloc, /*tp_dealloc*/
302 0, /*tp_print*/
303 (getattrfunc)capture_getattr, /*tp_getattr*/
304 0, /*tp_setattr*/
305 0, /*tp_compare*/
306 0, /*tp_repr*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000307};
308
Barry Warsawf630f6b1996-12-13 01:24:29 +0000309static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000310newcaptureobject(svobject *self, void *ptr, int mustunlock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000311{
312 captureobject *p;
313
Guido van Rossumb18618d2000-05-03 23:44:39 +0000314 p = PyObject_New(captureobject, &Capturetype);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000315 if (p == NULL)
316 return NULL;
317 p->ob_svideo = self;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000318 Py_INCREF(self);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000319 p->ob_capture = ptr;
320 p->ob_mustunlock = mustunlock;
321 p->ob_info = self->ob_info;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000322 return (PyObject *) p;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000323}
324
Barry Warsawf630f6b1996-12-13 01:24:29 +0000325static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000326sv_GetCaptureData(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000327{
328 void *ptr;
329 long fieldID;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000330 PyObject *res, *c;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000331
Barry Warsawf630f6b1996-12-13 01:24:29 +0000332 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000333 return NULL;
334
335 if (svGetCaptureData(self->ob_svideo, &ptr, &fieldID))
336 return sv_error();
337
338 if (ptr == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000339 PyErr_SetString(SvError, "no data available");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000340 return NULL;
341 }
342
343 c = newcaptureobject(self, ptr, 1);
344 if (c == NULL)
345 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000346 res = Py_BuildValue("(Oi)", c, fieldID);
347 Py_DECREF(c);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000348 return res;
349}
350
Barry Warsawf630f6b1996-12-13 01:24:29 +0000351static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000352sv_BindGLWindow(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000353{
354 long wid;
355 int mode;
356
Barry Warsawf630f6b1996-12-13 01:24:29 +0000357 if (!PyArg_Parse(args, "(ii)", &wid, &mode))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000358 return NULL;
359
360 if (svBindGLWindow(self->ob_svideo, wid, mode))
361 return sv_error();
362
Barry Warsawf630f6b1996-12-13 01:24:29 +0000363 Py_INCREF(Py_None);
364 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000365}
366
Barry Warsawf630f6b1996-12-13 01:24:29 +0000367static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000368sv_EndContinuousCapture(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000369{
370
Barry Warsawf630f6b1996-12-13 01:24:29 +0000371 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000372 return NULL;
373
374 if (svEndContinuousCapture(self->ob_svideo))
375 return sv_error();
376
Barry Warsawf630f6b1996-12-13 01:24:29 +0000377 Py_INCREF(Py_None);
378 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000379}
380
Barry Warsawf630f6b1996-12-13 01:24:29 +0000381static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000382sv_IsVideoDisplayed(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000383{
384 int v;
385
Barry Warsawf630f6b1996-12-13 01:24:29 +0000386 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000387 return NULL;
388
389 v = svIsVideoDisplayed(self->ob_svideo);
390 if (v == -1)
391 return sv_error();
392
Barry Warsawf630f6b1996-12-13 01:24:29 +0000393 return PyInt_FromLong((long) v);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000394}
395
Barry Warsawf630f6b1996-12-13 01:24:29 +0000396static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000397sv_OutputOffset(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000398{
399 int x_offset;
400 int y_offset;
401
Barry Warsawf630f6b1996-12-13 01:24:29 +0000402 if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000403 return NULL;
404
405 if (svOutputOffset(self->ob_svideo, x_offset, y_offset))
406 return sv_error();
407
Barry Warsawf630f6b1996-12-13 01:24:29 +0000408 Py_INCREF(Py_None);
409 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000410}
411
Barry Warsawf630f6b1996-12-13 01:24:29 +0000412static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000413sv_PutFrame(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000414{
415 char *buffer;
416
Barry Warsawf630f6b1996-12-13 01:24:29 +0000417 if (!PyArg_Parse(args, "s", &buffer))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000418 return NULL;
419
420 if (svPutFrame(self->ob_svideo, buffer))
421 return sv_error();
422
Barry Warsawf630f6b1996-12-13 01:24:29 +0000423 Py_INCREF(Py_None);
424 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000425}
426
Barry Warsawf630f6b1996-12-13 01:24:29 +0000427static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000428sv_QuerySize(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000429{
430 int w;
431 int h;
432 int rw;
433 int rh;
434
Barry Warsawf630f6b1996-12-13 01:24:29 +0000435 if (!PyArg_Parse(args, "(ii)", &w, &h))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000436 return NULL;
437
438 if (svQuerySize(self->ob_svideo, w, h, &rw, &rh))
439 return sv_error();
440
Barry Warsawf630f6b1996-12-13 01:24:29 +0000441 return Py_BuildValue("(ii)", (long) rw, (long) rh);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000442}
443
Barry Warsawf630f6b1996-12-13 01:24:29 +0000444static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000445sv_SetSize(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000446{
447 int w;
448 int h;
449
Barry Warsawf630f6b1996-12-13 01:24:29 +0000450 if (!PyArg_Parse(args, "(ii)", &w, &h))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000451 return NULL;
452
453 if (svSetSize(self->ob_svideo, w, h))
454 return sv_error();
455
Barry Warsawf630f6b1996-12-13 01:24:29 +0000456 Py_INCREF(Py_None);
457 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000458}
459
Barry Warsawf630f6b1996-12-13 01:24:29 +0000460static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000461sv_SetStdDefaults(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000462{
463
Barry Warsawf630f6b1996-12-13 01:24:29 +0000464 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000465 return NULL;
466
467 if (svSetStdDefaults(self->ob_svideo))
468 return sv_error();
469
Barry Warsawf630f6b1996-12-13 01:24:29 +0000470 Py_INCREF(Py_None);
471 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000472}
473
Barry Warsawf630f6b1996-12-13 01:24:29 +0000474static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000475sv_UseExclusive(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000476{
477 boolean onoff;
478 int mode;
479
Barry Warsawf630f6b1996-12-13 01:24:29 +0000480 if (!PyArg_Parse(args, "(ii)", &onoff, &mode))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000481 return NULL;
482
483 if (svUseExclusive(self->ob_svideo, onoff, mode))
484 return sv_error();
485
Barry Warsawf630f6b1996-12-13 01:24:29 +0000486 Py_INCREF(Py_None);
487 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000488}
489
Barry Warsawf630f6b1996-12-13 01:24:29 +0000490static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000491sv_WindowOffset(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000492{
493 int x_offset;
494 int y_offset;
495
Barry Warsawf630f6b1996-12-13 01:24:29 +0000496 if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000497 return NULL;
498
499 if (svWindowOffset(self->ob_svideo, x_offset, y_offset))
500 return sv_error();
501
Barry Warsawf630f6b1996-12-13 01:24:29 +0000502 Py_INCREF(Py_None);
503 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000504}
505
Barry Warsawf630f6b1996-12-13 01:24:29 +0000506static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000507sv_CaptureBurst(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000508{
509 int bytes, i;
510 svCaptureInfo info;
511 void *bitvector = NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000512 PyObject *videodata = NULL;
513 PyObject *bitvecobj = NULL;
Guido van Rossumc88c9cb1996-12-17 20:43:55 +0000514 PyObject *res = NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000515 static PyObject *evenitem, *odditem;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000516
Barry Warsawf630f6b1996-12-13 01:24:29 +0000517 if (!PyArg_Parse(args, "(iiiii)", &info.format,
518 &info.width, &info.height,
519 &info.size, &info.samplingrate))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000520 return NULL;
521
522 switch (info.format) {
523 case SV_RGB8_FRAMES:
524 bitvector = malloc(SV_BITVEC_SIZE(info.size));
525 break;
526 case SV_YUV411_FRAMES_AND_BLANKING_BUFFER:
527 break;
528 default:
Barry Warsawf630f6b1996-12-13 01:24:29 +0000529 PyErr_SetString(SvError, "illegal format specified");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000530 return NULL;
531 }
532
533 if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes)) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000534 res = sv_error();
535 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000536 }
537
Barry Warsawf630f6b1996-12-13 01:24:29 +0000538 if (!(videodata = PyString_FromStringAndSize(NULL, bytes)))
539 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000540
541 /* XXX -- need to do something about the bitvector */
Barry Warsawf630f6b1996-12-13 01:24:29 +0000542 {
543 char* str = PyString_AsString(videodata);
544 if (!str)
545 goto finally;
546
547 if (svCaptureBurst(self->ob_svideo, &info, str, bitvector)) {
548 res = sv_error();
549 goto finally;
550 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000551 }
552
553 if (bitvector) {
554 if (evenitem == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000555 if (!(evenitem = PyInt_FromLong(0)))
556 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000557 }
558 if (odditem == NULL) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000559 if (!(odditem = PyInt_FromLong(1)))
560 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000561 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000562 if (!(bitvecobj = PyTuple_New(2 * info.size)))
563 goto finally;
564
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000565 for (i = 0; i < 2 * info.size; i++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000566 int sts;
567
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000568 if (SV_GET_FIELD(bitvector, i) == SV_EVEN_FIELD) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000569 Py_INCREF(evenitem);
570 sts = PyTuple_SetItem(bitvecobj, i, evenitem);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000571 } else {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000572 Py_INCREF(odditem);
573 sts = PyTuple_SetItem(bitvecobj, i, odditem);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000574 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000575 if (sts < 0)
576 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000577 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000578 } else {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000579 bitvecobj = Py_None;
580 Py_INCREF(Py_None);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000581 }
582
Barry Warsawf630f6b1996-12-13 01:24:29 +0000583 res = Py_BuildValue("((iiiii)OO)", info.format,
584 info.width, info.height,
585 info.size, info.samplingrate,
586 videodata, bitvecobj);
587
588 finally:
589 if (bitvector)
590 free(bitvector);
591
592 Py_XDECREF(videodata);
593 Py_XDECREF(bitvecobj);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000594 return res;
595}
596
Barry Warsawf630f6b1996-12-13 01:24:29 +0000597static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000598sv_CaptureOneFrame(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000599{
600 svCaptureInfo info;
601 int format, width, height;
602 int bytes;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000603 PyObject *videodata = NULL;
604 PyObject *res = NULL;
Guido van Rossumc88c9cb1996-12-17 20:43:55 +0000605 char *str;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000606
Barry Warsawf630f6b1996-12-13 01:24:29 +0000607 if (!PyArg_Parse(args, "(iii)", &format, &width, &height))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000608 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000609
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000610 info.format = format;
611 info.width = width;
612 info.height = height;
613 info.size = 0;
614 info.samplingrate = 0;
615 if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes))
616 return sv_error();
Barry Warsawf630f6b1996-12-13 01:24:29 +0000617
618 if (!(videodata = PyString_FromStringAndSize(NULL, bytes)))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000619 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000620
Guido van Rossumc88c9cb1996-12-17 20:43:55 +0000621 str = PyString_AsString(videodata);
622 if (!str)
623 goto finally;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000624
625 if (svCaptureOneFrame(self->ob_svideo, format, &width, &height, str)) {
626 res = sv_error();
627 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000628 }
629
Barry Warsawf630f6b1996-12-13 01:24:29 +0000630 res = Py_BuildValue("(iiO)", width, height, videodata);
631
632 finally:
633 Py_XDECREF(videodata);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000634 return res;
635}
636
Barry Warsawf630f6b1996-12-13 01:24:29 +0000637static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000638sv_InitContinuousCapture(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000639{
640 svCaptureInfo info;
641
Barry Warsawf630f6b1996-12-13 01:24:29 +0000642 if (!PyArg_Parse(args, "(iiiii)", &info.format,
643 &info.width, &info.height,
644 &info.size, &info.samplingrate))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000645 return NULL;
646
647 if (svInitContinuousCapture(self->ob_svideo, &info))
648 return sv_error();
649
650 self->ob_info = info;
651
Barry Warsawf630f6b1996-12-13 01:24:29 +0000652 return Py_BuildValue("(iiiii)", info.format, info.width, info.height,
653 info.size, info.samplingrate);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000654}
655
Barry Warsawf630f6b1996-12-13 01:24:29 +0000656static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000657sv_LoadMap(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000658{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000659 PyObject *rgb;
660 PyObject *res = NULL;
661 rgb_tuple *mapp = NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000662 int maptype;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000663 int i, j; /* indices */
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000664
Barry Warsawf630f6b1996-12-13 01:24:29 +0000665 if (!PyArg_Parse(args, "(iO)", &maptype, &rgb))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000666 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000667
668 if (!PyList_Check(rgb) || PyList_Size(rgb) != 256) {
669 PyErr_BadArgument();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000670 return NULL;
671 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000672
673 if (!(mapp = PyMem_NEW(rgb_tuple, 256)))
674 return PyErr_NoMemory();
675
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000676 for (i = 0; i < 256; i++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000677 PyObject* v = PyList_GetItem(rgb, i);
678 if (!v)
679 goto finally;
680
681 if (!PyTuple_Check(v) || PyTuple_Size(v) != 3) {
682 PyErr_BadArgument();
683 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000684 }
685 for (j = 0; j < 3; j++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000686 PyObject* cell = PyTuple_GetItem(v, j);
687 if (!cell)
688 goto finally;
689
690 if (!PyInt_Check(cell)) {
691 PyErr_BadArgument();
692 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000693 }
694 switch (j) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000695 case 0: mapp[i].red = PyInt_AsLong(cell); break;
696 case 1: mapp[i].blue = PyInt_AsLong(cell); break;
697 case 2: mapp[i].green = PyInt_AsLong(cell); break;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000698 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000699 if (PyErr_Occurred())
700 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000701 }
702 }
703
704 if (svLoadMap(self->ob_svideo, maptype, mapp)) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000705 res = sv_error();
706 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000707 }
708
Barry Warsawf630f6b1996-12-13 01:24:29 +0000709 Py_INCREF(Py_None);
710 res = Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000711
Barry Warsawf630f6b1996-12-13 01:24:29 +0000712 finally:
713 PyMem_DEL(mapp);
714 return res;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000715}
716
Barry Warsawf630f6b1996-12-13 01:24:29 +0000717static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000718sv_CloseVideo(svobject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000719{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000720 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000721 return NULL;
722
723 if (svCloseVideo(self->ob_svideo))
724 return sv_error();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000725
Barry Warsawf630f6b1996-12-13 01:24:29 +0000726 self->ob_svideo = NULL;
727 Py_INCREF(Py_None);
728 return Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000729}
730
Barry Warsawf630f6b1996-12-13 01:24:29 +0000731static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000732doParams(svobject *self, PyObject *args,
733 int (*func)(SV_nodeP, long *, int), int modified)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000734{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000735 PyObject *list;
736 PyObject *res = NULL;
737 long *PVbuffer = NULL;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000738 long length;
739 int i;
740
Barry Warsawf630f6b1996-12-13 01:24:29 +0000741 if (!PyArg_Parse(args, "O", &list))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000742 return NULL;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000743
744 if (!PyList_Check(list)) {
745 PyErr_BadArgument();
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000746 return NULL;
747 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000748
749 if ((length = PyList_Size(list)) < 0)
750 return NULL;
751
752 PVbuffer = PyMem_NEW(long, length);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000753 if (PVbuffer == NULL)
Barry Warsawf630f6b1996-12-13 01:24:29 +0000754 return PyErr_NoMemory();
755
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000756 for (i = 0; i < length; i++) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000757 PyObject *v = PyList_GetItem(list, i);
758 if (!v)
759 goto finally;
760
761 if (!PyInt_Check(v)) {
762 PyErr_BadArgument();
763 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000764 }
Barry Warsawf630f6b1996-12-13 01:24:29 +0000765 PVbuffer[i] = PyInt_AsLong(v);
766 /* can't just test the return value, because what if the
767 value was -1?!
768 */
769 if (PVbuffer[i] == -1 && PyErr_Occurred())
770 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000771 }
772
773 if ((*func)(self->ob_svideo, PVbuffer, length)) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000774 res = sv_error();
775 goto finally;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000776 }
777
778 if (modified) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000779 for (i = 0; i < length; i++) {
780 PyObject* v = PyInt_FromLong(PVbuffer[i]);
781 if (!v || PyList_SetItem(list, i, v) < 0)
782 goto finally;
783 }
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000784 }
785
Barry Warsawf630f6b1996-12-13 01:24:29 +0000786 Py_INCREF(Py_None);
787 res = Py_None;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000788
Barry Warsawf630f6b1996-12-13 01:24:29 +0000789 finally:
790 PyMem_DEL(PVbuffer);
791 return res;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000792}
793
Barry Warsawf630f6b1996-12-13 01:24:29 +0000794static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000795sv_GetParam(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000796{
797 return doParams(self, args, svGetParam, 1);
798}
799
Barry Warsawf630f6b1996-12-13 01:24:29 +0000800static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000801sv_GetParamRange(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000802{
803 return doParams(self, args, svGetParamRange, 1);
804}
805
Barry Warsawf630f6b1996-12-13 01:24:29 +0000806static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000807sv_SetParam(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000808{
809 return doParams(self, args, svSetParam, 0);
810}
811
Barry Warsawf630f6b1996-12-13 01:24:29 +0000812static PyMethodDef svideo_methods[] = {
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000813 {"BindGLWindow", (PyCFunction)sv_BindGLWindow, METH_OLDARGS},
814 {"EndContinuousCapture",(PyCFunction)sv_EndContinuousCapture, METH_OLDARGS},
815 {"IsVideoDisplayed", (PyCFunction)sv_IsVideoDisplayed, METH_OLDARGS},
816 {"OutputOffset", (PyCFunction)sv_OutputOffset, METH_OLDARGS},
817 {"PutFrame", (PyCFunction)sv_PutFrame, METH_OLDARGS},
818 {"QuerySize", (PyCFunction)sv_QuerySize, METH_OLDARGS},
819 {"SetSize", (PyCFunction)sv_SetSize, METH_OLDARGS},
820 {"SetStdDefaults", (PyCFunction)sv_SetStdDefaults, METH_OLDARGS},
821 {"UseExclusive", (PyCFunction)sv_UseExclusive, METH_OLDARGS},
822 {"WindowOffset", (PyCFunction)sv_WindowOffset, METH_OLDARGS},
823 {"InitContinuousCapture",(PyCFunction)sv_InitContinuousCapture, METH_OLDARGS},
824 {"CaptureBurst", (PyCFunction)sv_CaptureBurst, METH_OLDARGS},
825 {"CaptureOneFrame", (PyCFunction)sv_CaptureOneFrame, METH_OLDARGS},
826 {"GetCaptureData", (PyCFunction)sv_GetCaptureData, METH_OLDARGS},
827 {"CloseVideo", (PyCFunction)sv_CloseVideo, METH_OLDARGS},
828 {"LoadMap", (PyCFunction)sv_LoadMap, METH_OLDARGS},
829 {"GetParam", (PyCFunction)sv_GetParam, METH_OLDARGS},
830 {"GetParamRange", (PyCFunction)sv_GetParamRange, METH_OLDARGS},
831 {"SetParam", (PyCFunction)sv_SetParam, METH_OLDARGS},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000832 {NULL, NULL} /* sentinel */
833};
834
Barry Warsawf630f6b1996-12-13 01:24:29 +0000835static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000836sv_conversion(PyObject *self, PyObject *args, void (*function)(),
837 int inputfactor, float factor)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000838{
839 int invert, width, height, inputlength;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000840 char *input, *str;
841 PyObject *output;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000842
Barry Warsawf630f6b1996-12-13 01:24:29 +0000843 if (!PyArg_Parse(args, "(is#ii)", &invert,
844 &input, &inputlength, &width, &height))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000845 return NULL;
846
847 if (width * height * inputfactor > inputlength) {
Barry Warsawf630f6b1996-12-13 01:24:29 +0000848 PyErr_SetString(SvError, "input buffer not long enough");
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000849 return NULL;
850 }
851
Barry Warsawf630f6b1996-12-13 01:24:29 +0000852 if (!(output = PyString_FromStringAndSize(NULL,
853 (int)(width * height * factor))))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000854 return NULL;
855
Barry Warsawf630f6b1996-12-13 01:24:29 +0000856 str = PyString_AsString(output);
857 if (!str) {
858 Py_DECREF(output);
859 return NULL;
860 }
861 (*function)(invert, input, str, width, height);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000862
863 return output;
864}
865
Barry Warsawf630f6b1996-12-13 01:24:29 +0000866static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000867sv_InterleaveFields(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000868{
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000869 return sv_conversion(self, args, svInterleaveFields, 1, 1.0);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000870}
871
Barry Warsawf630f6b1996-12-13 01:24:29 +0000872static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000873sv_RGB8toRGB32(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000874{
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000875 return sv_conversion(self, args, svRGB8toRGB32, 1, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000876}
877
Barry Warsawf630f6b1996-12-13 01:24:29 +0000878static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000879sv_YUVtoRGB(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000880{
Sjoerd Mullender6b517fd1993-03-16 12:25:30 +0000881 return sv_conversion(self, args, svYUVtoRGB, 2, (float) sizeof(long));
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000882}
883
884static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000885svideo_dealloc(svobject *self)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000886{
887 if (self->ob_svideo != NULL)
888 (void) svCloseVideo(self->ob_svideo);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000889 PyObject_Del(self);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000890}
891
Barry Warsawf630f6b1996-12-13 01:24:29 +0000892static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000893svideo_getattr(svobject *self, char *name)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000894{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000895 return Py_FindMethod(svideo_methods, (PyObject *)self, name);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000896}
897
Barry Warsawf630f6b1996-12-13 01:24:29 +0000898PyTypeObject Svtype = {
899 PyObject_HEAD_INIT(&PyType_Type)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000900 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000901 "sv.sv", /*tp_name*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000902 sizeof(svobject), /*tp_size*/
903 0, /*tp_itemsize*/
904 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000905 (destructor)svideo_dealloc, /*tp_dealloc*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000906 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000907 (getattrfunc)svideo_getattr, /*tp_getattr*/
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000908 0, /*tp_setattr*/
909 0, /*tp_compare*/
910 0, /*tp_repr*/
911};
912
Barry Warsawf630f6b1996-12-13 01:24:29 +0000913static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000914newsvobject(SV_nodeP svp)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000915{
916 svobject *p;
917
Guido van Rossumb18618d2000-05-03 23:44:39 +0000918 p = PyObject_New(svobject, &Svtype);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000919 if (p == NULL)
920 return NULL;
921 p->ob_svideo = svp;
922 p->ob_info.format = 0;
923 p->ob_info.size = 0;
924 p->ob_info.width = 0;
925 p->ob_info.height = 0;
926 p->ob_info.samplingrate = 0;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000927 return (PyObject *) p;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000928}
929
Barry Warsawf630f6b1996-12-13 01:24:29 +0000930static PyObject *
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000931sv_OpenVideo(PyObject *self, PyObject *args)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000932{
933 SV_nodeP svp;
934
Barry Warsawf630f6b1996-12-13 01:24:29 +0000935 if (!PyArg_Parse(args, ""))
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000936 return NULL;
937
938 svp = svOpenVideo();
939 if (svp == NULL)
940 return sv_error();
941
942 return newsvobject(svp);
943}
944
Barry Warsawf630f6b1996-12-13 01:24:29 +0000945static PyMethodDef sv_methods[] = {
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000946 {"InterleaveFields", (PyCFunction)sv_InterleaveFields, METH_OLDARGS},
947 {"RGB8toRGB32", (PyCFunction)sv_RGB8toRGB32, METH_OLDARGS},
948 {"YUVtoRGB", (PyCFunction)sv_YUVtoRGB, METH_OLDARGS},
949 {"OpenVideo", (PyCFunction)sv_OpenVideo, METH_OLDARGS},
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000950 {NULL, NULL} /* Sentinel */
951};
952
953void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000954initsv(void)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000955{
Barry Warsawf630f6b1996-12-13 01:24:29 +0000956 PyObject *m, *d;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000957
Barry Warsawf630f6b1996-12-13 01:24:29 +0000958 m = Py_InitModule("sv", sv_methods);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000959 if (m == NULL)
960 return;
Barry Warsawf630f6b1996-12-13 01:24:29 +0000961 d = PyModule_GetDict(m);
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000962
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000963 SvError = PyErr_NewException("sv.error", NULL, NULL);
Barry Warsawf630f6b1996-12-13 01:24:29 +0000964 if (SvError == NULL || PyDict_SetItemString(d, "error", SvError) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000965 return;
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000966}