Line 0
Link Here
|
|
|
1 |
/* |
2 |
* This file is part of emesene. |
3 |
* |
4 |
* Emesene is free software; you can redistribute it and/or modify |
5 |
* it under the terms of the GNU General Public License as published by |
6 |
* the Free Software Foundation; either version 2 of the License, or |
7 |
* (at your option) any later version. |
8 |
* |
9 |
* Emesene is distributed in the hope that it will be useful, |
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
* GNU General Public License for more details. |
13 |
* |
14 |
* You should have received a copy of the GNU General Public License |
15 |
* along with this program; if not, write to the Free Software |
16 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
17 |
* MA 02110-1301, USA. |
18 |
* |
19 |
* TODO: |
20 |
* - set useful exception messages |
21 |
*/ |
22 |
|
23 |
#include <Python.h> |
24 |
#include "py_libmimic.h" |
25 |
|
26 |
/* init module */ |
27 |
PyMODINIT_FUNC |
28 |
initlibmimic(void) |
29 |
{ |
30 |
PyObject* m; |
31 |
m = Py_InitModule("libmimic", LibmimicMethods); |
32 |
} |
33 |
static void close_decoder(void* ptr) { |
34 |
MimicDecoder* decoder; |
35 |
decoder = (MimicDecoder*)ptr; |
36 |
mimic_close(decoder->codec); |
37 |
free(decoder); |
38 |
} |
39 |
static PyObject* libmimic_new_decoder(PyObject *self, PyObject *args) { |
40 |
MimicDecoder* decoder; |
41 |
|
42 |
/* accept no arguments */ |
43 |
if(!PyArg_ParseTuple(args, "")) |
44 |
return NULL; |
45 |
|
46 |
decoder = (MimicDecoder*)malloc(sizeof(MimicDecoder)); |
47 |
if(!decoder) { |
48 |
PyErr_NoMemory(); |
49 |
return NULL; |
50 |
} |
51 |
|
52 |
decoder->codec = mimic_open(); |
53 |
decoder->is_init = 0; |
54 |
|
55 |
return PyCObject_FromVoidPtr(decoder, close_decoder); |
56 |
} |
57 |
static PyObject* libmimic_new_encoder(PyObject *self, PyObject *args) { |
58 |
MimicEncoder* encoder; |
59 |
unsigned char arg_resolution; |
60 |
MimicResEnum resolution; |
61 |
|
62 |
/* accept one argument, resolution: 0 = low, 1 = high */ |
63 |
if(!PyArg_ParseTuple(args, "b", &arg_resolution)) |
64 |
return NULL; |
65 |
|
66 |
encoder = (MimicEncoder*)malloc(sizeof(MimicEncoder)); |
67 |
if(!encoder) { |
68 |
PyErr_NoMemory(); |
69 |
return NULL; |
70 |
} |
71 |
|
72 |
encoder->codec = mimic_open(); |
73 |
encoder->num_frames = 0; |
74 |
|
75 |
resolution = arg_resolution ? MIMIC_RES_HIGH : MIMIC_RES_LOW; |
76 |
mimic_encoder_init(encoder->codec, resolution); |
77 |
|
78 |
return PyCObject_FromVoidPtr(encoder, close_decoder); |
79 |
} |
80 |
static PyObject* libmimic_decode(PyObject *self, PyObject *args) { |
81 |
MimicDecoder* decoder; |
82 |
BYTE* output; |
83 |
unsigned int width, height, length; |
84 |
|
85 |
/* parameters */ |
86 |
PyObject* pyobj = NULL; |
87 |
BYTE* input; |
88 |
int inputsize; |
89 |
|
90 |
if(!PyArg_ParseTuple(args, "Os#", &pyobj, &input, &inputsize)) |
91 |
return NULL; |
92 |
|
93 |
decoder = PyCObject_AsVoidPtr(pyobj); |
94 |
if(!decoder) { |
95 |
/* TODO: ERROR */ |
96 |
return NULL; |
97 |
} |
98 |
|
99 |
if (!decoder->is_init) { |
100 |
if (!mimic_decoder_init(decoder->codec, input + HEADER_SIZE)) { |
101 |
/* TODO: ERROR */ |
102 |
return NULL; |
103 |
} else { |
104 |
decoder->is_init = 1; |
105 |
} |
106 |
} |
107 |
|
108 |
mimic_get_property(decoder->codec, "buffer_size", &length); |
109 |
mimic_get_property(decoder->codec, "width", &width); |
110 |
mimic_get_property(decoder->codec, "height", &height); |
111 |
|
112 |
output = (BYTE*)malloc(length); |
113 |
if(!output) { |
114 |
PyErr_NoMemory(); |
115 |
return NULL; |
116 |
} |
117 |
|
118 |
if (!mimic_decode_frame(decoder->codec, input + HEADER_SIZE, output)) { |
119 |
/* TODO: ERROR */ |
120 |
free(output); |
121 |
return NULL; |
122 |
} |
123 |
|
124 |
PyObject* val = Py_BuildValue("iis#", width, height, output, length); |
125 |
free(output); |
126 |
return val; |
127 |
} |
128 |
|
129 |
static PyObject* libmimic_encode(PyObject *self, PyObject *args) { |
130 |
MimicEncoder* encoder; |
131 |
BYTE* output; |
132 |
int length, width, height; |
133 |
|
134 |
/* parameters */ |
135 |
PyObject* pyobj = NULL; |
136 |
BYTE* input; |
137 |
int inputsize; |
138 |
|
139 |
if(!PyArg_ParseTuple(args, "Os#", &pyobj, &input, &inputsize)) |
140 |
return NULL; |
141 |
|
142 |
encoder = PyCObject_AsVoidPtr(pyobj); |
143 |
if(!encoder) { |
144 |
/* TODO: ERROR */ |
145 |
return NULL; |
146 |
} |
147 |
|
148 |
mimic_get_property(encoder->codec, "buffer_size", &length); |
149 |
|
150 |
output = (BYTE*)malloc(length * 3); |
151 |
if(!output) { |
152 |
PyErr_NoMemory(); |
153 |
return NULL; |
154 |
} |
155 |
|
156 |
if (!mimic_encode_frame(encoder->codec, input, output, &length, |
157 |
encoder->num_frames % 10 == 0)) { |
158 |
/* TODO: ERROR */ |
159 |
free(output); |
160 |
return NULL; |
161 |
} |
162 |
|
163 |
encoder->num_frames++; |
164 |
|
165 |
mimic_get_property(encoder->codec, "width", &width); |
166 |
mimic_get_property(encoder->codec, "height", &height); |
167 |
PyObject* val = Py_BuildValue("s#ii", output, length, width, height); |
168 |
free(output); |
169 |
|
170 |
return val; |
171 |
} |
172 |
|