-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFlashCam.cpp
1741 lines (1446 loc) · 68.7 KB
/
FlashCam.cpp
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************
Software developed by Hessel van der Molen
Main author Hessel van der Molen (hmolen.science at gmail dot com)
This software is released under BSD license as expressed below
-------------------------------------------------------------------
Copyright (c) 2017, Hessel van der Molen
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by Hessel van der Molen
4. None of the names of the author or irs contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY Hessel van der Molen ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL AVA BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
****************************************************************/
#include "FlashCam.h"
#include "FlashCam_util_mmal.h"
#include "bcm_host.h"
#include "interface/mmal/util/mmal_util.h"
#include "interface/mmal/util/mmal_util_params.h"
#include "interface/mmal/util/mmal_default_components.h"
#include <stdio.h>
#include <sysexits.h>
/*
* Constructor
*/
FlashCam::FlashCam(){
clear();
}
/*
* Destructor
*/
FlashCam::~FlashCam(){
//allow callback/buffer data to be emptied..
sleep(1);
//cleanup.
destroyComponents();
FlashCamPLL::destroy();
vcos_semaphore_delete(&_userdata.sem_capture);
}
void FlashCam::clear() {
if (_active) {
fprintf(stderr, "%s: Cannot clear FlashCam while it is capturing.\n", __func__);
return;
}
if (_initialised) {
//allow callback/buffer data to be emptied..
sleep(1);
//clear old values
destroyComponents();
FlashCamPLL::destroy();
vcos_semaphore_delete(&_userdata.sem_capture);
}
_initialised = false;
_active = false;
FlashCamPLL::init(&_state);
//init with default parameter set;
getDefaultSettings(&_settings);
//disable verbose as it might confuse user.
unsigned int v = _settings.verbose;
_settings.verbose = 0;
//init with default settings
setSettings(&_settings); //implicitly resets/configures camera
//reset verbose
_settings.verbose = v;
}
int FlashCam::resetCamera() {
int status;
if (_active) {
fprintf(stderr, "%s: Cannot reset camera while it is capturing.\n", __func__);
return FlashCamMMAL::mmal_to_int(MMAL_EINVAL);
}
if (_settings.verbose)
fprintf(stdout, "%s: (re)setting/initializing components.\n", __func__);
// setup / reset camera (return upon error)
if (status = setupComponents())
return status;
if (_settings.verbose)
fprintf(stdout, "%s: (re)setting/initializing mode.\n", __func__);
// enable / reset selected mode
if (status = setSettingCaptureMode( _settings.mode ))
return status;
// update parameters
if (status = setParams(&_params))
return status;
if (_settings.verbose)
fprintf(stdout, "%s: Succes.\n", __func__);
//succes
return FlashCamMMAL::mmal_to_int(MMAL_SUCCESS);
}
// setup the camera with specified settings
int FlashCam::setupComponents() {
// Is camera active?
if (_active) {
fprintf(stderr, "%s: Cannot reset camera while it is capturing.\n", __func__);
return FlashCamMMAL::mmal_to_int(MMAL_EINVAL);
}
// Only update these settings when initialising for the first time
if (!_initialised) {
//print that we are starting
if (_settings.verbose)
fprintf(stdout, "\n FlashCam Version: %s\n\n", FLASHCAM_VERSION_STRING);
bcm_host_init();
//register to VCOS for logging
vcos_log_register("FlashCam", VCOS_LOG_CATEGORY);
// create & init semaphore
if (vcos_semaphore_create(&_userdata.sem_capture, "FlashCam_sem_captured", 0) != VCOS_SUCCESS) {
vcos_log_error("%s: Failed to create semaphore", __func__);
return FlashCamMMAL::mmal_to_int(MMAL_EINVAL);
}
//setup default camera params
getDefaultParams(&_params);
}
//clear current setup
destroyComponents();
// init/reset private variables
//_initialised : set at the end of this function.
//_active = false : initialised by constructor. Set when camera is recording.
//_params = default : initialised by constructor. Updated by User.
//_settings : should be set before calling setupComponents().
//_camera_component : (re)set by destroyComponents()
//_preview_component : (re)set by destroyComponents()
//_preview_connection : (re)set by destroyComponents()
//_camera_pool : (re)set by destroyComponents()
//_framebuffer : (re)set by destroyComponents()
//_opengl_queue : (re)set by destroyComponents()
//set userdata
_userdata.params = &_params;
_userdata.settings = &_settings;
_userdata.camera_pool = NULL;
_userdata.framebuffer = NULL;
_userdata.framebuffer_size = 0;
_userdata.framebuffer_idx = 0;
_userdata.callback = NULL;
#ifdef BUILD_FLASHCAM_WITH_OPENGL
_userdata.callback_egl = NULL;
_userdata.opengl_queue = NULL;
FlashCamOpenGL::init(&_state);
#endif
//Status flags
MMAL_STATUS_T status = MMAL_SUCCESS;
//setup camera
// - internally sets:
// _camera_component
// _camera_pool
// _userdata.camera_pool
// _userdata.framebuffer
// _userdata.framebuffer_size
if ((status = setupComponentCamera()) != MMAL_SUCCESS) {
vcos_log_error("%s: Failed to create camera component", __func__);
destroyComponents();
return FlashCamMMAL::mmal_to_int(status);
}
//setup preview (nullsink!)
// -> But we need it as it is used for measurements such as awb
// internally sets:
// _preview_component
if ((status = setupComponentPreview()) != MMAL_SUCCESS) {
vcos_log_error("%s: Failed to create preview component", __func__);
destroyComponents();
return FlashCamMMAL::mmal_to_int(status);
}
if (_settings.verbose)
fprintf(stdout, "%s: Starting component connection stage\n", __func__);
//get ports
MMAL_PORT_T *preview_port = _camera_component->output[MMAL_CAMERA_PREVIEW_PORT];
MMAL_PORT_T *preview_port_input = _preview_component->input[0];
// Connect preview
if ((status = connectPorts(preview_port, preview_port_input, &_preview_connection)) != MMAL_SUCCESS) {
vcos_log_error("%s: Failed to connect preview port", __func__);
destroyComponents();
return FlashCamMMAL::mmal_to_int(status);
}
if (_settings.verbose)
fprintf(stdout, "%s: Finished setup\n", __func__);
_initialised = true;
return EX_OK;
}
//Camera setup function
MMAL_STATUS_T FlashCam::setupComponentCamera() {
MMAL_STATUS_T status;
// Create Component
if ((status = mmal_component_create(MMAL_COMPONENT_DEFAULT_CAMERA, &_camera_component)) != MMAL_SUCCESS) {
vcos_log_error("%s: Failed to create camera component", __func__);
destroyComponents();
return status;
}
// Select Camera
if ( setCameraNum(_params.cameranum) ) {
vcos_log_error("%s: Could not select camera %d", __func__, _params.cameranum);
destroyComponents();
return MMAL_EINVAL;
}
// Validate outputs are available
if (!_camera_component->output_num){
vcos_log_error("%s: Camera doesn't have output ports", __func__);
destroyComponents();
return MMAL_EINVAL;
}
// Do we want setting-update messages?
if (setChangeEventRequest(MMAL_PARAMETER_CAMERA_SETTINGS, _settings.update)) {
vcos_log_error("%s: No camera settings events", __func__);
}
// Enable the camera, and tell it its control callback function
if ((status = mmal_port_enable(_camera_component->control, FlashCam::control_callback)) != MMAL_SUCCESS ) {
vcos_log_error("%s: Unable to enable control port (%u)", __func__, status);
destroyComponents();
return status;
}
// Set sensormode
if ( setSensorMode(_settings.sensormode) ) {
vcos_log_error("%s: Could not set sensormode %d", __func__, _settings.sensormode);
destroyComponents();
return MMAL_EINVAL;
}
// Setup ports
MMAL_PORT_T *preview_port = _camera_component->output[MMAL_CAMERA_PREVIEW_PORT];
MMAL_PORT_T *video_port = _camera_component->output[MMAL_CAMERA_VIDEO_PORT ];
MMAL_PORT_T *capture_port = _camera_component->output[MMAL_CAMERA_CAPTURE_PORT];
// Align height/width
_settings.width = VCOS_ALIGN_UP(_settings.width , 32);
_settings.height = VCOS_ALIGN_UP(_settings.height, 16);
if (_settings.verbose)
fprintf(stdout, "%s: Aligned image size: %d x %d (w x h) \n" , __func__, _settings.width , _settings.height);
// setup the camera configuration
MMAL_PARAMETER_CAMERA_CONFIG_T cam_config =
{
{ MMAL_PARAMETER_CAMERA_CONFIG, sizeof(cam_config) },
.max_stills_w = _settings.width,
.max_stills_h = _settings.height,
.stills_yuv422 = 0,
.one_shot_stills = 1,
.max_preview_video_w = _settings.width,
.max_preview_video_h = _settings.height,
.num_preview_video_frames = 3,
.stills_capture_circular_buffer_height = 0,
.fast_preview_resume = 0,
.use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RAW_STC
};
if ( setCameraConfig( &cam_config ) ) {
vcos_log_error("%s: Could not setup camera", __func__);
destroyComponents();
return MMAL_EINVAL;
}
// setup port-formats
MMAL_ES_FORMAT_T *format = preview_port->format;
//Preview format
format->encoding = MMAL_ENCODING_OPAQUE;
format->encoding_variant = MMAL_ENCODING_I420;
format->es->video.width = _settings.width;
format->es->video.height = _settings.height;
format->es->video.crop.x = 0;
format->es->video.crop.y = 0;
format->es->video.crop.width = _settings.width;
format->es->video.crop.height = _settings.height;
format->es->video.frame_rate.num = PREVIEW_FRAME_RATE_NUM;
format->es->video.frame_rate.den = PREVIEW_FRAME_RATE_DEN;
//Update preview-port with set format
if ((status = mmal_port_format_commit(preview_port)) != MMAL_SUCCESS ) {
vcos_log_error("%s: Preview format couldn't be set (%u)", __func__, status);
destroyComponents();
return status;
}
//Video format ==> same as Preview, except for encoding (YUV!)
format = video_port->format;
if (_settings.opengl_enabled) {
//For openGL we require the OPAQUE (= special GPU format) encoding.
format->encoding = MMAL_ENCODING_OPAQUE;
} else {
format->encoding = MMAL_ENCODING_I420;
}
format->encoding_variant = MMAL_ENCODING_I420;
format->es->video.width = _settings.width;
format->es->video.height = _settings.height;
format->es->video.crop.x = 0;
format->es->video.crop.y = 0;
format->es->video.crop.width = _settings.width;
format->es->video.crop.height = _settings.height;
format->es->video.frame_rate.num = VIDEO_FRAME_RATE_NUM;
format->es->video.frame_rate.den = VIDEO_FRAME_RATE_DEN;
#ifdef BUILD_FLASHCAM_WITH_OPENGL
/* Enable ZERO_COPY mode on the preview port which instructs MMAL to only
* pass the 4-byte opaque buffer handle instead of the contents of the opaque
* buffer.
* The opaque handle is resolved on VideoCore by the GL driver when the EGL
* image is created.
*/
if (_settings.opengl_enabled) {
if ((status = mmal_port_parameter_set_boolean(video_port, MMAL_PARAMETER_ZERO_COPY, MMAL_TRUE)) != MMAL_SUCCESS ) {
vcos_log_error("%s: Failed to enable zero copy on video port (%u)", __func__, status);
destroyComponents();
return status;
}
}
#endif
//Update capture-port with set format
if ((status = mmal_port_format_commit(video_port)) != MMAL_SUCCESS ) {
vcos_log_error("%s: Video format couldn't be set (%u)", __func__, status);
destroyComponents();
return status;
}
//Capture format ==> same as Preview, except for encoding (YUV!)
format = capture_port->format;
format->encoding = MMAL_ENCODING_I420;
format->encoding_variant = MMAL_ENCODING_I420;
format->es->video.width = _settings.width;
format->es->video.height = _settings.height;
format->es->video.crop.x = 0;
format->es->video.crop.y = 0;
format->es->video.crop.width = _settings.width;
format->es->video.crop.height = _settings.height;
format->es->video.frame_rate.num = CAPTURE_FRAME_RATE_NUM;
format->es->video.frame_rate.den = CAPTURE_FRAME_RATE_DEN;
//Update capture-port with set format
if ((status = mmal_port_format_commit(capture_port)) != MMAL_SUCCESS ) {
vcos_log_error("%s: Capture format couldn't be set (%u)", __func__, status);
destroyComponents();
return status;
}
//Enable camera
if ((status = mmal_component_enable(_camera_component)) != MMAL_SUCCESS ) {
vcos_log_error("%s: Camera component couldn't be enabled (%u)", __func__, status);
destroyComponents();
return status;
}
// YUV-data framesize
_userdata.framebuffer_size = VCOS_ALIGN_UP(_settings.width * _settings.height * 1.5, 32);
//create buffer for image
_framebuffer = new unsigned char[_userdata.framebuffer_size];
if (!_framebuffer) {
vcos_log_error("%s: Failed to allocate image buffer", __func__);
destroyComponents();
return MMAL_ENOMEM;
}
//update userdata with framebuffer
_userdata.framebuffer = _framebuffer;
if (_settings.verbose)
fprintf(stdout, "%s: Success.\n", __func__);
return MMAL_SUCCESS;
}
//Preview setup functions
MMAL_STATUS_T FlashCam::setupComponentPreview() {
MMAL_STATUS_T status;
//set nullsink
if ((status = mmal_component_create("vc.null_sink", &_preview_component)) != MMAL_SUCCESS) {
vcos_log_error("%s: Unable to create null sink component (%u)", __func__, status);
destroyComponents();
return status;
}
// Enable component
if ((status = mmal_component_enable( _preview_component )) != MMAL_SUCCESS) {
vcos_log_error("%s: Unable to enable preview/null sink component (%u)", __func__, status);
destroyComponents();
return status;
}
return MMAL_SUCCESS;
}
void FlashCam::destroyComponents() {
if (_settings.verbose)
fprintf(stdout, "%s: Clearing components\n", __func__);
//reset init flag
_initialised = false;
// Disable connections
if (_preview_connection) {
mmal_connection_destroy(_preview_connection);
}
// Disable ports
if (_preview_component) {
mmal_component_disable( _preview_component );
mmal_component_destroy( _preview_component );
}
if (_camera_component) {
mmal_component_disable( _camera_component );
mmal_component_destroy( _camera_component );
}
// Disable pool
if (_camera_pool) {
mmal_pool_destroy( _camera_pool );
}
// Clear buffer
if (_framebuffer)
delete[] _framebuffer;
#ifdef BUILD_FLASHCAM_WITH_OPENGL
if (_opengl_queue) {
mmal_queue_destroy( _opengl_queue );
_opengl_queue = NULL;
}
FlashCamOpenGL::destroy();
#endif
// Reset pointers
_preview_connection = NULL;
_preview_component = NULL;
_camera_component = NULL;
_camera_pool = NULL;
_framebuffer = NULL;
if (_settings.verbose)
fprintf(stdout, "%s: Components cleared\n", __func__);
}
/*
* void FlashCam::control_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
* Callback function for control-events
*/
void FlashCam::control_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer) {
//fprintf(stderr, "Camera control callback cmd=0x%08x", buffer->cmd);
//check received event
if (buffer->cmd == MMAL_EVENT_PARAMETER_CHANGED) {
MMAL_EVENT_PARAMETER_CHANGED_T *param = (MMAL_EVENT_PARAMETER_CHANGED_T *)buffer->data;
switch (param->hdr.id) {
case MMAL_PARAMETER_CAMERA_SETTINGS:
{
MMAL_PARAMETER_CAMERA_SETTINGS_T *settings = (MMAL_PARAMETER_CAMERA_SETTINGS_T*)param;
vcos_log_error("%s: Exposure now %u, analog gain %u/%u, digital gain %u/%u",
__func__, settings->exposure,
settings->analog_gain.num, settings->analog_gain.den,
settings->digital_gain.num, settings->digital_gain.den);
vcos_log_error("%s: AWB R=%u/%u, B=%u/%u", __func__,
settings->awb_red_gain.num, settings->awb_red_gain.den,
settings->awb_blue_gain.num, settings->awb_blue_gain.den
);
}
break;
default:
{
vcos_log_error("%s: Received unexpected updated: id=%d", __func__, param->hdr.id);
}
break;
}
} else if (buffer->cmd == MMAL_EVENT_ERROR) {
vcos_log_error("%s: No data received from sensor. Check all connections, including the Sunny one on the camera board", __func__);
} else {
vcos_log_error("%s: Received unexpected camera control callback event, 0x%08x", __func__, buffer->cmd);
}
//release header
mmal_buffer_header_release(buffer);
}
/*
* void FlashCam::control_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
* Callback function for buffer-events (that is, camera returned an image)
*/
void FlashCam::buffer_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer) {
int abort = 0; //flag for detecting if we need to abort due to error
int complete = 0; //flag for detecting if a full frame is recieved
int discard = 0; //flag for detecting if we need to discard buffer
int max_idx = 0; //flag for detecting if _framebuffer is out of memory
uint64_t presentationtime = 0;
//retrieve userdata
FLASHCAM_PORT_USERDATA_T *userdata = (FLASHCAM_PORT_USERDATA_T *)port->userdata;
//is userdata properly set?
if (userdata) {
// Are there bytes to write?
if (buffer->length) {
bool pll_state = false;
#ifdef BUILD_FLASHCAM_WITH_PLL
FlashCamPLL::update(buffer->pts, &pll_state);
#endif
//OpenGL processing?
if (userdata->settings->opengl_enabled) {
#ifdef BUILD_FLASHCAM_WITH_OPENGL
unsigned int length = mmal_queue_length(userdata->opengl_queue);
//fprintf(stdout, "%s: QueueSize - %d (%d) \n", __func__, length, port->buffer_num);
//push to queue, unlock buffer, update PLL and return.
if ( length + 1 < port->buffer_num) {
// set FlashCam data in Buffer.
FLASHCAM_OPENGL_BUF_T* glb = FlashCamOpenGL::getOpenGLBuffer();
if (glb != NULL) {
glb->pll_state = pll_state;
glb->buffer = buffer;
} else {
vcos_log_error("%s: No OpenGL buffer available in pool." , __func__);
}
//push buffer to OpenGL queue for processing
mmal_queue_put(userdata->opengl_queue, &glb->glb_mmal_buffer);
//fprintf(stdout, "%s: Posting...\n", __func__);
vcos_semaphore_post(&(userdata->sem_capture));
//buffer released by OpenGL worker.
return;
}
fprintf(stdout, "%s: DISCARD! \n", __func__);
#else
vcos_log_error("%s: OpenGL Support not build." , __func__);
#endif
discard = 1;
// `normal` processing
} else {
//lock buffer --> callback is async!
mmal_buffer_header_mem_lock(buffer);
// We are decoding YUV packages
// - 4/6 = Y
// - 1/6 = U
// - 1/6 = V
unsigned int length_Y = (buffer->length << 2) / 6;
unsigned int length_U = (buffer->length - length_Y) >> 1;
unsigned int length_V = length_U;
unsigned int offset_Y = userdata->framebuffer_idx;
unsigned int offset_U = userdata->settings->height * userdata->settings->width * 1.00 + (userdata->framebuffer_idx >> 2);
unsigned int offset_V = userdata->settings->height * userdata->settings->width * 1.25 + (userdata->framebuffer_idx >> 2);
//max index to be written
max_idx = offset_V + length_V;
//does it fit in buffer?
if ( max_idx > userdata->framebuffer_size ) {
vcos_log_error("%s: Framebuffer full (%d > %d) - aborting.." , __func__, max_idx , userdata->framebuffer_size );
abort = 1;
} else {
//copy Y
memcpy ( &userdata->framebuffer[offset_Y] , &buffer->data[0] , length_Y );
//copy U
memcpy ( &userdata->framebuffer[offset_U] , &buffer->data[length_Y] , length_U );
//copy V
memcpy ( &userdata->framebuffer[offset_V] , &buffer->data[length_Y + length_U] , length_V );
//update index
userdata->framebuffer_idx += length_Y;
}
//done with data..
mmal_buffer_header_mem_unlock(buffer);
}
}
if (discard == 0) {
// Check end of frame or error
if (buffer->flags & MMAL_BUFFER_HEADER_FLAG_TRANSMISSION_FAILED)
abort = 1;
if (buffer->flags & MMAL_BUFFER_HEADER_FLAG_FRAME_END)
complete = 1;
presentationtime = buffer->pts;
}
} else {
vcos_log_error("%s: Received a camera still buffer callback with no state", __func__);
}
// release buffer back to the pool
mmal_buffer_header_release(buffer);
// and send one back to the port (if still open)
if (port->is_enabled) {
MMAL_STATUS_T status;
MMAL_BUFFER_HEADER_T *new_buffer = mmal_queue_get(userdata->camera_pool->queue);
// and back to the port from there.
if (new_buffer)
status = mmal_port_send_buffer(port, new_buffer);
if (!new_buffer || status != MMAL_SUCCESS)
vcos_log_error("%s: Unable to return the buffer to the camera still port", __func__);
}
if (discard == 0) {
//post that we are done
if (abort) {
vcos_semaphore_post(&(userdata->sem_capture));
} else if (complete) {
if (userdata->callback)
userdata->callback( userdata->framebuffer , userdata->settings->width , userdata->settings->height);
//release semaphore
userdata->framebuffer_idx = 0;
vcos_semaphore_post(&(userdata->sem_capture));
}
}
}
// Setup connection between Input/Output ports
MMAL_STATUS_T FlashCam::connectPorts(MMAL_PORT_T *output_port, MMAL_PORT_T *input_port, MMAL_CONNECTION_T **connection) {
MMAL_STATUS_T status = mmal_connection_create(connection, output_port, input_port, MMAL_CONNECTION_FLAG_TUNNELLING | MMAL_CONNECTION_FLAG_ALLOCATION_ON_INPUT);
if (status == MMAL_SUCCESS) {
if ((status = mmal_connection_enable(*connection)) != MMAL_SUCCESS)
mmal_connection_destroy(*connection);
}
return status;
}
/*
* int FlashCam::capture( void )
*
* Capture an image
*
*/
int FlashCam::startCapture() {
int status;
if (!_initialised) {
fprintf(stderr, "%s: Camera not initialised.\n", __func__);
return FlashCamMMAL::mmal_to_int(MMAL_EINVAL);
}
// Is camera active?
if (_active) {
fprintf(stderr, "%s: Camera already capturing.\n", __func__);
return FlashCamMMAL::mmal_to_int(MMAL_EINVAL);
}
if (_settings.verbose)
fprintf(stdout, "%s: Starting capture\n", __func__);
//update state
_state.settings = &_settings;
_state.params = &_params;
_state.userdata = &_userdata;
if (_settings.mode == FLASHCAM_MODE_VIDEO) {
_state.port = _camera_component->output[MMAL_CAMERA_VIDEO_PORT];
} else if ( _settings.mode = FLASHCAM_MODE_CAPTURE ) {
_state.port = _camera_component->output[MMAL_CAMERA_CAPTURE_PORT];
} else {
fprintf(stderr, "%s: Cannot start camera. Unknown mode (%u)\n", __func__, _settings.mode);
return FlashCamMMAL::mmal_to_int(MMAL_EINVAL);
}
#ifdef BUILD_FLASHCAM_WITH_OPENGL
//start EGL thread for processing
if (_settings.opengl_enabled) {
FlashCamOpenGL::start();
}
#endif
#ifdef BUILD_FLASHCAM_WITH_PLL
if (_settings.mode == FLASHCAM_MODE_VIDEO) {
if (FlashCamPLL::start()) {
fprintf(stderr, "%s: PLL cannot be started.\n", __func__);
return FlashCamMMAL::mmal_to_int(MMAL_EINVAL);
}
}
#endif
//start camera
if (status = setCapture(_state.port, 1)) {
vcos_log_error("%s: Failed to start video stream", __func__);
return status;
}
_active = true;
// When in capturemode: Wait for capture to complete
if ( _settings.mode == FLASHCAM_MODE_CAPTURE ) {
vcos_semaphore_wait(&_userdata.sem_capture);
_active = false;
}
if (_settings.verbose)
fprintf(stdout, "%s: Success\n", __func__);
return FlashCamMMAL::mmal_to_int(MMAL_SUCCESS);
}
int FlashCam::stopCapture() {
int status;
if (!_initialised) {
fprintf(stderr, "%s: Camera not initialised.\n", __func__);
return FlashCamMMAL::mmal_to_int(MMAL_EINVAL);
}
// Is camera active?
if (!_active) {
fprintf(stderr, "%s: Camera not capturing.\n", __func__);
return FlashCamMMAL::mmal_to_int(MMAL_EINVAL);
}
if (_settings.verbose)
fprintf(stdout, "%s: Stopping stream.\n", __func__);
if (_settings.mode == FLASHCAM_MODE_VIDEO) {
//shutdown PLL before camera, as PLL has some internal Camera dependencies.
// Shutting down camera will crash PLL.
#ifdef BUILD_FLASHCAM_WITH_PLL
if (FlashCamPLL::stop()) {
fprintf(stderr, "%s: PLL cannot be stopped.\n", __func__);
return FlashCamMMAL::mmal_to_int(MMAL_EINVAL);
}
#endif
//stop video
if (status = setCapture(_camera_component->output[MMAL_CAMERA_VIDEO_PORT], 0)) {
vcos_log_error("%s: Failed to stop camera", __func__);
return status;
}
} else if ( _settings.mode = FLASHCAM_MODE_CAPTURE ) {
//stop capture
if (status = setCapture(_camera_component->output[MMAL_CAMERA_CAPTURE_PORT], 0)) {
vcos_log_error("%s: Failed to stop camera", __func__);
return status;
}
} else {
//unknown mode..
fprintf(stderr, "%s: Cannot stop camera. Unknown mode (%u)\n", __func__, _settings.mode);
return FlashCamMMAL::mmal_to_int(MMAL_EINVAL);
}
#ifdef BUILD_FLASHCAM_WITH_OPENGL
//Stop EGL thread
// This needs to be done after shutting down the camera,
// ensuring that pending frames/buffers are processed
// and pushed to the appropiate inter-thread queues.
// Sleeping is required to provide all async mmal-related systems to terminate.
usleep(1000000);
FlashCamOpenGL::stop();
#endif
//camera inactive
_active = false;
if (_settings.verbose)
fprintf(stdout, "%s: Success\n", __func__);
return FlashCamMMAL::mmal_to_int(MMAL_SUCCESS);
}
void FlashCam::setFrameCallback(FLASHCAM_CALLBACK_T callback) {
if (_active) return; //no changer/reset while in capturemode
_userdata.callback = callback;
}
#ifdef BUILD_FLASHCAM_WITH_OPENGL
void FlashCam::setFrameCallback(FLASHCAM_CALLBACK_OPENGL_T callback) {
if (_active) return; //no changer/reset while in capturemode
_userdata.callback_egl = callback;
}
#endif
void FlashCam::resetFrameCallback() {
if (_active) return; //no changer/reset while in capturemode
_userdata.callback = NULL;
#ifdef BUILD_FLASHCAM_WITH_OPENGL
_userdata.callback_egl = NULL;
#endif
}
int FlashCam::getGPUtime(uint64_t *us) {
*us = 0;
if (_state.port && _state.port->is_enabled) {
MMAL_STATUS_T status = mmal_port_parameter_get_uint64(_state.port, MMAL_PARAMETER_SYSTEM_TIME, us);
return FlashCamMMAL::mmal_to_int(status);
}
return FlashCamMMAL::mmal_to_int(MMAL_ENOTREADY);
}
/* SETTING MANAGEMENT */
void FlashCam::getDefaultSettings(FLASHCAM_SETTINGS_T *settings) {
settings->width = 640;
settings->height = 480;
settings->verbose = 1;
settings->update = 0;
settings->mode = FLASHCAM_MODE_CAPTURE;
settings->opengl_enabled = 0;
#ifdef BUILD_FLASHCAM_WITH_PLL
FlashCamPLL::getDefaultSettings(settings);
#endif
}
void FlashCam::printSettings(FLASHCAM_SETTINGS_T *settings) {
fprintf(stdout, "Width : %d\n", settings->width);
fprintf(stdout, "Height : %d\n", settings->height);
fprintf(stdout, "Verbose : %d\n", settings->verbose);
fprintf(stdout, "Update : %d\n", settings->update);
fprintf(stdout, "Camera-Mode : %d\n", settings->mode);
fprintf(stdout, "OpenGL : %d\n", settings->opengl_enabled);
#ifdef BUILD_FLASHCAM_WITH_PLL
FlashCamPLL::printSettings(settings);
#endif
}
#ifdef PLLTUNE
void FlashCam::getInternalState( FLASHCAM_INTERNAL_STATE_T** state ) {
*state = &_state;
}
#endif
int FlashCam::setSettings(FLASHCAM_SETTINGS_T *settings) {
//update settings
memcpy(&_settings, settings, sizeof(FLASHCAM_SETTINGS_T));
if (_settings.verbose)
fprintf(stdout, "%s: (re)setting camera.\n", __func__);
//reset camera
return resetCamera();
}
int FlashCam::getSettings(FLASHCAM_SETTINGS_T *settings) {
memcpy(settings, &_settings, sizeof(FLASHCAM_SETTINGS_T));
}
//To update size we need to reset & re-initialise all components
int FlashCam::setSettingSize( unsigned int width, unsigned int height ) {
//update settings
_settings.width = width;
_settings.height = height;
if (_settings.verbose)
fprintf(stdout, "%s: Updating size to: %u x %u (w x h)\n", __func__, width, height);
//reset camera
return resetCamera();
}
int FlashCam::getSettingSize( unsigned int *width, unsigned int *height ) {
*width = _settings.width;
*height = _settings.height;
return FlashCamMMAL::mmal_to_int(MMAL_SUCCESS);
}
int FlashCam::setSettingVerbose( int verbose ) {
_settings.verbose = verbose;
return FlashCamMMAL::mmal_to_int(MMAL_SUCCESS);
}
int FlashCam::getSettingVerbose( int *verbose ) {
*verbose = _settings.verbose;
return FlashCamMMAL::mmal_to_int(MMAL_SUCCESS);
}
int FlashCam::setSettingUpdate( int update ) {
_settings.update = update;
return setChangeEventRequest(MMAL_PARAMETER_CAMERA_SETTINGS, _settings.update);
}
int FlashCam::getSettingUpdate( int *update ) {
*update = _settings.update;
return FlashCamMMAL::mmal_to_int(MMAL_SUCCESS);
}
int FlashCam::setSettingCaptureMode( FLASHCAM_MODE_T mode ) {
MMAL_STATUS_T status;
// Is camera active?
if (_active) {
fprintf(stderr, "%s: Cannot change camera mode while being in use\n", __func__);
return FlashCamMMAL::mmal_to_int(MMAL_EINVAL);
}
// Is camera initialised?
if (!_initialised) {
fprintf(stderr, "%s: Components not initialised\n", __func__);
return FlashCamMMAL::mmal_to_int(MMAL_EINVAL);
}
_settings.mode = mode;
//Setup userdata / camera
MMAL_PORT_T *old_port = NULL;
MMAL_PORT_T *new_port = NULL;
if (_settings.mode == FLASHCAM_MODE_VIDEO) {
//video mode..
if (_settings.verbose)
fprintf(stdout, "%s: Enabling video-mode.\n", __func__);
new_port = _camera_component->output[MMAL_CAMERA_VIDEO_PORT];
old_port = _camera_component->output[MMAL_CAMERA_CAPTURE_PORT];
//set buffer sizes
if (new_port->buffer_size < new_port->buffer_size_min)
new_port->buffer_size = new_port->buffer_size_min;