-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathomxImageRead.c
294 lines (225 loc) · 8.57 KB
/
omxImageRead.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
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
//
// omxImageRead.c
// OpenMAX
//
// Created by Michael Kwasnicki on 17.09.17.
// Copyright © 2017 Michael Kwasnicki. All rights reserved.
//
#include "omxJPEGDec.h"
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/param.h>
#define OMX_SKIP64BIT
#include <IL/OMX_Component.h>
#include <IL/OMX_Core.h>
#include <interface/vcos/vcos.h>
#include "cHelper.h"
#include "omxHelper.h"
#include "omxDump.h"
typedef struct {
OMX_HANDLETYPE handle;
OMX_BUFFERHEADERTYPE *outputBuffer[3];
OMX_U32 outputPortIndex;
bool outputReady;
VCOS_SEMAPHORE_T handler_lock;
VCOS_SEMAPHORE_T portChangeLock;
int flushed;
} ComponentContext;
static OMX_ERRORTYPE omxEventHandler(
OMX_IN OMX_HANDLETYPE hComponent,
OMX_IN OMX_PTR pAppData,
OMX_IN OMX_EVENTTYPE eEvent,
OMX_IN OMX_U32 nData1,
OMX_IN OMX_U32 nData2,
OMX_IN OMX_PTR pEventData) {
printf("eEvent: %s, ", omxEventTypeEnum(eEvent));
//printf("eEvent: %s, nData1: %x, nData2: %x\n", omxEventTypeEnum(eEvent), nData1, nData2);
ComponentContext* ctx = (ComponentContext*)pAppData;
switch(eEvent) {
case OMX_EventCmdComplete:
printf("Command: %s, ", omxCommandTypeEnum(nData1));
switch (nData1) {
case OMX_CommandStateSet:
printf("State: %s\n", omxStateTypeEnum(nData2));
break;
case OMX_CommandPortDisable:
case OMX_CommandPortEnable:
printf("Port: %d\n", nData2);
break;
default:
printf("nData2: 0x%x\n", nData2);
}
vcos_semaphore_wait(&ctx->handler_lock);
if ( nData1 == OMX_CommandFlush ) {
ctx->flushed = true;
}
vcos_semaphore_post(&ctx->handler_lock);
break;
case OMX_EventPortSettingsChanged:
printf("Port: %d nData2: %x\n", nData1, nData2);
vcos_semaphore_post(&ctx->portChangeLock);
break;
case OMX_EventError:
printf(COLOR_RED "ErrorType: %s, nData2: %x\n" COLOR_NC, omxErrorTypeEnum(nData1), nData2);
if (nData1 != OMX_ErrorStreamCorrupt) {
assert(NULL);
}
break;
default:
printf("nData1: %x, nData2: %x\n", nData1, nData2);
printf("unhandeled event %x: %x %x\n", eEvent, nData1, nData2);
break;
}
return OMX_ErrorNone;
}
static OMX_ERRORTYPE omxEmptyBufferDone(
OMX_IN OMX_HANDLETYPE hComponent,
OMX_IN OMX_PTR pAppData,
OMX_IN OMX_BUFFERHEADERTYPE* pBuffer) {
puts("omxEmptyBufferDone");
ComponentContext *ctx = (ComponentContext*)pAppData;
vcos_semaphore_wait(&ctx->handler_lock);
vcos_semaphore_post(&ctx->handler_lock);
return OMX_ErrorNone;
}
static OMX_ERRORTYPE omxFillBufferDone(
OMX_OUT OMX_HANDLETYPE hComponent,
OMX_OUT OMX_PTR pAppData,
OMX_OUT OMX_BUFFERHEADERTYPE* pBuffer) {
puts("omxFillBufferDone");
ComponentContext *ctx = (ComponentContext*)pAppData;
vcos_semaphore_wait(&ctx->handler_lock);
ctx->outputReady = true;
vcos_semaphore_post(&ctx->handler_lock);
return OMX_ErrorNone;
}
static void setupInputPort(ComponentContext *ctx) {
char uri[] = "/home/pi/Developer/OMXPlayground/36903_9_1.jpg";
size_t size = sizeof(OMX_PARAM_CONTENTURITYPE) + strlen(uri);
OMX_ERRORTYPE omxErr = OMX_ErrorNone;
OMX_PARAM_CONTENTURITYPE *contentURI = malloc(size);
OMX_INIT_STRUCTURE_P(contentURI, size);
omxErr = OMX_GetParameter(ctx->handle, OMX_IndexParamContentURI, contentURI);
omxAssert(omxErr);
strncpy(contentURI->contentURI, uri, strlen(uri));
omxErr = OMX_SetParameter(ctx->handle, OMX_IndexParamContentURI, contentURI);
omxAssert(omxErr);
free(contentURI);
}
static void setupOutputPort(ComponentContext *ctx) {
OMX_ERRORTYPE omxErr = OMX_ErrorNone;
OMX_PARAM_PORTDEFINITIONTYPE portDefinition;
OMX_INIT_STRUCTURE(portDefinition);
portDefinition.nPortIndex = ctx->outputPortIndex;
omxErr = OMX_GetParameter(ctx->handle, OMX_IndexParamPortDefinition, &portDefinition);
omxAssert(omxErr);
omxEnablePort(ctx->handle, ctx->outputPortIndex, OMX_TRUE);
omxPrintPort(ctx->handle, ctx->outputPortIndex);
for (int i = 0; i < portDefinition.nBufferCountActual; i++) {
printf("portDefinition.nBufferSize: %d\n", portDefinition.nBufferSize);
omxErr = OMX_AllocateBuffer(ctx->handle, &ctx->outputBuffer[i], ctx->outputPortIndex, i, portDefinition.nBufferSize);
omxAssert(omxErr);
}
}
static void omxGetPorts(ComponentContext *ctx) {
OMX_ERRORTYPE omxErr = OMX_ErrorNone;
OMX_PORT_PARAM_TYPE ports;
OMX_INIT_STRUCTURE(ports);
omxErr = OMX_GetParameter(ctx->handle, OMX_IndexParamImageInit, &ports);
omxAssert(omxErr);
const OMX_U32 pEnd = ports.nStartPortNumber + ports.nPorts;
for (OMX_U32 p = ports.nStartPortNumber; p < pEnd; p++) {
OMX_PARAM_PORTDEFINITIONTYPE portDefinition;
OMX_INIT_STRUCTURE(portDefinition);
portDefinition.nPortIndex = p;
omxErr = OMX_GetParameter(ctx->handle, OMX_IndexParamPortDefinition, &portDefinition);
omxAssert(omxErr);
//if (portDefinition.eDir == OMX_DirInput) {
// assert(ctx->inputPortIndex == 0);
// ctx->inputPortIndex = p;
//}
if (portDefinition.eDir == OMX_DirOutput) {
assert(ctx->outputPortIndex == 0);
ctx->outputPortIndex = p;
}
}
}
void omxImageRead() {
OMX_ERRORTYPE omxErr = OMX_ErrorNone;
OMX_STATETYPE omxState = OMX_StateInvalid;
VCOS_STATUS_T vcosErr = VCOS_SUCCESS;
bcm_host_init();
omxErr = OMX_Init();
omxAssert(omxErr);
ComponentContext ctx;
memset(&ctx, 0, sizeof(ctx));
vcosErr = vcos_semaphore_create(&ctx.handler_lock, "handler_lock", 1);
assert(vcosErr == VCOS_SUCCESS);
vcosErr = vcos_semaphore_create(&ctx.portChangeLock, "portChangeLock", 1);
assert(vcosErr == VCOS_SUCCESS);
OMX_STRING omxComponentName = "OMX.broadcom.image_read";
OMX_CALLBACKTYPE omxCallbacks;
omxCallbacks.EventHandler = omxEventHandler;
omxCallbacks.EmptyBufferDone = omxEmptyBufferDone;
omxCallbacks.FillBufferDone = omxFillBufferDone;
omxErr = OMX_GetHandle(&ctx.handle, omxComponentName, &ctx, &omxCallbacks);
omxAssert(omxErr);
omxAssertState(ctx.handle, OMX_StateLoaded);
omxGetPorts(&ctx);
omxEnablePort(ctx.handle, ctx.outputPortIndex, OMX_FALSE);
setupInputPort(&ctx);
puts("1");
omxSwitchToState(ctx.handle, OMX_StateIdle);
puts("2");
setupOutputPort(&ctx);
// setupOutputPort(&ctx);
// puts("4"); sleep(1);
// puts("5"); sleep(1);
// //prepareOutputPort(&ctx);
// omxSwitchToState(ctx.handle, OMX_StateExecuting);
// puts("6"); sleep(1);
//
// puts("wait for port change...");
// vcos_semaphore_wait(&ctx.portChangeLock);
// puts("continuing...");
//
// setupOutputPort(&ctx);
//
// puts("OMX_FillThisBuffer");
// omxErr = OMX_FillThisBuffer(ctx.handle, ctx.outputBuffer);
// omxAssert(omxErr);
//
//
//
//
sleep(10);
return;
int bufferIndex = 0;
ctx.outputReady = false;
FILE * output = fopen("out.data", "wb");
puts("OMX_FillThisBuffer");
omxErr = OMX_FillThisBuffer(ctx.handle, ctx.outputBuffer[bufferIndex]);
omxAssert(omxErr);
while (true) {
if (ctx.outputReady) {
ctx.outputReady = false;
puts("x");
// fwrite(ctx.outputBuffer->pBuffer + ctx.outputBuffer->nOffset, sizeof(uint8_t), ctx.outputBuffer->nFilledLen, output);
//
// printf("nFilledLen: %d\n", ctx.outputBuffer->nFilledLen);
// printf("nFlags: 0x%08x\n", ctx.outputBuffer->nFlags);
// if (ctx.outputBuffer->nFlags & OMX_BUFFERFLAG_EOS) {
// puts("received OMX_BUFFERFLAG_EOS");
// break;
// }
bufferIndex++;
bufferIndex %= 3;
puts("OMX_FillThisBuffer");
omxErr = OMX_FillThisBuffer(ctx.handle, ctx.outputBuffer[bufferIndex]);
omxAssert(omxErr);
}
}
fclose(output);
return;
}