Kaydet (Commit) f630f6b9 authored tarafından Barry Warsaw's avatar Barry Warsaw

Renamed, and scrutinized for missed potential error conditions.

Alas, I don't have an Indigo, so I could not even compile this.
üst e3c0170b
...@@ -33,57 +33,63 @@ PERFORMANCE OF THIS SOFTWARE. ...@@ -33,57 +33,63 @@ PERFORMANCE OF THIS SOFTWARE.
#include <sys/time.h> #include <sys/time.h>
#include <svideo.h> #include <svideo.h>
#include "allobjects.h" #include "Python.h"
#include "import.h"
#include "modsupport.h"
#include "compile.h" #include "compile.h"
#include "ceval.h"
#include "yuv.h" /* for YUV conversion functions */ #include "yuv.h" /* for YUV conversion functions */
typedef struct { typedef struct {
OB_HEAD PyObject_HEAD
SV_nodeP ob_svideo; SV_nodeP ob_svideo;
svCaptureInfo ob_info; svCaptureInfo ob_info;
} svobject; } svobject;
typedef struct { typedef struct {
OB_HEAD PyObject_HEAD
void *ob_capture; void *ob_capture;
int ob_mustunlock; int ob_mustunlock;
svCaptureInfo ob_info; svCaptureInfo ob_info;
svobject *ob_svideo; svobject *ob_svideo;
} captureobject; } captureobject;
static object *SvError; /* exception sv.error */ static PyObject *SvError; /* exception sv.error */
static object *newcaptureobject PROTO((svobject *, void *, int)); static PyObject *newcaptureobject Py_PROTO((svobject *, void *, int));
/* Set a SV-specific error from svideo_errno and return NULL */ /* Set a SV-specific error from svideo_errno and return NULL */
static object * static PyObject *
sv_error() sv_error()
{ {
err_setstr(SvError, svStrerror(svideo_errno)); PyErr_SetString(SvError, svStrerror(svideo_errno));
return NULL; return NULL;
} }
static object * static PyObject *
svc_conversion(self, args, function, factor) svc_conversion(self, args, function, factor)
captureobject *self; captureobject *self;
object *args; PyObject *args;
void (*function)(); void (*function)();
float factor; float factor;
{ {
object *output; PyObject *output;
int invert; int invert;
char* outstr;
if (!getargs(args, "i", &invert)) if (!PyArg_Parse(args, "i", &invert))
return NULL; return NULL;
output = newsizedstringobject(NULL, (int) (self->ob_info.width * self->ob_info.height * factor)); if (!(output = PyString_FromStringAndSize(
if (output == NULL) NULL,
(int)(self->ob_info.width * self->ob_info.height * factor))))
{
return NULL; return NULL;
}
if (!(outstr = PyString_AsString(output))) {
Py_DECREF(output);
return NULL;
}
(*function)((boolean) invert, self->ob_capture, getstringvalue(output), (*function)((boolean)invert, self->ob_capture,
outstr,
self->ob_info.width, self->ob_info.height); self->ob_info.width, self->ob_info.height);
return output; return output;
...@@ -93,114 +99,126 @@ svc_conversion(self, args, function, factor) ...@@ -93,114 +99,126 @@ svc_conversion(self, args, function, factor)
* 3 functions to convert from Starter Video YUV 4:1:1 format to * 3 functions to convert from Starter Video YUV 4:1:1 format to
* Compression Library 4:2:2 Duplicate Chroma format. * Compression Library 4:2:2 Duplicate Chroma format.
*/ */
static object * static PyObject *
svc_YUVtoYUV422DC(self, args) svc_YUVtoYUV422DC(self, args)
captureobject *self; captureobject *self;
object *args; PyObject *args;
{ {
if (self->ob_info.format != SV_YUV411_FRAMES) { if (self->ob_info.format != SV_YUV411_FRAMES) {
err_setstr(SvError, "data has bad format"); PyErr_SetString(SvError, "data has bad format");
return NULL; return NULL;
} }
return svc_conversion(self, args, yuv_sv411_to_cl422dc, 2.0); return svc_conversion(self, args, yuv_sv411_to_cl422dc, 2.0);
} }
static object * static PyObject *
svc_YUVtoYUV422DC_quarter(self, args) svc_YUVtoYUV422DC_quarter(self, args)
captureobject *self; captureobject *self;
object *args; PyObject *args;
{ {
if (self->ob_info.format != SV_YUV411_FRAMES) { if (self->ob_info.format != SV_YUV411_FRAMES) {
err_setstr(SvError, "data has bad format"); PyErr_SetString(SvError, "data has bad format");
return NULL; return NULL;
} }
return svc_conversion(self, args, yuv_sv411_to_cl422dc_quartersize, 0.5); return svc_conversion(self, args,
yuv_sv411_to_cl422dc_quartersize, 0.5);
} }
static object * static PyObject *
svc_YUVtoYUV422DC_sixteenth(self, args) svc_YUVtoYUV422DC_sixteenth(self, args)
captureobject *self; captureobject *self;
object *args; PyObject *args;
{ {
if (self->ob_info.format != SV_YUV411_FRAMES) { if (self->ob_info.format != SV_YUV411_FRAMES) {
err_setstr(SvError, "data has bad format"); PyErr_SetString(SvError, "data has bad format");
return NULL; return NULL;
} }
return svc_conversion(self, args, yuv_sv411_to_cl422dc_sixteenthsize, 0.125); return svc_conversion(self, args,
yuv_sv411_to_cl422dc_sixteenthsize, 0.125);
} }
static object * static PyObject *
svc_YUVtoRGB(self, args) svc_YUVtoRGB(self, args)
captureobject *self; captureobject *self;
object *args; PyObject *args;
{ {
switch (self->ob_info.format) { switch (self->ob_info.format) {
case SV_YUV411_FRAMES: case SV_YUV411_FRAMES:
case SV_YUV411_FRAMES_AND_BLANKING_BUFFER: case SV_YUV411_FRAMES_AND_BLANKING_BUFFER:
break; break;
default: default:
err_setstr(SvError, "data had bad format"); PyErr_SetString(SvError, "data had bad format");
return NULL; return NULL;
} }
return svc_conversion(self, args, svYUVtoRGB, (float) sizeof(long)); return svc_conversion(self, args, svYUVtoRGB, (float) sizeof(long));
} }
static object * static PyObject *
svc_RGB8toRGB32(self, args) svc_RGB8toRGB32(self, args)
captureobject *self; captureobject *self;
object *args; PyObject *args;
{ {
if (self->ob_info.format != SV_RGB8_FRAMES) { if (self->ob_info.format != SV_RGB8_FRAMES) {
err_setstr(SvError, "data has bad format"); PyErr_SetString(SvError, "data has bad format");
return NULL; return NULL;
} }
return svc_conversion(self, args, svRGB8toRGB32, (float) sizeof(long)); return svc_conversion(self, args, svRGB8toRGB32, (float) sizeof(long));
} }
static object * static PyObject *
svc_InterleaveFields(self, args) svc_InterleaveFields(self, args)
captureobject *self; captureobject *self;
object *args; PyObject *args;
{ {
if (self->ob_info.format != SV_RGB8_FRAMES) { if (self->ob_info.format != SV_RGB8_FRAMES) {
err_setstr(SvError, "data has bad format"); PyErr_SetString(SvError, "data has bad format");
return NULL; return NULL;
} }
return svc_conversion(self, args, svInterleaveFields, 1.0); return svc_conversion(self, args, svInterleaveFields, 1.0);
} }
static object * static PyObject *
svc_GetFields(self, args) svc_GetFields(self, args)
captureobject *self; captureobject *self;
object *args; PyObject *args;
{ {
object *f1, *f2, *ret; PyObject *f1 = NULL;
PyObject *f2 = NULL;
PyObject *ret = NULL;
int fieldsize; int fieldsize;
char* obcapture;
if (self->ob_info.format != SV_RGB8_FRAMES) { if (self->ob_info.format != SV_RGB8_FRAMES) {
err_setstr(SvError, "data has bad format"); PyErr_SetString(SvError, "data has bad format");
return NULL; return NULL;
} }
fieldsize = self->ob_info.width * self->ob_info.height / 2; fieldsize = self->ob_info.width * self->ob_info.height / 2;
f1 = newsizedstringobject((char *) self->ob_capture, fieldsize); obcapture = (char*)self->ob_capture;
f2 = newsizedstringobject((char *) self->ob_capture + fieldsize, fieldsize);
ret = mkvalue("(OO)", f1, f2); if (!(f1 = PyString_FromStringAndSize(obcapture, fieldsize)))
XDECREF(f1); goto finally;
XDECREF(f2); if (!(f2 = PyString_FromStringAndSize(obcapture + fieldsize,
fieldsize)))
goto finally;
ret = Py_BuildValue("(OO)", f1, f2);
finally:
Py_XDECREF(f1);
Py_XDECREF(f2);
return ret; return ret;
} }
static object * static PyObject *
svc_UnlockCaptureData(self, args) svc_UnlockCaptureData(self, args)
captureobject *self; captureobject *self;
object *args; PyObject *args;
{ {
if (!getnoarg(args)) if (!PyArg_Parse(args, ""))
return NULL; return NULL;
if (!self->ob_mustunlock) { if (!self->ob_mustunlock) {
err_setstr(SvError, "buffer should not be unlocked"); PyErr_SetString(SvError, "buffer should not be unlocked");
return NULL; return NULL;
} }
...@@ -209,93 +227,99 @@ svc_UnlockCaptureData(self, args) ...@@ -209,93 +227,99 @@ svc_UnlockCaptureData(self, args)
self->ob_mustunlock = 0; self->ob_mustunlock = 0;
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
#ifdef USE_GL #ifdef USE_GL
#include <gl.h> #include <gl.h>
static object * static PyObject *
svc_lrectwrite(self, args) svc_lrectwrite(self, args)
captureobject *self; captureobject *self;
object *args; PyObject *args;
{ {
Screencoord x1, x2, y1, y2; Screencoord x1, x2, y1, y2;
if (!getargs(args, "(hhhh)", &x1, &x2, &y1, &y2)) if (!PyArg_Parse(args, "(hhhh)", &x1, &x2, &y1, &y2))
return NULL; return NULL;
lrectwrite(x1, x2, y1, y2, (unsigned long *) self->ob_capture); lrectwrite(x1, x2, y1, y2, (unsigned long *) self->ob_capture);
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
#endif #endif
static object * static PyObject *
svc_writefile(self, args) svc_writefile(self, args)
captureobject *self; captureobject *self;
object *args; PyObject *args;
{ {
object *file; PyObject *file;
int size; int size;
FILE* fp;
if (!getargs(args, "O", &file)) if (!PyArg_Parse(args, "O", &file))
return NULL; return NULL;
if (!is_fileobject(file)) { if (!PyFile_Check(file)) {
err_setstr(SvError, "not a file object"); PyErr_SetString(SvError, "not a file object");
return NULL; return NULL;
} }
if (!(fp = PyFile_AsFile(file)))
return NULL;
size = self->ob_info.width * self->ob_info.height; size = self->ob_info.width * self->ob_info.height;
if (fwrite(self->ob_capture, sizeof(long), size, getfilefile(file)) != size) { if (fwrite(self->ob_capture, sizeof(long), size, fp) != size) {
err_setstr(SvError, "writing failed"); PyErr_SetString(SvError, "writing failed");
return NULL; return NULL;
} }
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
static object * static PyObject *
svc_FindVisibleRegion(self, args) svc_FindVisibleRegion(self, args)
captureobject *self; captureobject *self;
object *args; PyObject *args;
{ {
void *visible; void *visible;
int width; int width;
if (!getnoarg(args)) if (!PyArg_Parse(args, ""))
return NULL; return NULL;
if (svFindVisibleRegion(self->ob_svideo->ob_svideo, self->ob_capture, &visible, self->ob_info.width)) if (svFindVisibleRegion(self->ob_svideo->ob_svideo,
self->ob_capture, &visible,
self->ob_info.width))
return sv_error(); return sv_error();
if (visible == NULL) { if (visible == NULL) {
err_setstr(SvError, "data in wrong format"); PyErr_SetString(SvError, "data in wrong format");
return NULL; return NULL;
} }
return newcaptureobject(self->ob_svideo, visible, 0); return newcaptureobject(self->ob_svideo, visible, 0);
} }
static struct methodlist capture_methods[] = { static PyMethodDef capture_methods[] = {
{"YUVtoRGB", (method)svc_YUVtoRGB}, {"YUVtoRGB", (PyCFunction)svc_YUVtoRGB},
{"RGB8toRGB32", (method)svc_RGB8toRGB32}, {"RGB8toRGB32", (PyCFunction)svc_RGB8toRGB32},
{"InterleaveFields", (method)svc_InterleaveFields}, {"InterleaveFields", (PyCFunction)svc_InterleaveFields},
{"UnlockCaptureData", (method)svc_UnlockCaptureData}, {"UnlockCaptureData", (PyCFunction)svc_UnlockCaptureData},
{"FindVisibleRegion", (method)svc_FindVisibleRegion}, {"FindVisibleRegion", (PyCFunction)svc_FindVisibleRegion},
{"GetFields", (method)svc_GetFields}, {"GetFields", (PyCFunction)svc_GetFields},
{"YUVtoYUV422DC", (method)svc_YUVtoYUV422DC}, {"YUVtoYUV422DC", (PyCFunction)svc_YUVtoYUV422DC},
{"YUVtoYUV422DC_quarter",(method)svc_YUVtoYUV422DC_quarter}, {"YUVtoYUV422DC_quarter",(PyCFunction)svc_YUVtoYUV422DC_quarter},
{"YUVtoYUV422DC_sixteenth",(method)svc_YUVtoYUV422DC_sixteenth}, {"YUVtoYUV422DC_sixteenth",(PyCFunction)svc_YUVtoYUV422DC_sixteenth},
#ifdef USE_GL #ifdef USE_GL
{"lrectwrite", (method)svc_lrectwrite}, {"lrectwrite", (PyCFunction)svc_lrectwrite},
#endif #endif
{"writefile", (method)svc_writefile}, {"writefile", (PyCFunction)svc_writefile},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
...@@ -305,24 +329,25 @@ capture_dealloc(self) ...@@ -305,24 +329,25 @@ capture_dealloc(self)
{ {
if (self->ob_capture != NULL) { if (self->ob_capture != NULL) {
if (self->ob_mustunlock) if (self->ob_mustunlock)
(void) svUnlockCaptureData(self->ob_svideo->ob_svideo, self->ob_capture); (void)svUnlockCaptureData(self->ob_svideo->ob_svideo,
self->ob_capture);
self->ob_capture = NULL; self->ob_capture = NULL;
DECREF(self->ob_svideo); Py_DECREF(self->ob_svideo);
self->ob_svideo = NULL; self->ob_svideo = NULL;
} }
DEL(self); PyMem_DEL(self);
} }
static object * static PyObject *
capture_getattr(self, name) capture_getattr(self, name)
svobject *self; svobject *self;
char *name; char *name;
{ {
return findmethod(capture_methods, (object *)self, name); return Py_FindMethod(capture_methods, (PyObject *)self, name);
} }
typeobject Capturetype = { PyTypeObject Capturetype = {
OB_HEAD_INIT(&Typetype) PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/ 0, /*ob_size*/
"capture", /*tp_name*/ "capture", /*tp_name*/
sizeof(captureobject), /*tp_size*/ sizeof(captureobject), /*tp_size*/
...@@ -336,7 +361,7 @@ typeobject Capturetype = { ...@@ -336,7 +361,7 @@ typeobject Capturetype = {
0, /*tp_repr*/ 0, /*tp_repr*/
}; };
static object * static PyObject *
newcaptureobject(self, ptr, mustunlock) newcaptureobject(self, ptr, mustunlock)
svobject *self; svobject *self;
void *ptr; void *ptr;
...@@ -344,233 +369,236 @@ newcaptureobject(self, ptr, mustunlock) ...@@ -344,233 +369,236 @@ newcaptureobject(self, ptr, mustunlock)
{ {
captureobject *p; captureobject *p;
p = NEWOBJ(captureobject, &Capturetype); p = PyObject_NEW(captureobject, &Capturetype);
if (p == NULL) if (p == NULL)
return NULL; return NULL;
p->ob_svideo = self; p->ob_svideo = self;
INCREF(self); Py_INCREF(self);
p->ob_capture = ptr; p->ob_capture = ptr;
p->ob_mustunlock = mustunlock; p->ob_mustunlock = mustunlock;
p->ob_info = self->ob_info; p->ob_info = self->ob_info;
return (object *) p; return (PyObject *) p;
} }
static object * static PyObject *
sv_GetCaptureData(self, args) sv_GetCaptureData(self, args)
svobject *self; svobject *self;
object *args; PyObject *args;
{ {
void *ptr; void *ptr;
long fieldID; long fieldID;
object *res, *c; PyObject *res, *c;
if (!getnoarg(args)) if (!PyArg_Parse(args, ""))
return NULL; return NULL;
if (svGetCaptureData(self->ob_svideo, &ptr, &fieldID)) if (svGetCaptureData(self->ob_svideo, &ptr, &fieldID))
return sv_error(); return sv_error();
if (ptr == NULL) { if (ptr == NULL) {
err_setstr(SvError, "no data available"); PyErr_SetString(SvError, "no data available");
return NULL; return NULL;
} }
c = newcaptureobject(self, ptr, 1); c = newcaptureobject(self, ptr, 1);
if (c == NULL) if (c == NULL)
return NULL; return NULL;
res = mkvalue("(Oi)", c, fieldID); res = Py_BuildValue("(Oi)", c, fieldID);
DECREF(c); Py_DECREF(c);
return res; return res;
} }
static object * static PyObject *
sv_BindGLWindow(self, args) sv_BindGLWindow(self, args)
svobject *self; svobject *self;
object *args; PyObject *args;
{ {
long wid; long wid;
int mode; int mode;
if (!getargs(args, "(ii)", &wid, &mode)) if (!PyArg_Parse(args, "(ii)", &wid, &mode))
return NULL; return NULL;
if (svBindGLWindow(self->ob_svideo, wid, mode)) if (svBindGLWindow(self->ob_svideo, wid, mode))
return sv_error(); return sv_error();
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
static object * static PyObject *
sv_EndContinuousCapture(self, args) sv_EndContinuousCapture(self, args)
svobject *self; svobject *self;
object *args; PyObject *args;
{ {
if (!getnoarg(args)) if (!PyArg_Parse(args, ""))
return NULL; return NULL;
if (svEndContinuousCapture(self->ob_svideo)) if (svEndContinuousCapture(self->ob_svideo))
return sv_error(); return sv_error();
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
static object * static PyObject *
sv_IsVideoDisplayed(self, args) sv_IsVideoDisplayed(self, args)
svobject *self; svobject *self;
object *args; PyObject *args;
{ {
int v; int v;
if (!getnoarg(args)) if (!PyArg_Parse(args, ""))
return NULL; return NULL;
v = svIsVideoDisplayed(self->ob_svideo); v = svIsVideoDisplayed(self->ob_svideo);
if (v == -1) if (v == -1)
return sv_error(); return sv_error();
return newintobject((long) v); return PyInt_FromLong((long) v);
} }
static object * static PyObject *
sv_OutputOffset(self, args) sv_OutputOffset(self, args)
svobject *self; svobject *self;
object *args; PyObject *args;
{ {
int x_offset; int x_offset;
int y_offset; int y_offset;
if (!getargs(args, "(ii)", &x_offset, &y_offset)) if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset))
return NULL; return NULL;
if (svOutputOffset(self->ob_svideo, x_offset, y_offset)) if (svOutputOffset(self->ob_svideo, x_offset, y_offset))
return sv_error(); return sv_error();
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
static object * static PyObject *
sv_PutFrame(self, args) sv_PutFrame(self, args)
svobject *self; svobject *self;
object *args; PyObject *args;
{ {
char *buffer; char *buffer;
if (!getargs(args, "s", &buffer)) if (!PyArg_Parse(args, "s", &buffer))
return NULL; return NULL;
if (svPutFrame(self->ob_svideo, buffer)) if (svPutFrame(self->ob_svideo, buffer))
return sv_error(); return sv_error();
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
static object * static PyObject *
sv_QuerySize(self, args) sv_QuerySize(self, args)
svobject *self; svobject *self;
object *args; PyObject *args;
{ {
int w; int w;
int h; int h;
int rw; int rw;
int rh; int rh;
if (!getargs(args, "(ii)", &w, &h)) if (!PyArg_Parse(args, "(ii)", &w, &h))
return NULL; return NULL;
if (svQuerySize(self->ob_svideo, w, h, &rw, &rh)) if (svQuerySize(self->ob_svideo, w, h, &rw, &rh))
return sv_error(); return sv_error();
return mkvalue("(ii)", (long) rw, (long) rh); return Py_BuildValue("(ii)", (long) rw, (long) rh);
} }
static object * static PyObject *
sv_SetSize(self, args) sv_SetSize(self, args)
svobject *self; svobject *self;
object *args; PyObject *args;
{ {
int w; int w;
int h; int h;
if (!getargs(args, "(ii)", &w, &h)) if (!PyArg_Parse(args, "(ii)", &w, &h))
return NULL; return NULL;
if (svSetSize(self->ob_svideo, w, h)) if (svSetSize(self->ob_svideo, w, h))
return sv_error(); return sv_error();
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
static object * static PyObject *
sv_SetStdDefaults(self, args) sv_SetStdDefaults(self, args)
svobject *self; svobject *self;
object *args; PyObject *args;
{ {
if (!getnoarg(args)) if (!PyArg_Parse(args, ""))
return NULL; return NULL;
if (svSetStdDefaults(self->ob_svideo)) if (svSetStdDefaults(self->ob_svideo))
return sv_error(); return sv_error();
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
static object * static PyObject *
sv_UseExclusive(self, args) sv_UseExclusive(self, args)
svobject *self; svobject *self;
object *args; PyObject *args;
{ {
boolean onoff; boolean onoff;
int mode; int mode;
if (!getargs(args, "(ii)", &onoff, &mode)) if (!PyArg_Parse(args, "(ii)", &onoff, &mode))
return NULL; return NULL;
if (svUseExclusive(self->ob_svideo, onoff, mode)) if (svUseExclusive(self->ob_svideo, onoff, mode))
return sv_error(); return sv_error();
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
static object * static PyObject *
sv_WindowOffset(self, args) sv_WindowOffset(self, args)
svobject *self; svobject *self;
object *args; PyObject *args;
{ {
int x_offset; int x_offset;
int y_offset; int y_offset;
if (!getargs(args, "(ii)", &x_offset, &y_offset)) if (!PyArg_Parse(args, "(ii)", &x_offset, &y_offset))
return NULL; return NULL;
if (svWindowOffset(self->ob_svideo, x_offset, y_offset)) if (svWindowOffset(self->ob_svideo, x_offset, y_offset))
return sv_error(); return sv_error();
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
static object * static PyObject *
sv_CaptureBurst(self, args) sv_CaptureBurst(self, args)
svobject *self; svobject *self;
object *args; PyObject *args;
{ {
int bytes, i; int bytes, i;
svCaptureInfo info; svCaptureInfo info;
void *bitvector = NULL; void *bitvector = NULL;
object *videodata, *bitvecobj, *res; PyObject *videodata = NULL;
static object *evenitem, *odditem; PyObject *bitvecobj = NULL;
PyObject* *res = NULL;
if (!getargs(args, "(iiiii)", &info.format, &info.width, &info.height, static PyObject *evenitem, *odditem;
&info.size, &info.samplingrate))
if (!PyArg_Parse(args, "(iiiii)", &info.format,
&info.width, &info.height,
&info.size, &info.samplingrate))
return NULL; return NULL;
switch (info.format) { switch (info.format) {
...@@ -580,89 +608,88 @@ sv_CaptureBurst(self, args) ...@@ -580,89 +608,88 @@ sv_CaptureBurst(self, args)
case SV_YUV411_FRAMES_AND_BLANKING_BUFFER: case SV_YUV411_FRAMES_AND_BLANKING_BUFFER:
break; break;
default: default:
err_setstr(SvError, "illegal format specified"); PyErr_SetString(SvError, "illegal format specified");
return NULL; return NULL;
} }
if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes)) { if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes)) {
if (bitvector) res = sv_error();
free(bitvector); goto finally;
return sv_error();
} }
videodata = newsizedstringobject(NULL, bytes); if (!(videodata = PyString_FromStringAndSize(NULL, bytes)))
if (videodata == NULL) { goto finally;
if (bitvector)
free(bitvector);
return NULL;
}
/* XXX -- need to do something about the bitvector */ /* XXX -- need to do something about the bitvector */
if (svCaptureBurst(self->ob_svideo, &info, getstringvalue(videodata), {
bitvector)) { char* str = PyString_AsString(videodata);
if (bitvector) if (!str)
free(bitvector); goto finally;
DECREF(videodata);
return sv_error(); if (svCaptureBurst(self->ob_svideo, &info, str, bitvector)) {
res = sv_error();
goto finally;
}
} }
if (bitvector) { if (bitvector) {
if (evenitem == NULL) { if (evenitem == NULL) {
evenitem = newintobject(0); if (!(evenitem = PyInt_FromLong(0)))
if (evenitem == NULL) { goto finally;
free(bitvector);
DECREF(videodata);
return NULL;
}
} }
if (odditem == NULL) { if (odditem == NULL) {
odditem = newintobject(1); if (!(odditem = PyInt_FromLong(1)))
if (odditem == NULL) { goto finally;
free(bitvector);
DECREF(videodata);
return NULL;
}
}
bitvecobj = newtupleobject(2 * info.size);
if (bitvecobj == NULL) {
free(bitvecobj);
DECREF(videodata);
return NULL;
} }
if (!(bitvecobj = PyTuple_New(2 * info.size)))
goto finally;
for (i = 0; i < 2 * info.size; i++) { for (i = 0; i < 2 * info.size; i++) {
int sts;
if (SV_GET_FIELD(bitvector, i) == SV_EVEN_FIELD) { if (SV_GET_FIELD(bitvector, i) == SV_EVEN_FIELD) {
INCREF(evenitem); Py_INCREF(evenitem);
settupleitem(bitvecobj, i, evenitem); sts = PyTuple_SetItem(bitvecobj, i, evenitem);
} else { } else {
INCREF(odditem); Py_INCREF(odditem);
settupleitem(bitvecobj, i, odditem); sts = PyTuple_SetItem(bitvecobj, i, odditem);
} }
if (sts < 0)
goto finally;
} }
free(bitvector);
} else { } else {
bitvecobj = None; bitvecobj = Py_None;
INCREF(None); Py_INCREF(Py_None);
} }
res = mkvalue("((iiiii)OO)", info.format, info.width, info.height, res = Py_BuildValue("((iiiii)OO)", info.format,
info.size, info.samplingrate, videodata, bitvecobj); info.width, info.height,
DECREF(videodata); info.size, info.samplingrate,
DECREF(bitvecobj); videodata, bitvecobj);
finally:
if (bitvector)
free(bitvector);
Py_XDECREF(videodata);
Py_XDECREF(bitvecobj);
return res; return res;
} }
static object * static PyObject *
sv_CaptureOneFrame(self, args) sv_CaptureOneFrame(self, args)
svobject *self; svobject *self;
object *args; PyObject *args;
{ {
svCaptureInfo info; svCaptureInfo info;
int format, width, height; int format, width, height;
int bytes; int bytes;
object *videodata, *res; PyObject *videodata = NULL;
PyObject *res = NULL;
if (!getargs(args, "(iii)", &format, &width, &height)) if (!PyArg_Parse(args, "(iii)", &format, &width, &height))
return NULL; return NULL;
info.format = format; info.format = format;
info.width = width; info.width = width;
info.height = height; info.height = height;
...@@ -670,29 +697,37 @@ sv_CaptureOneFrame(self, args) ...@@ -670,29 +697,37 @@ sv_CaptureOneFrame(self, args)
info.samplingrate = 0; info.samplingrate = 0;
if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes)) if (svQueryCaptureBufferSize(self->ob_svideo, &info, &bytes))
return sv_error(); return sv_error();
videodata = newsizedstringobject(NULL, bytes);
if (videodata == NULL) if (!(videodata = PyString_FromStringAndSize(NULL, bytes)))
return NULL; return NULL;
if (svCaptureOneFrame(self->ob_svideo, format, &width, &height,
getstringvalue(videodata))) { {
DECREF(videodata); char* str = PyString_AsString(videodata);
return sv_error(); if (!str)
goto finally;
if (svCaptureOneFrame(self->ob_svideo, format, &width, &height, str)) {
res = sv_error();
goto finally;
} }
res = mkvalue("(iiO)", width, height, videodata); res = Py_BuildValue("(iiO)", width, height, videodata);
DECREF(videodata);
finally:
Py_XDECREF(videodata);
return res; return res;
} }
static object * static PyObject *
sv_InitContinuousCapture(self, args) sv_InitContinuousCapture(self, args)
svobject *self; svobject *self;
object *args; PyObject *args;
{ {
svCaptureInfo info; svCaptureInfo info;
if (!getargs(args, "(iiiii)", &info.format, &info.width, &info.height, if (!PyArg_Parse(args, "(iiiii)", &info.format,
&info.size, &info.samplingrate)) &info.width, &info.height,
&info.size, &info.samplingrate))
return NULL; return NULL;
if (svInitContinuousCapture(self->ob_svideo, &info)) if (svInitContinuousCapture(self->ob_svideo, &info))
...@@ -700,215 +735,250 @@ sv_InitContinuousCapture(self, args) ...@@ -700,215 +735,250 @@ sv_InitContinuousCapture(self, args)
self->ob_info = info; self->ob_info = info;
return mkvalue("(iiiii)", info.format, info.width, info.height, return Py_BuildValue("(iiiii)", info.format, info.width, info.height,
info.size, info.samplingrate); info.size, info.samplingrate);
} }
static object * static PyObject *
sv_LoadMap(self, args) sv_LoadMap(self, args)
svobject *self; svobject *self;
object *args; PyObject *args;
{ {
object *rgb, *v, *cell; PyObject *rgb;
rgb_tuple *mapp; PyObject *res = NULL;
rgb_tuple *mapp = NULL;
int maptype; int maptype;
int i, j; /* indices */ int i, j; /* indices */
if (!getargs(args, "(iO)", &maptype, &rgb)) if (!PyArg_Parse(args, "(iO)", &maptype, &rgb))
return NULL; return NULL;
if (!is_listobject(rgb) || getlistsize(rgb) != 256) {
err_badarg(); if (!PyList_Check(rgb) || PyList_Size(rgb) != 256) {
PyErr_BadArgument();
return NULL; return NULL;
} }
mapp = NEW(rgb_tuple, 256);
if (mapp == NULL) if (!(mapp = PyMem_NEW(rgb_tuple, 256)))
return err_nomem(); return PyErr_NoMemory();
for (i = 0; i < 256; i++) { for (i = 0; i < 256; i++) {
v = getlistitem(rgb, i); PyObject* v = PyList_GetItem(rgb, i);
if (!is_tupleobject(v) || gettuplesize(v) != 3) { if (!v)
DEL(mapp); goto finally;
err_badarg();
return NULL; if (!PyTuple_Check(v) || PyTuple_Size(v) != 3) {
PyErr_BadArgument();
goto finally;
} }
for (j = 0; j < 3; j++) { for (j = 0; j < 3; j++) {
cell = gettupleitem(v, j); PyObject* cell = PyTuple_GetItem(v, j);
if (!is_intobject(cell)) { if (!cell)
DEL(mapp); goto finally;
err_badarg();
return NULL; if (!PyInt_Check(cell)) {
PyErr_BadArgument();
goto finally;
} }
switch (j) { switch (j) {
case 0: mapp[i].red = getintvalue(cell); break; case 0: mapp[i].red = PyInt_AsLong(cell); break;
case 1: mapp[i].blue = getintvalue(cell); break; case 1: mapp[i].blue = PyInt_AsLong(cell); break;
case 2: mapp[i].green = getintvalue(cell); break; case 2: mapp[i].green = PyInt_AsLong(cell); break;
} }
if (PyErr_Occurred())
goto finally;
} }
} }
if (svLoadMap(self->ob_svideo, maptype, mapp)) { if (svLoadMap(self->ob_svideo, maptype, mapp)) {
DEL(mapp); res = sv_error();
return sv_error(); goto finally;
} }
DEL(mapp); Py_INCREF(Py_None);
res = Py_None;
INCREF(None); finally:
return None; PyMem_DEL(mapp);
return res;
} }
static object * static PyObject *
sv_CloseVideo(self, args) sv_CloseVideo(self, args)
svobject *self; svobject *self;
object *args; PyObject *args;
{ {
if (!getnoarg(args)) if (!PyArg_Parse(args, ""))
return NULL; return NULL;
if (svCloseVideo(self->ob_svideo)) if (svCloseVideo(self->ob_svideo))
return sv_error(); return sv_error();
self->ob_svideo = NULL;
INCREF(None); self->ob_svideo = NULL;
return None; Py_INCREF(Py_None);
return Py_None;
} }
static object * static PyObject *
doParams(self, args, func, modified) doParams(self, args, func, modified)
svobject *self; svobject *self;
object *args; PyObject *args;
int (*func)(SV_nodeP, long *, int); int (*func)(SV_nodeP, long *, int);
int modified; int modified;
{ {
object *list, *v; PyObject *list;
long *PVbuffer; PyObject *res = NULL;
long *PVbuffer = NULL;
long length; long length;
int i; int i;
if (!getargs(args, "O", &list)) if (!PyArg_Parse(args, "O", &list))
return NULL; return NULL;
if (!is_listobject(list)) {
err_badarg(); if (!PyList_Check(list)) {
PyErr_BadArgument();
return NULL; return NULL;
} }
length = getlistsize(list);
PVbuffer = NEW(long, length); if ((length = PyList_Size(list)) < 0)
return NULL;
PVbuffer = PyMem_NEW(long, length);
if (PVbuffer == NULL) if (PVbuffer == NULL)
return err_nomem(); return PyErr_NoMemory();
for (i = 0; i < length; i++) { for (i = 0; i < length; i++) {
v = getlistitem(list, i); PyObject *v = PyList_GetItem(list, i);
if (!is_intobject(v)) { if (!v)
DEL(PVbuffer); goto finally;
err_badarg();
return NULL; if (!PyInt_Check(v)) {
PyErr_BadArgument();
goto finally;
} }
PVbuffer[i] = getintvalue(v); PVbuffer[i] = PyInt_AsLong(v);
/* can't just test the return value, because what if the
value was -1?!
*/
if (PVbuffer[i] == -1 && PyErr_Occurred())
goto finally;
} }
if ((*func)(self->ob_svideo, PVbuffer, length)) { if ((*func)(self->ob_svideo, PVbuffer, length)) {
DEL(PVbuffer); res = sv_error();
return sv_error(); goto finally;
} }
if (modified) { if (modified) {
for (i = 0; i < length; i++) for (i = 0; i < length; i++) {
setlistitem(list, i, newintobject(PVbuffer[i])); PyObject* v = PyInt_FromLong(PVbuffer[i]);
if (!v || PyList_SetItem(list, i, v) < 0)
goto finally;
}
} }
DEL(PVbuffer); Py_INCREF(Py_None);
res = Py_None;
INCREF(None); finally:
return None; PyMem_DEL(PVbuffer);
return res;
} }
static object * static PyObject *
sv_GetParam(self, args) sv_GetParam(self, args)
object *self, *args; PyObject *self, *args;
{ {
return doParams(self, args, svGetParam, 1); return doParams(self, args, svGetParam, 1);
} }
static object * static PyObject *
sv_GetParamRange(self, args) sv_GetParamRange(self, args)
object *self, *args; PyObject *self, *args;
{ {
return doParams(self, args, svGetParamRange, 1); return doParams(self, args, svGetParamRange, 1);
} }
static object * static PyObject *
sv_SetParam(self, args) sv_SetParam(self, args)
object *self, *args; PyObject *self, *args;
{ {
return doParams(self, args, svSetParam, 0); return doParams(self, args, svSetParam, 0);
} }
static struct methodlist svideo_methods[] = { static PyMethodDef svideo_methods[] = {
{"BindGLWindow", (method)sv_BindGLWindow}, {"BindGLWindow", (PyCFunction)sv_BindGLWindow},
{"EndContinuousCapture",(method)sv_EndContinuousCapture}, {"EndContinuousCapture",(PyCFunction)sv_EndContinuousCapture},
{"IsVideoDisplayed", (method)sv_IsVideoDisplayed}, {"IsVideoDisplayed", (PyCFunction)sv_IsVideoDisplayed},
{"OutputOffset", (method)sv_OutputOffset}, {"OutputOffset", (PyCFunction)sv_OutputOffset},
{"PutFrame", (method)sv_PutFrame}, {"PutFrame", (PyCFunction)sv_PutFrame},
{"QuerySize", (method)sv_QuerySize}, {"QuerySize", (PyCFunction)sv_QuerySize},
{"SetSize", (method)sv_SetSize}, {"SetSize", (PyCFunction)sv_SetSize},
{"SetStdDefaults", (method)sv_SetStdDefaults}, {"SetStdDefaults", (PyCFunction)sv_SetStdDefaults},
{"UseExclusive", (method)sv_UseExclusive}, {"UseExclusive", (PyCFunction)sv_UseExclusive},
{"WindowOffset", (method)sv_WindowOffset}, {"WindowOffset", (PyCFunction)sv_WindowOffset},
{"InitContinuousCapture",(method)sv_InitContinuousCapture}, {"InitContinuousCapture",(PyCFunction)sv_InitContinuousCapture},
{"CaptureBurst", (method)sv_CaptureBurst}, {"CaptureBurst", (PyCFunction)sv_CaptureBurst},
{"CaptureOneFrame", (method)sv_CaptureOneFrame}, {"CaptureOneFrame", (PyCFunction)sv_CaptureOneFrame},
{"GetCaptureData", (method)sv_GetCaptureData}, {"GetCaptureData", (PyCFunction)sv_GetCaptureData},
{"CloseVideo", (method)sv_CloseVideo}, {"CloseVideo", (PyCFunction)sv_CloseVideo},
{"LoadMap", (method)sv_LoadMap}, {"LoadMap", (PyCFunction)sv_LoadMap},
{"GetParam", (method)sv_GetParam}, {"GetParam", (PyCFunction)sv_GetParam},
{"GetParamRange", (method)sv_GetParamRange}, {"GetParamRange", (PyCFunction)sv_GetParamRange},
{"SetParam", (method)sv_SetParam}, {"SetParam", (PyCFunction)sv_SetParam},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
static object * static PyObject *
sv_conversion(self, args, function, inputfactor, factor) sv_conversion(self, args, function, inputfactor, factor)
object *self, *args; PyObject *self, *args;
void (*function)(); void (*function)();
int inputfactor; int inputfactor;
float factor; float factor;
{ {
int invert, width, height, inputlength; int invert, width, height, inputlength;
char *input; char *input, *str;
object *output; PyObject *output;
if (!getargs(args, "(is#ii)", &invert, &input, &inputlength, &width, &height)) if (!PyArg_Parse(args, "(is#ii)", &invert,
&input, &inputlength, &width, &height))
return NULL; return NULL;
if (width * height * inputfactor > inputlength) { if (width * height * inputfactor > inputlength) {
err_setstr(SvError, "input buffer not long enough"); PyErr_SetString(SvError, "input buffer not long enough");
return NULL; return NULL;
} }
output = newsizedstringobject(NULL, (int) (width * height * factor)); if (!(output = PyString_FromStringAndSize(NULL,
if (output == NULL) (int)(width * height * factor))))
return NULL; return NULL;
(*function)(invert, input, getstringvalue(output), width, height); str = PyString_AsString(output);
if (!str) {
Py_DECREF(output);
return NULL;
}
(*function)(invert, input, str, width, height);
return output; return output;
} }
static object * static PyObject *
sv_InterleaveFields(self, args) sv_InterleaveFields(self, args)
object *self, *args; PyObject *self, *args;
{ {
return sv_conversion(self, args, svInterleaveFields, 1, 1.0); return sv_conversion(self, args, svInterleaveFields, 1, 1.0);
} }
static object * static PyObject *
sv_RGB8toRGB32(self, args) sv_RGB8toRGB32(self, args)
object *self, *args; PyObject *self, *args;
{ {
return sv_conversion(self, args, svRGB8toRGB32, 1, (float) sizeof(long)); return sv_conversion(self, args, svRGB8toRGB32, 1, (float) sizeof(long));
} }
static object * static PyObject *
sv_YUVtoRGB(self, args) sv_YUVtoRGB(self, args)
object *self, *args; PyObject *self, *args;
{ {
return sv_conversion(self, args, svYUVtoRGB, 2, (float) sizeof(long)); return sv_conversion(self, args, svYUVtoRGB, 2, (float) sizeof(long));
} }
...@@ -919,19 +989,19 @@ svideo_dealloc(self) ...@@ -919,19 +989,19 @@ svideo_dealloc(self)
{ {
if (self->ob_svideo != NULL) if (self->ob_svideo != NULL)
(void) svCloseVideo(self->ob_svideo); (void) svCloseVideo(self->ob_svideo);
DEL(self); PyMem_DEL(self);
} }
static object * static PyObject *
svideo_getattr(self, name) svideo_getattr(self, name)
svobject *self; svobject *self;
char *name; char *name;
{ {
return findmethod(svideo_methods, (object *)self, name); return Py_FindMethod(svideo_methods, (PyObject *)self, name);
} }
typeobject Svtype = { PyTypeObject Svtype = {
OB_HEAD_INIT(&Typetype) PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/ 0, /*ob_size*/
"sv", /*tp_name*/ "sv", /*tp_name*/
sizeof(svobject), /*tp_size*/ sizeof(svobject), /*tp_size*/
...@@ -945,13 +1015,13 @@ typeobject Svtype = { ...@@ -945,13 +1015,13 @@ typeobject Svtype = {
0, /*tp_repr*/ 0, /*tp_repr*/
}; };
static object * static PyObject *
newsvobject(svp) newsvobject(svp)
SV_nodeP svp; SV_nodeP svp;
{ {
svobject *p; svobject *p;
p = NEWOBJ(svobject, &Svtype); p = PyObject_NEW(svobject, &Svtype);
if (p == NULL) if (p == NULL)
return NULL; return NULL;
p->ob_svideo = svp; p->ob_svideo = svp;
...@@ -960,16 +1030,16 @@ newsvobject(svp) ...@@ -960,16 +1030,16 @@ newsvobject(svp)
p->ob_info.width = 0; p->ob_info.width = 0;
p->ob_info.height = 0; p->ob_info.height = 0;
p->ob_info.samplingrate = 0; p->ob_info.samplingrate = 0;
return (object *) p; return (PyObject *) p;
} }
static object * static PyObject *
sv_OpenVideo(self, args) sv_OpenVideo(self, args)
object *self, *args; PyObject *self, *args;
{ {
SV_nodeP svp; SV_nodeP svp;
if (!getnoarg(args)) if (!PyArg_Parse(args, ""))
return NULL; return NULL;
svp = svOpenVideo(); svp = svOpenVideo();
...@@ -979,23 +1049,23 @@ sv_OpenVideo(self, args) ...@@ -979,23 +1049,23 @@ sv_OpenVideo(self, args)
return newsvobject(svp); return newsvobject(svp);
} }
static struct methodlist sv_methods[] = { static PyMethodDef sv_methods[] = {
{"InterleaveFields", (method)sv_InterleaveFields}, {"InterleaveFields", (PyCFunction)sv_InterleaveFields},
{"RGB8toRGB32", (method)sv_RGB8toRGB32}, {"RGB8toRGB32", (PyCFunction)sv_RGB8toRGB32},
{"YUVtoRGB", (method)sv_YUVtoRGB}, {"YUVtoRGB", (PyCFunction)sv_YUVtoRGB},
{"OpenVideo", (method)sv_OpenVideo}, {"OpenVideo", (PyCFunction)sv_OpenVideo},
{NULL, NULL} /* Sentinel */ {NULL, NULL} /* Sentinel */
}; };
void void
initsv() initsv()
{ {
object *m, *d; PyObject *m, *d;
m = initmodule("sv", sv_methods); m = Py_InitModule("sv", sv_methods);
d = getmoduledict(m); d = PyModule_GetDict(m);
SvError = newstringobject("sv.error"); SvError = PyString_FromString("sv.error");
if (SvError == NULL || dictinsert(d, "error", SvError) != 0) if (SvError == NULL || PyDict_SetItemString(d, "error", SvError) != 0)
fatal("can't define sv.error"); Py_FatalError("can't define sv.error");
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment