forked from jnohlgard/python-v4l2capture
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibvideolive.cpp
312 lines (270 loc) · 10.8 KB
/
libvideolive.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// libvideolive
// Python extension to capture and stream video
//
// 2009, 2010, 2011 Fredrik Portstrom, released into the public domain
// 2011, Joakim Gebart
// 2013, Tim Sheerman-Chase
// See README for license
#include <Python.h>
#include <string.h>
#include <string>
#include <sstream>
#include <map>
#include <vector>
#include <stdexcept>
#include <iostream>
#include <pthread.h>
#include "pixfmt.h"
#include "videoout.h"
#include "videoin.h"
#include "videooutfile.h"
#include "pixfmt.h"
// *********************************************************************
PyObject *InsertHuffmanTable(PyObject *self, PyObject *args)
{
/* This converts an MJPEG frame into a standard JPEG binary
MJPEG images omit the huffman table if the standard table
is used. If it is missing, this function adds the table
into the file structure. */
if(PyTuple_Size(args) < 1)
{
PyErr_SetString(PyExc_TypeError, "Function requires 1 argument");
return NULL;
}
PyObject *inBuffer = PyTuple_GetItem(args, 0);
if(!PyString_Check(inBuffer))
{
PyErr_SetString(PyExc_TypeError, "Argument 1 must be a string.");
//PyObject* type = PyObject_Type(inBuffer);
//PyObject_Print(type, stdout, Py_PRINT_RAW);
//Py_CLEAR(type);
return NULL;
}
unsigned char* inBufferPtr = (unsigned char*)PyString_AsString(inBuffer);
Py_ssize_t inBufferLen = PyString_Size(inBuffer);
std::string outBuffer;
InsertHuffmanTableCTypes((unsigned char*)inBufferPtr, inBufferLen, outBuffer);
PyObject *outBufferPy = PyString_FromStringAndSize(outBuffer.c_str(), outBuffer.length());
return outBufferPy;
}
PyObject *DecodeAndResizeFrameHighLevel(PyObject *self, PyObject *args)
{
if (PyErr_Occurred() != NULL)
throw std::runtime_error("Python error set with unexpected state.");
//0 string src pixFormat
//1 int src width
//2 int src height
//3 ByteArray src data
//4 string out pixFormat
//5 int out width
//6 int out height
//7 ByteArray out data
if(PyTuple_Size(args) < 8)
{
PyErr_SetString(PyExc_TypeError, "Function requires 8 arguments (and 1 optional)");
return NULL;
}
//Input image
PyObject *inPixFmt = PyTuple_GetItem(args, 0);
if(!PyString_Check(inPixFmt)) {PyErr_SetString(PyExc_TypeError, "Argument 1 must be a string."); return NULL;}
PyObject *inWidth = PyTuple_GetItem(args, 1);
if(!PyInt_Check(inWidth)) {PyErr_SetString(PyExc_TypeError, "Argument 2 must be an int."); return NULL;}
PyObject *inHeight = PyTuple_GetItem(args, 2);
if(!PyInt_Check(inHeight)) {PyErr_SetString(PyExc_TypeError, "Argument 3 must be an int."); return NULL;}
PyObject *inData = PyTuple_GetItem(args, 3);
if(!PyByteArray_Check(inData)) {PyErr_SetString(PyExc_TypeError, "Argument 4 must be a byte array."); return NULL;}
//Output image
PyObject *outPixFmt = PyTuple_GetItem(args, 4);
if(!PyString_Check(outPixFmt)) {PyErr_SetString(PyExc_TypeError, "Argument 5 must be a string."); return NULL;}
PyObject *outWidth = PyTuple_GetItem(args, 5);
if(!PyInt_Check(outWidth)) {PyErr_SetString(PyExc_TypeError, "Argument 6 must be an int."); return NULL;}
PyObject *outHeight = PyTuple_GetItem(args, 6);
if(!PyInt_Check(outHeight)) {PyErr_SetString(PyExc_TypeError, "Argument 7 must be an int."); return NULL;}
PyObject *outData = PyTuple_GetItem(args, 7);
if(!PyByteArray_Check(outData)) {PyErr_SetString(PyExc_TypeError, "Argument 8 must be a byte array."); return NULL;}
//Optional arguments
PyObject *metaOut = NULL;
if(PyTuple_Size(args) >= 9)
{
metaOut = PyTuple_GetItem(args, 8);
if(!PyDict_Check(metaOut) && metaOut != Py_None)
{
PyErr_SetString(PyExc_TypeError, "Argument 9 (if set) must be a dict or None.");
return NULL;
}
if(metaOut==Py_None)
metaOut = NULL;
}
unsigned char *buffOut = NULL;
unsigned buffOutLen = 0;
int useExistingBuff = 0;
if(PyByteArray_Size(outData) > 0)
{
buffOut = (unsigned char *)PyString_AsString(outData);
buffOutLen = PyByteArray_Size(outData);
useExistingBuff = 1;
}
int ret = 0;
try
{
int outWidthInt = PyInt_AsLong(outWidth);
int outHeightInt = PyInt_AsLong(outHeight);
unsigned char *inDataC = (unsigned char*)PyByteArray_AsString(inData);
long inDataLen = PyByteArray_Size(inData);
char *inPixFmtC = PyString_AsString(inPixFmt);
char *outPixFmtC = PyString_AsString(outPixFmt);
ret = DecodeAndResizeFrame(inDataC,
inDataLen,
inPixFmtC,
PyInt_AsLong(inWidth), PyInt_AsLong(inHeight),
outPixFmtC,
&buffOut,
&buffOutLen,
outWidthInt,
outHeightInt);
if(metaOut!=NULL && ret > 0)
{
PyDict_SetItemString(metaOut, "width", PyInt_FromLong(outWidthInt));
PyDict_SetItemString(metaOut, "height", PyInt_FromLong(outHeightInt));
PyDict_SetItemString(metaOut, "format", PyString_FromString(outPixFmtC));
}
}
catch(std::exception &err)
{
PyErr_SetString(PyExc_RuntimeError, err.what());
return NULL;
}
if(!useExistingBuff && ret > 0)
{
PyByteArray_Resize(outData, buffOutLen);
memcpy(PyByteArray_AsString(outData), buffOut, buffOutLen);
delete [] buffOut;
}
if (PyErr_Occurred() != NULL)
throw std::runtime_error("Python error set with unexpected state.");
return PyInt_FromLong(ret);
}
// *********************************************************************
static PyMethodDef Device_manager_methods[] = {
{"open", (PyCFunction)Device_manager_open, METH_VARARGS,
"open(dev = '\\dev\\video0')\n\n"
"Open video capture."},
{"set_format", (PyCFunction)Device_manager_set_format, METH_VARARGS,
"set_format(dev, size_x, size_y, pixel_format='RGB24') -> size_x, size_y\n\n"
"Request the video device to set image size and format. The device may "
"choose another size than requested and will return its choice. The "
"pixel format may be either RGB24, YUV420 or MJPEG."},
{"start", (PyCFunction)Device_manager_Start, METH_VARARGS,
"start(dev = '\\dev\\video0', reqSize=(640, 480), reqFps = 30, fmt = 'MJPEG\', buffer_count = 10)\n\n"
"Start video capture."},
{"get_frame", (PyCFunction)Device_manager_Get_frame, METH_VARARGS,
"start(dev = '\\dev\\video0'\n\n"
"Get video frame."},
{"stop", (PyCFunction)Device_manager_stop, METH_VARARGS,
"stop(dev = '\\dev\\video0')\n\n"
"Stop video capture."},
{"close", (PyCFunction)Device_manager_close, METH_VARARGS,
"close(dev = '\\dev\\video0')\n\n"
"Close video device. Subsequent calls to other methods will fail."},
{"list_devices", (PyCFunction)Device_manager_list_devices, METH_NOARGS,
"list_devices()\n\n"
"List available capture devices."},
{NULL}
};
static PyTypeObject Device_manager_type = {
PyObject_HEAD_INIT(NULL)
0, "v4l2capture.Device_manager", sizeof(Device_manager), 0,
(destructor)Device_manager_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, Py_TPFLAGS_DEFAULT, "Device_manager(path)\n\nOpens the video device at "
"the given path and returns an object that can capture images. The "
"constructor and all methods except close may raise IOError.", 0, 0, 0,
0, 0, 0, Device_manager_methods, 0, 0, 0, 0, 0, 0, 0,
(initproc)Device_manager_init
};
static PyMethodDef Video_out_manager_methods[] = {
{"open", (PyCFunction)Video_out_manager_open, METH_VARARGS,
"open(dev = '\\dev\\video0', pixel_format, width, height)\n\n"
"Open video output."},
{"send_frame", (PyCFunction)Video_out_manager_Send_frame, METH_VARARGS,
"send_frame(dev = '\\dev\\video0', img, pixel_format, width, height)\n\n"
"Send frame to video stream output."},
{"close", (PyCFunction)Video_out_manager_close, METH_VARARGS,
"close(dev = '\\dev\\video0')\n\n"
"Close video device. Subsequent calls to other methods will fail."},
{"list_devices", (PyCFunction)Video_out_manager_list_devices, METH_NOARGS,
"list_devices()\n\n"
"List available capture devices."},
{NULL}
};
static PyTypeObject Video_out_manager_type = {
PyObject_HEAD_INIT(NULL)
0, "v4l2capture.Video_out_stream_manager", sizeof(Video_out_manager), 0,
(destructor)Video_out_manager_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, Py_TPFLAGS_DEFAULT, "Video_out_manager(path)\n\nOpens the video device at "
"the given path and returns an object that can capture images. The "
"constructor and all methods except close may raise IOError.", 0, 0, 0,
0, 0, 0, Video_out_manager_methods, 0, 0, 0, 0, 0, 0, 0,
(initproc)Video_out_manager_init
};
static PyMethodDef Video_out_file_manager_methods[] = {
{"open", (PyCFunction)Video_out_file_manager_open, METH_VARARGS,
"open(filename = 'out.wmv', width, height)\n\n"
"Open video output."},
{"send_frame", (PyCFunction)Video_out_file_manager_Send_frame, METH_VARARGS,
"send_frame(filename = 'out.wmv', img, pixel_format, width, height, timestamp=None)\n\n"
"Send frame to video stream output."},
{"close", (PyCFunction)Video_out_file_manager_close, METH_VARARGS,
"close(filename = 'out.wmv')\n\n"
"Close video device. Subsequent calls to other methods will fail."},
{"set_frame_rate", (PyCFunction)Video_out_file_manager_Set_Frame_Rate, METH_VARARGS,
"set_frame_rate(filename = 'out.wmv', frame_rate)\n\n"
"Set output frame rate."},
{"set_video_codec", (PyCFunction)Video_out_file_manager_Set_Video_Codec, METH_VARARGS,
"set_video_codec(filename = 'out.wmv', codec = 'H264', bitrate)\n\n"
"Set output video codec."},
{NULL}
};
static PyTypeObject Video_out_file_manager_type = {
PyObject_HEAD_INIT(NULL)
0, "v4l2capture.Video_out_file_manager", sizeof(Video_out_manager), 0,
(destructor)Video_out_file_manager_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, Py_TPFLAGS_DEFAULT, "Video_out_manager(path)\n\nOpens the video device at "
"the given path and returns an object that can capture images. The "
"constructor and all methods except close may raise IOError.", 0, 0, 0,
0, 0, 0, Video_out_file_manager_methods, 0, 0, 0, 0, 0, 0, 0,
(initproc)Video_out_file_manager_init
};
// *********************************************************************
static PyMethodDef module_methods[] = {
{ "InsertHuffmanTable", (PyCFunction)InsertHuffmanTable, METH_VARARGS, NULL },
{ "DecodeAndResizeFrame", (PyCFunction)DecodeAndResizeFrameHighLevel, METH_VARARGS, NULL },
{ NULL, NULL, 0, NULL }
};
PyMODINIT_FUNC initvideolive(void)
{
Device_manager_type.tp_new = PyType_GenericNew;
Video_out_manager_type.tp_new = PyType_GenericNew;
Video_out_file_manager_type.tp_new = PyType_GenericNew;
if(PyType_Ready(&Device_manager_type) < 0)
{
return;
}
if(PyType_Ready(&Video_out_manager_type) < 0)
{
return;
}
if(PyType_Ready(&Video_out_file_manager_type) < 0)
{
return;
}
PyObject *module = Py_InitModule3("videolive", module_methods,
"Capture and stream video.");
if(!module)
{
return;
}
Py_INCREF(&Device_manager_type);
PyModule_AddObject(module, "Video_in_stream_manager", (PyObject *)&Device_manager_type);
PyModule_AddObject(module, "Video_out_stream_manager", (PyObject *)&Video_out_manager_type);
PyModule_AddObject(module, "Video_out_file_manager", (PyObject *)&Video_out_file_manager_type);
}