-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathomxcv.cpp
More file actions
346 lines (307 loc) · 11.5 KB
/
omxcv.cpp
File metadata and controls
346 lines (307 loc) · 11.5 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
* @file omxcv.cpp
* @brief Routines to perform hardware accelerated H.264 encoding on the RPi.
*/
#include "omxcv-config.h"
#include "omxcv.h"
#include "omxcv-impl.h"
using namespace omxcv;
using std::this_thread::sleep_for;
using std::chrono::milliseconds;
using std::chrono::steady_clock;
using std::chrono::duration_cast;
#include <cstdio>
#include <cstdlib>
#include <ctime>
#define TIMEDIFF(start) (duration_cast<milliseconds>(steady_clock::now() - start).count())
#ifdef ENABLE_NEON
extern "C" void omxcv_bgr2rgb_neon(const unsigned char *src, unsigned char *dst, int n);
#endif
/**
* Perform the BGR2RGB conversion.
* @param [in] src The source buffer.
* @param [in] dst The destination buffer.
* @param [in] stride The stride of the image.
*/
void BGR2RGB(const cv::Mat &src, uint8_t *dst, int stride) {
#ifdef ENABLE_NEON
for (int i = 0; i < src.rows; i++) {
const uint8_t *buffer = src.ptr<const uint8_t>(i);
omxcv_bgr2rgb_neon(buffer, dst+stride*i, src.cols);
}
#else
cv::Mat omat(src.rows, src.cols, CV_8UC3, dst, stride);
cv::cvtColor(src, omat, CV_BGR2RGB);
#endif
}
/**
* Constructor.
* @param [in] name The file to save to.
* @param [in] width The video width.
* @param [in] height The video height.
* @param [in] bitrate The bitrate, in Kbps.
* @param [in] fpsnum The FPS numerator.
* @param [in] fpsden The FPS denominator.
*/
OmxCvImpl::OmxCvImpl(const char *name, int width, int height, int bitrate,
int fpsnum, int fpsden) :
m_width(width), m_height(height), m_stride(((width + 31) & ~31) * 3), m_bitrate(
bitrate), m_filename(name), m_stop { false } {
int ret;
bcm_host_init();
if (fpsden <= 0 || fpsnum <= 0) {
fpsden = 1;
fpsnum = 25;
}
m_fpsnum = fpsnum;
m_fpsden = fpsden;
//Initialise OpenMAX and the IL client.
CHECKED(OMX_Init() != OMX_ErrorNone, "OMX_Init failed.");
m_ilclient = ilclient_init();
CHECKED(m_ilclient == NULL, "ILClient initialisation failed.");
ret = ilclient_create_component(m_ilclient, &m_encoder_component,
(char*) "video_encode",
(ILCLIENT_CREATE_FLAGS_T)(
ILCLIENT_DISABLE_ALL_PORTS | ILCLIENT_ENABLE_INPUT_BUFFERS
| ILCLIENT_ENABLE_OUTPUT_BUFFERS));
CHECKED(ret != 0, "ILCient video_encode component creation failed.");
//Set input definition to the encoder
OMX_PARAM_PORTDEFINITIONTYPE def = {};
def.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
def.nVersion.nVersion = OMX_VERSION;
def.nPortIndex = OMX_ENCODE_PORT_IN;
ret = OMX_GetParameter(ILC_GET_HANDLE(m_encoder_component),
OMX_IndexParamPortDefinition, &def);
CHECKED(ret != OMX_ErrorNone,
"OMX_GetParameter failed for encode port in.");
def.format.video.nFrameWidth = m_width;
def.format.video.nFrameHeight = m_height;
def.format.video.xFramerate = 30 << 16;
//Must be a multiple of 16
def.format.video.nSliceHeight = (m_height + 15) & ~15;
//Must be a multiple of 32
def.format.video.nStride = m_stride;
def.format.video.eColorFormat = OMX_COLOR_Format24bitBGR888; //OMX_COLOR_Format32bitABGR8888;//OMX_COLOR_FormatYUV420PackedPlanar;
//Must be manually defined to ensure sufficient size if stride needs to be rounded up to multiple of 32.
def.nBufferSize = def.format.video.nStride * def.format.video.nSliceHeight;
//We allocate 6 input buffers.
def.nBufferCountActual = 6;
ret = OMX_SetParameter(ILC_GET_HANDLE(m_encoder_component),
OMX_IndexParamPortDefinition, &def);
CHECKED(ret != OMX_ErrorNone,
"OMX_SetParameter failed for input format definition.");
//Set the output format of the encoder
OMX_VIDEO_PARAM_PORTFORMATTYPE format = {};
format.nSize = sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE);
format.nVersion.nVersion = OMX_VERSION;
format.nPortIndex = OMX_ENCODE_PORT_OUT;
format.eCompressionFormat = OMX_VIDEO_CodingAVC;
//format.eCompressionFormat = OMX_VIDEO_CodingMPEG4;
ret = OMX_SetParameter(ILC_GET_HANDLE(m_encoder_component),
OMX_IndexParamVideoPortFormat, &format);
CHECKED(ret != OMX_ErrorNone,
"OMX_SetParameter failed for setting encoder output format.");
//Set the encoding bitrate
OMX_VIDEO_PARAM_BITRATETYPE bitrate_type = {};
bitrate_type.nSize = sizeof(OMX_VIDEO_PARAM_BITRATETYPE);
bitrate_type.nVersion.nVersion = OMX_VERSION;
bitrate_type.eControlRate = OMX_Video_ControlRateVariable;
bitrate_type.nTargetBitrate = bitrate * 1000;
bitrate_type.nPortIndex = OMX_ENCODE_PORT_OUT;
ret = OMX_SetParameter(ILC_GET_HANDLE(m_encoder_component),
OMX_IndexParamVideoBitrate, &bitrate_type);
CHECKED(ret != OMX_ErrorNone,
"OMX_SetParameter failed for setting encoder bitrate.");
// if (format.eCompressionFormat == OMX_VIDEO_CodingAVC) {
//// //Set the output profile level of the encoder
//// OMX_VIDEO_PARAM_PROFILELEVELTYPE profileLevel; // OMX_IndexParamVideoProfileLevelCurrent
//// profileLevel.nSize = sizeof(OMX_VIDEO_PARAM_PROFILELEVELTYPE);
//// profileLevel.nVersion.nVersion = OMX_VERSION;
//// profileLevel.nPortIndex = OMX_ENCODE_PORT_OUT;
//// profileLevel.eProfile = OMX_VIDEO_AVCProfileMain;
//// profileLevel.eLevel = OMX_VIDEO_AVCLevel31;
//// ret = OMX_SetParameter(ILC_GET_HANDLE(m_encoder_component),
//// OMX_IndexParamVideoProfileLevelCurrent, &profileLevel);
//
// //I think this decreases the chance of NALUs being split across buffers.
// /*
// OMX_CONFIG_BOOLEANTYPE frg = {0};
// frg.nSize = sizeof(OMX_CONFIG_BOOLEANTYPE);
// frg.nVersion.nVersion = OMX_VERSION;
// frg.bEnabled = OMX_TRUE;
// ret = OMX_SetParameter(ILC_GET_HANDLE(m_encoder_component),
// OMX_IndexConfigMinimiseFragmentation, &frg);
// CHECKED(ret != 0, "OMX_SetParameter failed for setting fragmentation minimisation.");
// */
//
// CHECKED(ret != OMX_ErrorNone,
// "OMX_SetParameter failed for setting encoder output format.");
// //We want at most one NAL per output buffer that we receive.
// OMX_CONFIG_BOOLEANTYPE nal = {};
// nal.nSize = sizeof(OMX_CONFIG_BOOLEANTYPE);
// nal.nVersion.nVersion = OMX_VERSION;
// nal.bEnabled = OMX_TRUE;
// ret = OMX_SetParameter(ILC_GET_HANDLE(m_encoder_component),
// OMX_IndexParamBrcmNALSSeparate, &nal);
// CHECKED(ret != 0,
// "OMX_SetParameter failed for setting separate NALUs.");
//
// //We want the encoder to write the NALU length instead start codes.
// OMX_NALSTREAMFORMATTYPE nal2 = {};
// nal2.nSize = sizeof(OMX_NALSTREAMFORMATTYPE);
// nal2.nVersion.nVersion = OMX_VERSION;
// nal2.nPortIndex = OMX_ENCODE_PORT_OUT;
// nal2.eNaluFormat = OMX_NaluFormatFourByteInterleaveLength;
// ret = OMX_SetParameter(ILC_GET_HANDLE(m_encoder_component),
// (OMX_INDEXTYPE) OMX_IndexParamNalStreamFormatSelect, &nal2);
// CHECKED(ret != 0, "OMX_SetParameter failed for setting NALU format.");
// }
ret = ilclient_change_component_state(m_encoder_component, OMX_StateIdle);
CHECKED(ret != 0, "ILClient failed to change encoder to idle state.");
ret = ilclient_enable_port_buffers(m_encoder_component, OMX_ENCODE_PORT_IN,
NULL, NULL, NULL);
CHECKED(ret != 0, "ILClient failed to enable input buffers.");
ret = ilclient_enable_port_buffers(m_encoder_component, OMX_ENCODE_PORT_OUT,
NULL, NULL, NULL);
CHECKED(ret != 0, "ILClient failed to enable output buffers.");
ret = ilclient_change_component_state(m_encoder_component,
OMX_StateExecuting);
CHECKED(ret != 0, "ILClient failed to change encoder to executing stage.");
m_ofstream.open(m_filename, std::ios::out);
//Start the worker thread for dumping the encoded data
m_input_worker = std::thread(&OmxCvImpl::input_worker, this);
}
/**
* Destructor.
* @return Return_Description
*/
OmxCvImpl::~OmxCvImpl() {
m_stop = true;
m_input_signaller.notify_one();
m_input_worker.join();
//Teardown similar to hello_encode
ilclient_change_component_state(m_encoder_component, OMX_StateIdle);
ilclient_disable_port_buffers(m_encoder_component, OMX_ENCODE_PORT_IN, NULL,
NULL, NULL);
ilclient_disable_port_buffers(m_encoder_component, OMX_ENCODE_PORT_OUT,
NULL, NULL, NULL);
//ilclient_change_component_state(m_encoder_component, OMX_StateIdle);
ilclient_change_component_state(m_encoder_component, OMX_StateLoaded);
COMPONENT_T *list[] = { m_encoder_component, NULL };
ilclient_cleanup_components(list);
ilclient_destroy(m_ilclient);
}
/**
* Input encoding routine.
*/
void OmxCvImpl::input_worker() {
std::unique_lock < std::mutex > lock(m_input_mutex);
OMX_BUFFERHEADERTYPE *out = ilclient_get_output_buffer(m_encoder_component,
OMX_ENCODE_PORT_OUT, 1);
while (true) {
m_input_signaller.wait(lock,
[this] {return m_stop || m_input_queue.size() > 0;});
if (m_stop) {
if (m_input_queue.size() > 0) {
printf(
"Stop acknowledged but need to flush the input buffer (%d)...\n",
m_input_queue.size());
} else {
break;
}
}
//auto proc_start = steady_clock::now();
std::pair<OMX_BUFFERHEADERTYPE *, int64_t> frame =
m_input_queue.front();
m_input_queue.pop_front();
lock.unlock();
//auto conv_start = steady_clock::now();
//static int framecounter = 0;
OMX_EmptyThisBuffer(ILC_GET_HANDLE(m_encoder_component), frame.first);
//fflush(stdout);
//printf("Encoding time (ms): %d [%d]\r", (int)TIMEDIFF(conv_start), ++framecounter);
do {
out->nFilledLen = 0; //I don't think this is necessary, but whatever.
OMX_FillThisBuffer(ILC_GET_HANDLE(m_encoder_component), out);
out = ilclient_get_output_buffer(m_encoder_component,
OMX_ENCODE_PORT_OUT, 1);
} while (!write_data(out, frame.second));
lock.lock();
//printf("Total processing time (ms): %d\n", (int)TIMEDIFF(proc_start));
}
//Needed because we call ilclient_get_output_buffer last.
//Otherwise ilclient waits forever for the buffer to be filled.
OMX_FillThisBuffer(ILC_GET_HANDLE(m_encoder_component), out);
}
/**
* Output muxing routine.
* @param [in] out Buffer to be saved.
* @param [in] timestamp Timestamp of this buffer.
* @return true if buffer was saved.
*/
bool OmxCvImpl::write_data(OMX_BUFFERHEADERTYPE *out, int64_t timestamp) {
if (out->nFilledLen != 0) {
printf("write data : %d\n", (int)out->nFilledLen);
m_ofstream.write((const char*)out->pBuffer, (int)out->nFilledLen);
return true;
} else {
printf("write data : return false\n");
return true;
}
}
/**
* Enqueue video to be encoded.
* @param [in] mat The mat to be encoded.
* @return true iff enqueued.
*/
bool OmxCvImpl::process(const cv::Mat &mat) {
OMX_BUFFERHEADERTYPE *in = ilclient_get_input_buffer(m_encoder_component,
OMX_ENCODE_PORT_IN, 0);
if (in == NULL) {
printf("No free buffer; dropping frame!\n");
return false;
}
assert(mat.cols == m_width && mat.rows == m_height);
auto now = steady_clock::now();
memcpy(in->pBuffer, mat.data, m_stride * m_height);
//BGR2RGB(mat, in->pBuffer, m_stride);
in->nFilledLen = in->nAllocLen;
std::unique_lock < std::mutex > lock(m_input_mutex);
if (m_frame_count++ == 0) {
m_frame_start = now;
}
m_input_queue.push_back(
std::pair<OMX_BUFFERHEADERTYPE *, int64_t>(in,
duration_cast < milliseconds
> (now - m_frame_start).count()));
lock.unlock();
m_input_signaller.notify_one();
return true;
}
/**
* Constructor for our wrapper.
* @param [in] name The file to save to.
* @param [in] width The video width.
* @param [in] height The video height.
* @param [in] bitrate The bitrate, in Kbps.
* @param [in] fpsnum The FPS numerator.
* @param [in] fpsden The FPS denominator.
*/
OmxCv::OmxCv(const char *name, int width, int height, int bitrate, int fpsnum,
int fpsden) {
m_impl = new OmxCvImpl(name, width, height, bitrate, fpsnum, fpsden);
}
/**
* Wrapper destructor.
*/
OmxCv::~OmxCv() {
delete m_impl;
}
/**
* Encode image.
* @param [in] in Image to be encoded.
* @return true iff the image was encoded.
*/
bool OmxCv::Encode(const cv::Mat &in) {
return m_impl->process(in);
}