-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcamera1.backup20181007.pas
762 lines (592 loc) · 22.5 KB
/
camera1.backup20181007.pas
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
unit Camera1;
interface
{$mode delphi}{$H+}
//{$define use_tftp} // if PI not connected to LAN and set for DHCP then remove this
uses
classes,sysutils,uIL_Client, uOMX, VC4, threads,
retromalina, mwindows,blitter;
(* based on hjimbens camera example
https://www.raspberrypi.org/forums/viewtopic.php?t=44852
pjde 2018 *)
const
kRendererInputPort = 90;
kClockOutputPort0 = 80;
kCameraCapturePort = 71; //71
kCameraClockPort = 73;
type TCameraThread=class (TThread)
private
protected
procedure Execute; override;
public
Constructor Create(CreateSuspended : boolean);
end;
const COMPONENT_CAMERA='OMX.broadcom.camera';
const COMPONENT_RENDER='OMX.broadcom.video_render';
type PContext=^context;
context=record
pcamera:OMX_HANDLETYPE;
prender:OMX_HANDLETYPE;
iscameraready:OMX_BOOL;
nwidth,nheight,nframerate:cardinal;
pBufferCameraOut:^OMX_BUFFERHEADERTYPE;
pSrcY,pSrcU,pSrcV:^OMX_U8;
nSizeY, nSizeU, nSizeV:cardinal;
pBufferPool:array[0..15] of ^OMX_BUFFERHEADERTYPE; // OMX_BUFFERHEADERTYPE**
nBufferPoolSize,nBufferPoolIndex:cardinal;
isFilled:OMX_BOOL;
end;
procedure camera_tunnel_not;
var camerathread:TCameraThread;
cmw:pointer=nil;
camerawindow:TWindow=nil;
mContext:context;
bc: PILCLIENT_BUFFER_CALLBACK_T;
implementation
procedure print_log(s:string);
begin
camerawindow.println(s);
end;
procedure bc1(userdata : pointer; comp : PCOMPONENT_T); cdecl;
begin
camerawindow.println('Callback called');
end;
constructor TCameraThread.create(CreateSuspended : boolean);
begin
FreeOnTerminate := True;
inherited Create(CreateSuspended);
end;
procedure TCameraThread.execute;
label p999;
begin
ThreadSetpriority(ThreadGetCurrent,6);
sleep(1);
if camerawindow=nil then
begin
camerawindow:=TWindow.create(480,3200,'Camera log');
camerawindow.decoration.hscroll:=true;
camerawindow.decoration.vscroll:=true;
camerawindow.resizable:=true;
camerawindow.cls(147);
camerawindow.tc:=154;
camerawindow.move(1200,64,480,1100,0,0);
cmw:=camerawindow;
end
else goto p999;
camera_tunnel_not;
repeat sleep(100) until camerawindow.needclose;
camerawindow.destroy;
camerawindow:=nil;
cmw:=nil;
p999:
end;
// /----------------------------------------------------------------------------------------------------------
// ============================================================================
// Name : camera_tunnel_non.c
// Author : SonienTaegi ( https://github.com/SonienTaegi/rpi-omx-tutorial )
// Version :
// Copyright : GPLv2
// Description : This is tutorial of OpenMAX to play currently captured video frames.
// Without tunneling, client handles any events of components and
// copy camera buffer to renderer buffer if it needs.
// Finally client requests empty buffer to renderer so renderer shows
// the captured frame on user screen.
//
// Without propriety communication, it costs large amount of CPU works.
// So it is slow and not so efficient. But this technique may need when
// modulate current captured buffer directly.
// ============================================================================
// */
// Event Handler : OMX Event */
// OMX_IN ???
function onOMXevent (
hComponent: OMX_HANDLETYPE;
pAppData:OMX_PTR;
eEvent: OMX_EVENTTYPE;
nData1: OMX_U32;
nData2: OMX_U32;
pEventData: OMX_PTR): OMX_ERRORTYPE; cdecl;
begin
print_log('Callback called '+inttostr(ndata1)+' '+inttostr(ndata2)) ;// hComponent, eEvent, nData1, nData2);
case eEvent of
OMX_EventParamOrConfigChanged :
begin
print_log('Param or config changed ');
if(nData2 = OMX_IndexParamCameraDeviceNumber) then
begin
Pcontext(pAppData)^.isCameraReady := OMX_TRUE;
camerawindow.println('Camera device is ready.');
end;
end;
end;
result:= OMX_ErrorNone;
end;
//* Callback : Camera-out buffer is filled */
function onFillCameraOut (
hComponent:OMX_HANDLETYPE;
pAppData: OMX_PTR;
pBuffer: POMX_BUFFERHEADERTYPE): OMX_ERRORTYPE; cdecl;
begin
mContext.isFilled := OMX_TRUE;
result:=OMX_ErrorNone;
end;
//* Callback : Render-in buffer is emptied */
function onEmptyRenderIn(
hComponent: OMX_HANDLETYPE;
pAppData: OMX_PTR;
PBuffer: POMX_BUFFERHEADERTYPE):OMX_ERRORTYPE ; cdecl;
begin
// camerawindow.println('buffer '+ inttohex(cardinal(pbuffer),8)+' emptied');
result:= OMX_ErrorNone;
end;
function isState(hcomponent:POMX_HANDLETYPE; state:OMX_STATETYPE): OMX_BOOL;
var currentState: OMX_STATETYPE;
begin
if (hComponent=nil) then result:=OMX_FALSE;
OMX_GetState(hComponent,@currentState);
if currentState=state then result:=OMX_TRUE else result:=OMX_FALSE;
end;
function wait_for_state_change(state_tobe: OMX_STATETYPE; handle:OMX_HANDLETYPE): OMX_BOOL;
var isValid:OMX_BOOL=OMX_TRUE;
timeout_counter:cardinal=0;
state_current:OMX_STATETYPE;
begin
camerawindow.println('Waiting for '+inttohex(cardinal(handle),8));
isValid := OMX_FALSE;
while (timeout_counter < 5000) and (isValid=OMX_FALSE) do
begin
timeout_counter:=0;
OMX_GetState(handle, @state_current);
if (state_current <> state_tobe) then begin threadsleep(1); inc(timeout_counter); end else IsValid:=OMX_TRUE;
end;
result:=IsValid;
end;
procedure terminate;
var state:OMX_STATETYPE;
bWaitForCamera, bWaitForRender: OMX_BOOL;
begin
camerawindow.println('On terminating...');
bWaitForCamera := OMX_FALSE;
bWaitForRender := OMX_FALSE;
if(isState(mContext.pCamera, OMX_StateExecuting))=OMX_TRUE then
begin
OMX_SendCommand(mContext.pCamera, OMX_CommandStateSet, OMX_StateIdle, nil);
bWaitForCamera := OMX_TRUE;
end;
if(isState(mContext.pRender, OMX_StateExecuting))=OMX_TRUE then
begin
OMX_SendCommand(mContext.pRender, OMX_CommandStateSet, OMX_StateIdle, nil);
bWaitForRender := OMX_TRUE;
end;
if(bWaitForCamera=OMX_TRUE) then wait_for_state_change(OMX_StateIdle, mContext.pCamera);
if(bWaitForRender=OMX_TRUE) then wait_for_state_change(OMX_StateIdle, mContext.pRender);
// Idle -> Loaded
bWaitForCamera := OMX_FALSE;
bWaitForRender := OMX_FALSE;
if(isState(mContext.pCamera, OMX_StateIdle)) =OMX_TRUE then
begin
OMX_SendCommand(mContext.pCamera, OMX_CommandStateSet, OMX_StateLoaded, nil);
bWaitForCamera := OMX_TRUE;
end;
if(isState(mContext.pRender, OMX_StateIdle)) then
begin
OMX_SendCommand(mContext.pRender, OMX_CommandStateSet, OMX_StateLoaded, nil);
bWaitForRender := OMX_TRUE;
end;
if(bWaitForCamera=OMX_TRUE) then wait_for_state_change(OMX_StateLoaded, mContext.pCamera);
if(bWaitForRender=OMX_TRUE) then wait_for_state_change(OMX_StateLoaded, mContext.pRender);
// Loaded -> Free
if(isState(mContext.pCamera, OMX_StateLoaded))=OMX_TRUE then OMX_FreeHandle(mContext.pCamera);
if(isState(mContext.pRender, OMX_StateLoaded))=OMX_TRUE then OMX_FreeHandle(mContext.pRender);
OMX_Deinit();
camerawindow.println('Press enter to terminate.');
while keypressed do readkey;
repeat until keypressed;
end;
procedure componentLoad(pCallbackOMX:POMX_CALLBACKTYPE);
var err:OMX_ERRORTYPE;
begin
// Loading component
//print_log('Load ' +COMPONENT_CAMERA);
err := OMX_GetHandle(@(mContext.pCamera), COMPONENT_CAMERA, @mContext, pCallbackOMX);
if (err <> OMX_ErrorNone ) then
begin
print_log('error loading camera'+inttostr(err));
terminate();
//exit(-1);
end;
//mcontext.pcamera:=pointer(cardinal(mcontext.pcamera) and $3FFFFFFF);
print_log('Handler address: camera '+inttohex(cardinal(mContext.pCamera),8));
print_log('Load ' +COMPONENT_RENDER);
err := OMX_GetHandle(@(mContext.pRender), COMPONENT_RENDER, @mContext, pCallbackOMX);
if (err <> OMX_ErrorNone ) then
begin
print_log('error loading renderer'+inttostr(err));
terminate();
//exit(-1);
end;
//mcontext.prender:=pointer(cardinal(mcontext.prender) and $3FFFFFFF);
print_log('Handler address: renderer '+inttohex(cardinal(mContext.pRender),8));
end;
procedure componentConfigure;
var err: OMX_ERRORTYPE;
portDef: OMX_PARAM_PORTDEFINITIONTYPE;
formatVideo:POMX_VIDEO_PORTDEFINITIONTYPE;
configCameraCallback:OMX_CONFIG_REQUESTCALLBACKTYPE;
deviceNumber:OMX_PARAM_U32TYPE;
displayRegion:OMX_CONFIG_DISPLAYREGIONTYPE;
begin
// Disable any unused ports
OMX_SendCommand(mContext.pCamera, OMX_CommandPortDisable, 70, nil);
OMX_SendCommand(mContext.pCamera, OMX_CommandPortDisable, 72, nil);
OMX_SendCommand(mContext.pCamera, OMX_CommandPortDisable, 73, nil);
// Configure OMX_IndexParamCameraDeviceNumber callback enable to ensure whether camera is initialized properly.
print_log('Configure DeviceNumber callback enable.');
FillChar (configCameraCallback, SizeOf(configCameraCallback), 0);
configCameraCallback.nSize := sizeof(configCameraCallback);
configCameraCallback.nVersion.nVersion := OMX_VERSION;
configCameraCallback.nVersion.nVersionMajor := OMX_VERSION_MAJOR;
configCameraCallback.nVersion.nVersionMinor := OMX_VERSION_MINOR;
configCameraCallback.nVersion.nRevision := OMX_VERSION_REVISION;
configCameraCallback.nVersion.nStep := OMX_VERSION_STEP;
configCameraCallback.nPortIndex := OMX_ALL; // Must Be OMX_ALL
configCameraCallback.nIndex := OMX_IndexParamCameraDeviceNumber;
configCameraCallback.bEnable := OMX_TRUE;
err := OMX_SetConfig(mContext.pCamera, OMX_IndexConfigRequestCallback, @configCameraCallback);
if err<> OMX_ErrorNone then
begin
print_log(inttostr(err)+ 'camera set config FAIL');
terminate();
//exit(-1);
end;
// OMX CameraDeviceNumber set -> will trigger Camera Ready callback
print_log('Set CameraDeviceNumber parameter.');
FillChar (deviceNumber, SizeOf(deviceNumber), 0);
deviceNumber.nSize := sizeof(deviceNumber);
deviceNumber.nVersion.nVersion := OMX_VERSION;
deviceNumber.nVersion.nVersionMajor := OMX_VERSION_MAJOR;
deviceNumber.nVersion.nVersionMinor := OMX_VERSION_MINOR;
deviceNumber.nVersion.nRevision := OMX_VERSION_REVISION;
deviceNumber.nVersion.nStep := OMX_VERSION_STEP;
deviceNumber.nPortIndex := OMX_ALL;
deviceNumber.nU32 := 0; // Mostly zero
err := OMX_SetParameter(mContext.pCamera, OMX_IndexParamCameraDeviceNumber, @deviceNumber);
if err<>OMX_ErrorNone then
begin
print_log(inttostr(err)+ 'camera set number FAIL');
terminate();
//exit(-1);
end;
// Set video format of #71 port.
print_log('Set video format of the camera : Using #71.');
FillChar (portDef, SizeOf(portDef), 0);
portDef.nSize := sizeof(portDef);
portDef.nVersion.nVersion := OMX_VERSION;
portDef.nVersion.nVersionMajor := OMX_VERSION_MAJOR;
portDef.nVersion.nVersionMinor := OMX_VERSION_MINOR;
portDef.nVersion.nRevision := OMX_VERSION_REVISION;
portDef.nVersion.nStep := OMX_VERSION_STEP;
portDef.nPortIndex := 71;
print_log('Get non-initialized definition of #71.');
OMX_GetParameter(mContext.pCamera, OMX_IndexParamPortDefinition, @portDef);
print_log('Set up parameters of video format of #71.');
formatVideo := @portDef.format.video;
formatVideo^.eColorFormat := OMX_COLOR_FormatYUV420PackedPlanar;
formatVideo^.nFrameWidth := mContext.nWidth;
formatVideo^.nFrameHeight := mContext.nHeight;
formatVideo^.xFramerate := mContext.nFramerate shl 16; // Fixed point. 1
formatVideo^.nStride := formatVideo^.nFrameWidth; // Stride 0 -> Raise segment fault.
formatVideo^.nSliceHeight:=768;
err := OMX_SetParameter(mContext.pCamera, OMX_IndexParamPortDefinition, @portDef);
if err<>OMX_ErrorNone then
begin
print_log(inttostr(err)+ 'camera set format FAIL');
terminate();
//exit(-1);
end;
OMX_GetParameter(mContext.pCamera, OMX_IndexParamPortDefinition, @portDef);
formatVideo := @portDef.format.video;
mContext.nSizeY := formatVideo^.nFrameWidth * formatVideo^.nSliceHeight;
mContext.nSizeU := mContext.nSizeY div 4;
mContext.nSizeV := mContext.nSizeY div 4;
print_log(inttostr(mContext.nSizeY)+' '+inttostr(mContext.nSizeU)+' '+inttostr(mContext.nSizeV));
// Set video format of #90 port.
print_log('Set video format of the render : Using #90.');
FillChar (portDef, SizeOf(portDef), 0);
portDef.nSize := sizeof(portDef);
portDef.nVersion.nVersion := OMX_VERSION;
portDef.nVersion.nVersionMajor := OMX_VERSION_MAJOR;
portDef.nVersion.nVersionMinor := OMX_VERSION_MINOR;
portDef.nVersion.nRevision := OMX_VERSION_REVISION;
portDef.nVersion.nStep := OMX_VERSION_STEP;
portDef.nPortIndex := 90;
print_log('Get default definition of #90.');
OMX_GetParameter(mContext.pRender, OMX_IndexParamPortDefinition, @portDef);
print_log('Set up parameters of video format of #90.');
formatVideo := @portDef.format.video;
formatVideo^.eColorFormat := OMX_COLOR_FormatYUV420PackedPlanar;
formatVideo^.eCompressionFormat := OMX_VIDEO_CodingUnused;
formatVideo^.nFrameWidth := mContext.nWidth;
formatVideo^.nFrameHeight := mContext.nHeight;
formatVideo^.nStride := mContext.nWidth;
formatVideo^.nSliceHeight := mContext.nHeight;
formatVideo^.xFramerate := mContext.nFramerate shl 16;
err := OMX_SetParameter(mContext.pRender, OMX_IndexParamPortDefinition, @portDef);
if err<>OMX_ErrorNone then
begin
print_log(inttostr(err)+ 'renderer set format FAIL');
terminate();
//exit(-1);
end;
// Configure rendering region
FillChar (displayRegion, SizeOf(displayRegion), 0);
displayRegion.nSize := sizeof(displayRegion);
displayRegion.nVersion.nVersion := OMX_VERSION;
displayRegion.nVersion.nVersionMajor := OMX_VERSION_MAJOR;
displayRegion.nVersion.nVersionMinor := OMX_VERSION_MINOR;
displayRegion.nVersion.nRevision := OMX_VERSION_REVISION;
displayRegion.nVersion.nStep := OMX_VERSION_STEP;
displayRegion.nPortIndex := 90;
displayRegion.dest_rect.width := mContext.nWidth;
displayRegion.dest_rect.height := mContext.nHeight;
displayRegion.set_ := OMX_DISPLAY_SET_NUM or OMX_DISPLAY_SET_FULLSCREEN or OMX_DISPLAY_SET_MODE or OMX_DISPLAY_SET_DEST_RECT;
displayRegion.mode := OMX_DISPLAY_MODE_FILL;
displayRegion.fullscreen := OMX_FALSE;
displayRegion.num := 0;
err := OMX_SetConfig(mContext.pRender, OMX_IndexConfigDisplayRegion, @displayRegion);
if err<>OMX_ErrorNone then
begin
print_log(inttostr(err)+ 'renderer set region FAIL');
terminate();
//exit(-1);
end;
// Wait up for camera being ready.
print_log('Waiting until camera device is ready');
//while (mContext.isCameraReady=OMX_FALSE) do
// begin
//
// end;
sleep(2000);
print_log('Camera is ready');
end;
procedure componentPrepare;
var err:OMX_ERRORTYPE;
portDef:OMX_PARAM_PORTDEFINITIONTYPE;
i:cardinal;
begin
// Request state of components to be IDLE.
// The command will turn the component into waiting mode.
// After allocating buffer to all enabled ports than the component will be IDLE.
print_log('STATE : CAMERA - IDLE request');
err := OMX_SendCommand(mContext.pCamera, OMX_CommandStateSet, OMX_StateIdle, nil);
if err<>OMX_ErrorNone then
begin
print_log(inttostr(err)+ 'camera idle request FAIL');
terminate();
//exit(-1);
end;
print_log('STATE : RENDER - IDLE request');
err := OMX_SendCommand(mContext.pRender, OMX_CommandStateSet, OMX_StateIdle, nil);
if err<>OMX_ErrorNone then
begin
print_log(inttostr(err)+ 'renderer idle request FAIL');
terminate();
//exit(-1);
end;
// Allocate buffers to render
print_log('Allocate buffer to renderer #90 for input.');
FillChar (portDef, SizeOf(portDef), 0);
portDef.nSize := sizeof(portDef);
portDef.nVersion.nVersion := OMX_VERSION;
portDef.nVersion.nVersionMajor := OMX_VERSION_MAJOR;
portDef.nVersion.nVersionMinor := OMX_VERSION_MINOR;
portDef.nVersion.nRevision := OMX_VERSION_REVISION;
portDef.nVersion.nStep := OMX_VERSION_STEP;
portDef.nPortIndex := 90;
OMX_GetParameter(mContext.pRender, OMX_IndexParamPortDefinition, @portDef);
print_log('Size of render predefined buffer :'+inttostr(portDef.nBufferSize)+' '+inttostr(portDef.nBufferCountActual));
mContext.nBufferPoolSize := portDef.nBufferCountActual;
mContext.nBufferPoolIndex := 0;
//mContext.pBufferPool := getmem(sizeof(pointer) * mContext.nBufferPoolSize); // OMX_BUFFERHEADERTYPE* - pointer?
for i:=0 to mContext.nBufferPoolSize-1 do
begin
mContext.pBufferPool[i] := nil;
err := OMX_AllocateBuffer(mContext.pRender, @(mContext.pBufferPool[i]), 90, @mContext, portDef.nBufferSize);
if err<>OMX_ErrorNone then
begin
print_log(inttostr(err)+ 'allocate render buffer FAIL');
terminate();
//exit(-1);
end;
end;
// Allocate buffer to camera
FillChar (portDef, SizeOf(portDef), 0);
portDef.nSize := sizeof(portDef);
portDef.nVersion.nVersion := OMX_VERSION;
portDef.nVersion.nVersionMajor := OMX_VERSION_MAJOR;
portDef.nVersion.nVersionMinor := OMX_VERSION_MINOR;
portDef.nVersion.nRevision := OMX_VERSION_REVISION;
portDef.nVersion.nStep := OMX_VERSION_STEP;
portDef.nPortIndex := 71;
OMX_GetParameter(mContext.pCamera, OMX_IndexParamPortDefinition, @portDef);
print_log('Size of camera predefined buffer :'+inttostr(portDef.nBufferSize)+' '+inttostr(portDef.nBufferCountActual));
OMX_AllocateBuffer(mContext.pCamera, @mContext.pBufferCameraOut, 71, @mContext, portDef.nBufferSize);
if err<>OMX_ErrorNone then
begin
print_log(inttostr(err)+ 'allocate camera buffer FAIL');
terminate();
//exit(-1);
end;
mContext.pSrcY := mContext.pBufferCameraOut^.pBuffer;
mContext.pSrcU := pointer(cardinal(mContext.pSrcY) + mContext.nSizeY);
mContext.pSrcV := pointer(cardinal(mContext.pSrcU) + mContext.nSizeU);
print_log(inttohex(cardinal(mContext.pSrcY),8)+' '+inttohex(cardinal(mContext.pSrcU),8)+' '+inttohex(cardinal(mContext.pSrcV),8));
for i:=0 to 16 do removeramlimits(cardinal(mContext.pSrcY)+i*4096);
// Wait up for component being idle.
if wait_for_state_change(OMX_StateIdle, mContext.pRender)=OMX_FALSE then
begin
print_log('FAIL waiting for idle state');
terminate();
// exit(-1);
end;
if wait_for_state_change(OMX_StateIdle, mContext.pCamera)=OMX_FALSE then
begin
print_log('FAIL waiting for idle state');
terminate();
// exit(-1);
end;
print_log('STATE : IDLE OK!');
end;
// -- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
procedure camera_tunnel_not;
label p999;
///* Temporary variables */
var err:OMX_ERRORTYPE;
portdef:OMX_PARAM_PORTDEFINITIONTYPE;
callbackOMX:OMX_CALLBACKTYPE;
portCapturing:OMX_CONFIG_PORTBOOLEANTYPE;
py:POMX_U8=nil;
pu:POMX_U8=nil;
pv:POMX_U8=nil;
py2:POMX_U8=nil;
pu2:POMX_U8=nil;
pv2:POMX_U8=nil;
nOffsetU, nOffsetV, nFrameMax, nFrames:cardinal;
PBuffer: POMX_BUFFERHEADERTYPE;
i:integer;
qqq:int64;
begin
//* Initialize application variables */
FillChar (mContext, SizeOf(mContext), 0);
mContext.nWidth := 1024;
mContext.nHeight := 768;
mContext.nFramerate := 25;
// RPI initialize.
bcmhostinit;
// OMX initialize.
print_log('Initialize OMX');
err := OMX_Init;
if err<> OMX_ErrorNone then
begin
print_log(inttostr(err)+' OMX init FAIL');
OMX_Deinit();
goto p999;
end;
// For loading component, Callback shall provide.
callbackOMX.EventHandler := onOMXevent;
callbackOMX.EmptyBufferDone := onEmptyRenderIn;
callbackOMX.FillBufferDone := onFillCameraOut;
componentLoad(@callbackOMX);
componentConfigure();
componentPrepare();
// Request state of component to be EXECUTE.
print_log('STATE : CAMERA - EXECUTING request');
err := OMX_SendCommand(mContext.pCamera, OMX_CommandStateSet, OMX_StateExecuting, nil);
if err<>OMX_ErrorNone then
begin
print_log(inttostr(err)+' Camera executing FAIL');
goto p999;
end;
print_log('STATE : RENDER - EXECUTING request');
err := OMX_SendCommand(mContext.pRender, OMX_CommandStateSet, OMX_StateExecuting, nil);
if err<>OMX_ErrorNone then
begin
print_log(inttostr(err)+' Renderer executing FAIL');
goto p999;
end;
if wait_for_state_change(OMX_StateExecuting, mContext.pCamera)=OMX_FALSE then
begin
print_log(inttostr(err)+' Camera wait for executing FAIL');
goto p999;
end;
if wait_for_state_change(OMX_StateExecuting, mContext.pRender)=OMX_FALSE then
begin
print_log(inttostr(err)+' Renderer wait for executing FAIL');
goto p999;
end;
print_log('STATE : EXECUTING OK!');
// Since #71 is capturing port, needs capture signal like other handy capture devices
print_log('Capture start.');
FillChar (portCapturing, SizeOf(portCapturing), 0);
portCapturing.nSize := sizeof(portCapturing);
portCapturing.nVersion.nVersion := OMX_VERSION;
portCapturing.nVersion.nVersionMajor := OMX_VERSION_MAJOR;
portCapturing.nVersion.nVersionMinor := OMX_VERSION_MINOR;
portCapturing.nVersion.nRevision := OMX_VERSION_REVISION;
portCapturing.nVersion.nStep := OMX_VERSION_STEP;
portCapturing.nPortIndex := 71;
portCapturing.bEnabled := OMX_TRUE;
OMX_SetConfig(mContext.pCamera, OMX_IndexConfigPortCapturing, @portCapturing);
pY := nil;
pU := nil;
pV := nil;
nOffsetU := mContext.nWidth * mContext.nHeight;
nOffsetV := (nOffsetU * 5) div 4;
nFrameMax:= mContext.nFramerate * 60;
nFrames := 0;
print_log('Capture for '+inttostr(nFramemax)+' frames.');
OMX_FillThisBuffer(mContext.pCamera, mContext.pBufferCameraOut);
while(nFrames < nFrameMax) do
begin
if (mContext.isFilled) then
begin
pBuffer := mContext.pBufferPool[mContext.nBufferPoolIndex];
if(pBuffer^.nFilledLen = 0) then
begin
pY := pBuffer^.pBuffer;
pU := pointer(cardinal(pY) + nOffsetU);
pV := pointer(cardinal(pY) + nOffsetV);
end;
py:=pointer(cardinal(py));// and $3FFFFFFF);
pu:=pointer(cardinal(pu));// and $3FFFFFFF);
pv:=pointer(cardinal(pv));//; and $3FFFFFFF);
py2:=pointer(cardinal(mContext.pSrcY));//; and $3FFFFFFF);
pu2:=pointer(cardinal(mContext.pSrcU));// and $3FFFFFFF);
pv2:=pointer(cardinal(mContext.pSrcV));// and $3FFFFFFF);
// print_log('nsizey is '+inttostr(mcontext.nsizey));
fastmove(integer(pY2), integer(py), mContext.nSizeY); pY := pointer(cardinal(py)+mContext.nSizeY);
fastmove(integer(pU2), integer(pu), mContext.nSizeU); pU := pointer(cardinal(pu)+mContext.nSizeU);
fastmove(integer(pV2), integer(pv), mContext.nSizeV); pV := pointer(cardinal(pv)+mContext.nSizeV);
pBuffer^.nFilledLen += mContext.pBufferCameraOut^.nFilledLen;
if (mContext.pBufferCameraOut^.nFlags and OMX_BUFFERFLAG_ENDOFFRAME)<>0 then
begin
print_log('BUFFER '+inttohex(cardinal(pbuffer),8)+' filled');
OMX_EmptyThisBuffer(mContext.pRender, pBuffer);
mContext.nBufferPoolIndex+=1;
if(mContext.nBufferPoolIndex = mContext.nBufferPoolSize) then mContext.nBufferPoolIndex := 0;
nFrames+=1;
end;
mContext.isFilled := OMX_FALSE;
qqq:=gettime;
OMX_FillThisBuffer(mContext.pCamera, mContext.pBufferCameraOut);
qqq:=gettime-qqq;
print_log('time is '+inttostr(qqq));
end;
threadsleep(1);
end;
portCapturing.bEnabled := OMX_FALSE;
OMX_SetConfig(mContext.pCamera, OMX_IndexConfigPortCapturing, @portCapturing);
print_log('Capture stop.');
p999:
terminate;
end;
end.