-
Notifications
You must be signed in to change notification settings - Fork 3
/
_py.c
126 lines (96 loc) · 2.83 KB
/
_py.c
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
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "_demux.h"
static PyObject* open_wrapper(PyObject* self, PyObject* arg);
static PyObject* get_frame_wrapper(PyObject* self, PyObject* arg);
static PyObject* release_frame_wrapper(PyObject* self, PyObject* arg);
static PyObject* close_wrapper(PyObject* self, PyObject* arg);
static PyObject* seek_wrapper(PyObject* self, PyObject* arg);
static PyMethodDef methods[] =
{
{"open", open_wrapper, METH_VARARGS, "open"},
{"get_frame", get_frame_wrapper, METH_VARARGS, "get_frame"},
{"close", close_wrapper, METH_VARARGS, "close"},
{"seek", seek_wrapper, METH_VARARGS, "seek: ms stream_type seek_type"},
{NULL, NULL, 0, NULL}
};
#if PY_VERSION_HEX >= 0x03000000
PyMODINIT_FUNC
PyInit__demux(void) {
PyObject* m;
static PyModuleDef module_def = {
PyModuleDef_HEAD_INIT,
"_demux", /* m_name */
NULL, /* m_doc */
-1, /* m_size */
methods, /* m_methods */
};
m = PyModule_Create(&module_def);
return m;
}
#else
PyMODINIT_FUNC
init_demux(void)
{
PyObject* m = Py_InitModule("_demux", methods);
}
#endif
static PyObject* open_wrapper(PyObject* self, PyObject* args)
{
char* filename;
demux_ctx_t* ctx = NULL;
if (!PyArg_ParseTuple(args, "s", &filename))
return NULL;
ctx = demux_open(filename);
if (!ctx)
Py_RETURN_NONE;
return Py_BuildValue("l", ctx);
}
static PyObject* get_frame_wrapper(PyObject* self, PyObject* args)
{
PyObject* ret;
PyObject* bytes;
demux_ctx_t* ctx = NULL;
uint8_t* frame;
int width;
int height;
if (!PyArg_ParseTuple(args, "l", &ctx))
return NULL;
// demux frame
frame = demux_get_frame(ctx);
if (!frame)
Py_RETURN_NONE;
// convert RGB array to python string
width = demux_get_width(ctx);
height = demux_get_height(ctx);
bytes = PyBytes_FromStringAndSize((char *)frame, (width*height*3));
demux_release_frame(ctx, frame);
ret = Py_BuildValue("Sii", bytes, width, height);
Py_XDECREF(bytes);
return ret;
}
static PyObject* close_wrapper(PyObject* self, PyObject* args)
{
demux_ctx_t* ctx = NULL;
if (!PyArg_ParseTuple(args, "l", &ctx))
return NULL;
demux_close(ctx);
Py_RETURN_NONE;
}
static PyObject* seek_wrapper(PyObject* self, PyObject* args) {
PyObject* ret;
demux_ctx_t* ctx = NULL;
int ms = 0;
int stream_type = 0; // 0: video 1: audio
int seek_type = 0; // 0: goto 1: move
if (!PyArg_ParseTuple(args, "li|ii", &ctx, &ms, &stream_type, &seek_type))
return NULL;
int error;
if(seek_type == 0) {
error = demux_goto(ctx, stream_type, ms, -1);
} else {
error = demux_move(ctx, stream_type, ms);
}
ret = Py_BuildValue("i", error);
return ret;
}