-
Notifications
You must be signed in to change notification settings - Fork 1
/
camcv.c
2061 lines (1727 loc) · 68 KB
/
camcv.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
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
/*
Copyright (c) 2013, Broadcom Europe Ltd
Copyright (c) 2013, James Hughes
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* 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.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
*/
/**
* \file RaspiStill.c
* Command line program to capture a still frame and encode it to file.
* Also optionally display a preview/viewfinder of current camera input.
*
* \date 31 Jan 2013
* \Author: James Hughes
*
* Description
*
* 3 components are created; camera, preview and JPG encoder.
* Camera component has three ports, preview, video and stills.
* This program connects preview and stills to the preview and jpg
* encoder. Using mmal we don't need to worry about buffers between these
* components, but we do need to handle buffers from the encoder, which
* are simply written straight to the file in the requisite buffer callback.
*
* We use the RaspiCamControl code to handle the specific camera settings.
*/
// We use some GNU extensions (asprintf, basename)
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <unistd.h>
#include <errno.h>
#include <sysexits.h>
#define VERSION_STRING "v1.3.8"
#include "bcm_host.h"
#include "interface/vcos/vcos.h"
#include "interface/mmal/mmal.h"
#include "interface/mmal/mmal_logging.h"
#include "interface/mmal/mmal_buffer.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 "interface/mmal/util/mmal_connection.h"
#include "RaspiCamControl.h"
#include "RaspiPreview.h"
#include "RaspiCLI.h"
#include "RaspiTex.h"
#include <semaphore.h>
// *** PR : ADDED for OPENCV
#include <cv.h>
#include <highgui.h>
// Standard port setting for the camera component
#define MMAL_CAMERA_PREVIEW_PORT 0
#define MMAL_CAMERA_VIDEO_PORT 1
#define MMAL_CAMERA_CAPTURE_PORT 2
// Stills format information
// 0 implies variable
#define STILLS_FRAME_RATE_NUM 0
#define STILLS_FRAME_RATE_DEN 1
/// Video render needs at least 2 buffers.
#define VIDEO_OUTPUT_BUFFERS_NUM 3
#define MAX_USER_EXIF_TAGS 32
#define MAX_EXIF_PAYLOAD_LENGTH 128
/// Frame advance method
#define FRAME_NEXT_SINGLE 0
#define FRAME_NEXT_TIMELAPSE 1
#define FRAME_NEXT_KEYPRESS 2
#define FRAME_NEXT_FOREVER 3
#define FRAME_NEXT_GPIO 4
#define FRAME_NEXT_SIGNAL 5
#define FRAME_NEXT_IMMEDIATELY 6
int mmal_status_to_int(MMAL_STATUS_T status);
static void signal_handler(int signal_number);
/** Structure containing all state information for the current run
*/
typedef struct
{
int timeout; /// Time taken before frame is grabbed and app then shuts down. Units are milliseconds
int width; /// Requested width of image
int height; /// requested height of image
int quality; /// JPEG quality setting (1-100)
int wantRAW; /// Flag for whether the JPEG metadata also contains the RAW bayer image
char *filename; /// filename of output file
char *linkname; /// filename of output file
MMAL_PARAM_THUMBNAIL_CONFIG_T thumbnailConfig;
int verbose; /// !0 if want detailed run information
int demoMode; /// Run app in demo mode
int demoInterval; /// Interval between camera settings changes
MMAL_FOURCC_T encoding; /// Encoding to use for the output file.
const char *exifTags[MAX_USER_EXIF_TAGS]; /// Array of pointers to tags supplied from the command line
int numExifTags; /// Number of supplied tags
int enableExifTags; /// Enable/Disable EXIF tags in output
int timelapse; /// Delay between each picture in timelapse mode. If 0, disable timelapse
int fullResPreview; /// If set, the camera preview port runs at capture resolution. Reduces fps.
int frameNextMethod; /// Which method to use to advance to next frame
int useGL; /// Render preview using OpenGL
int glCapture; /// Save the GL frame-buffer instead of camera output
int settings; /// Request settings from the camera
int cameraNum; /// Camera number
int burstCaptureMode; /// Enable burst mode
int sensor_mode; /// Sensor mode. 0=auto. Check docs/forum for modes selected by other values.
int datetime; /// Use DateTime instead of frame#
int timestamp; /// Use timestamp instead of frame#
RASPIPREVIEW_PARAMETERS preview_parameters; /// Preview setup parameters
RASPICAM_CAMERA_PARAMETERS camera_parameters; /// Camera setup parameters
MMAL_COMPONENT_T *camera_component; /// Pointer to the camera component
MMAL_COMPONENT_T *encoder_component; /// Pointer to the encoder component
MMAL_COMPONENT_T *null_sink_component; /// Pointer to the null sink component
MMAL_CONNECTION_T *preview_connection; /// Pointer to the connection from camera to preview
MMAL_CONNECTION_T *encoder_connection; /// Pointer to the connection from camera to encoder
MMAL_POOL_T *encoder_pool; /// Pointer to the pool of buffers used by encoder output port
RASPITEX_STATE raspitex_state; /// GL renderer state and parameters
} RASPISTILL_STATE;
/** Struct used to pass information in encoder port userdata to callback
*/
typedef struct
{
FILE *file_handle; /// File handle to write buffer data to.
VCOS_SEMAPHORE_T complete_semaphore; /// semaphore which is posted when we reach end of frame (indicates end of capture or fault)
RASPISTILL_STATE *pstate; /// pointer to our state in case required in callback
} PORT_USERDATA;
static void display_valid_parameters(char *app_name);
static void store_exif_tag(RASPISTILL_STATE *state, const char *exif_tag);
/// Comamnd ID's and Structure defining our command line options
#define CommandHelp 0
#define CommandWidth 1
#define CommandHeight 2
#define CommandQuality 3
#define CommandRaw 4
#define CommandOutput 5
#define CommandVerbose 6
#define CommandTimeout 7
#define CommandThumbnail 8
#define CommandDemoMode 9
#define CommandEncoding 10
#define CommandExifTag 11
#define CommandTimelapse 12
#define CommandFullResPreview 13
#define CommandLink 14
#define CommandKeypress 15
#define CommandSignal 16
#define CommandGL 17
#define CommandGLCapture 18
#define CommandSettings 19
#define CommandCamSelect 20
#define CommandBurstMode 21
#define CommandSensorMode 22
#define CommandDateTime 23
#define CommandTimeStamp 24
static COMMAND_LIST cmdline_commands[] =
{
{ CommandHelp, "-help", "?", "This help information", 0 },
{ CommandWidth, "-width", "w", "Set image width <size>", 1 },
{ CommandHeight, "-height", "h", "Set image height <size>", 1 },
{ CommandQuality, "-quality", "q", "Set jpeg quality <0 to 100>", 1 },
{ CommandRaw, "-raw", "r", "Add raw bayer data to jpeg metadata", 0 },
{ CommandOutput, "-output", "o", "Output filename <filename> (to write to stdout, use '-o -'). If not specified, no file is saved", 1 },
{ CommandLink, "-latest", "l", "Link latest complete image to filename <filename>", 1},
{ CommandVerbose, "-verbose", "v", "Output verbose information during run", 0 },
{ CommandTimeout, "-timeout", "t", "Time (in ms) before takes picture and shuts down (if not specified, set to 5s)", 1 },
{ CommandThumbnail,"-thumb", "th", "Set thumbnail parameters (x:y:quality) or none", 1},
{ CommandDemoMode,"-demo", "d", "Run a demo mode (cycle through range of camera options, no capture)", 0},
{ CommandEncoding,"-encoding", "e", "Encoding to use for output file (jpg, bmp, gif, png)", 1},
{ CommandExifTag, "-exif", "x", "EXIF tag to apply to captures (format as 'key=value') or none", 1},
{ CommandTimelapse,"-timelapse", "tl", "Timelapse mode. Takes a picture every <t>ms", 1},
{ CommandFullResPreview,"-fullpreview","fp", "Run the preview using the still capture resolution (may reduce preview fps)", 0},
{ CommandKeypress,"-keypress", "k", "Wait between captures for a ENTER, X then ENTER to exit", 0},
{ CommandSignal, "-signal", "s", "Wait between captures for a SIGUSR1 from another process", 0},
{ CommandGL, "-gl", "g", "Draw preview to texture instead of using video render component", 0},
{ CommandGLCapture, "-glcapture","gc", "Capture the GL frame-buffer instead of the camera image", 0},
{ CommandSettings, "-settings", "set","Retrieve camera settings and write to stdout", 0},
{ CommandCamSelect, "-camselect","cs", "Select camera <number>. Default 0", 1 },
{ CommandBurstMode, "-burst", "bm", "Enable 'burst capture mode'", 0},
{ CommandSensorMode,"-mode", "md", "Force sensor mode. 0=auto. See docs for other modes available", 1},
{ CommandDateTime, "-datetime", "dt", "Replace frame number in file name with DateTime (YearMonthDayHourMinSec)", 0},
{ CommandTimeStamp, "-timestamp", "ts", "Replace frame number in file name with unix timestamp (seconds since 1900)", 0},
};
static int cmdline_commands_size = sizeof(cmdline_commands) / sizeof(cmdline_commands[0]);
static struct
{
char *format;
MMAL_FOURCC_T encoding;
} encoding_xref[] =
{
{"jpg", MMAL_ENCODING_JPEG},
{"bmp", MMAL_ENCODING_BMP},
{"gif", MMAL_ENCODING_GIF},
{"png", MMAL_ENCODING_PNG}
};
static int encoding_xref_size = sizeof(encoding_xref) / sizeof(encoding_xref[0]);
static struct
{
char *description;
int nextFrameMethod;
} next_frame_description[] =
{
{"Single capture", FRAME_NEXT_SINGLE},
{"Capture on timelapse", FRAME_NEXT_TIMELAPSE},
{"Capture on keypress", FRAME_NEXT_KEYPRESS},
{"Run forever", FRAME_NEXT_FOREVER},
{"Capture on GPIO", FRAME_NEXT_GPIO},
{"Capture on signal", FRAME_NEXT_SIGNAL},
};
static int next_frame_description_size = sizeof(next_frame_description) / sizeof(next_frame_description[0]);
/**
* Assign a default set of parameters to the state passed in
*
* @param state Pointer to state structure to assign defaults to
*/
static void default_status(RASPISTILL_STATE *state)
{
if (!state)
{
vcos_assert(0);
return;
}
// state->timeout = 5000; // 5s delay before take image
state->timeout = 1000; // 1s delay before take image
// state->width = 2592;
state->width = 320;
// state->height = 1944;
state->height = 200;
state->quality = 85;
state->wantRAW = 0;
state->filename = NULL;
state->linkname = NULL;
state->verbose = 0;
state->thumbnailConfig.enable = 1;
state->thumbnailConfig.width = 64;
state->thumbnailConfig.height = 48;
state->thumbnailConfig.quality = 35;
state->demoMode = 0;
state->demoInterval = 250; // ms
state->camera_component = NULL;
state->encoder_component = NULL;
state->preview_connection = NULL;
state->encoder_connection = NULL;
state->encoder_pool = NULL;
state->encoding = MMAL_ENCODING_JPEG;
state->numExifTags = 0;
state->enableExifTags = 1;
state->timelapse = 0;
state->fullResPreview = 0;
state->frameNextMethod = FRAME_NEXT_SINGLE;
state->useGL = 0;
state->glCapture = 0;
state->settings = 0;
state->cameraNum = 0;
state->burstCaptureMode=0;
state->sensor_mode = 0;
state->datetime = 0;
state->timestamp = 0;
// Setup preview window defaults
raspipreview_set_defaults(&state->preview_parameters);
// Set up the camera_parameters to default
raspicamcontrol_set_defaults(&state->camera_parameters);
// Set initial GL preview state
raspitex_set_defaults(&state->raspitex_state);
}
/**
* Dump image state parameters to stderr. Used for debugging
*
* @param state Pointer to state structure to assign defaults to
*/
static void dump_status(RASPISTILL_STATE *state)
{
int i;
if (!state)
{
vcos_assert(0);
return;
}
fprintf(stderr, "Width %d, Height %d, quality %d, filename %s\n", state->width,
state->height, state->quality, state->filename);
fprintf(stderr, "Time delay %d, Raw %s\n", state->timeout,
state->wantRAW ? "yes" : "no");
fprintf(stderr, "Thumbnail enabled %s, width %d, height %d, quality %d\n",
state->thumbnailConfig.enable ? "Yes":"No", state->thumbnailConfig.width,
state->thumbnailConfig.height, state->thumbnailConfig.quality);
fprintf(stderr, "Link to latest frame enabled ");
if (state->linkname)
{
fprintf(stderr, " yes, -> %s\n", state->linkname);
}
else
{
fprintf(stderr, " no\n");
}
fprintf(stderr, "Full resolution preview %s\n", state->fullResPreview ? "Yes": "No");
fprintf(stderr, "Capture method : ");
for (i=0;i<next_frame_description_size;i++)
{
if (state->frameNextMethod == next_frame_description[i].nextFrameMethod)
fprintf(stderr, next_frame_description[i].description);
}
fprintf(stderr, "\n\n");
if (state->enableExifTags)
{
if (state->numExifTags)
{
fprintf(stderr, "User supplied EXIF tags :\n");
for (i=0;i<state->numExifTags;i++)
{
fprintf(stderr, "%s", state->exifTags[i]);
if (i != state->numExifTags-1)
fprintf(stderr, ",");
}
fprintf(stderr, "\n\n");
}
}
else
fprintf(stderr, "EXIF tags disabled\n");
raspipreview_dump_parameters(&state->preview_parameters);
raspicamcontrol_dump_parameters(&state->camera_parameters);
}
/**
* Parse the incoming command line and put resulting parameters in to the state
*
* @param argc Number of arguments in command line
* @param argv Array of pointers to strings from command line
* @param state Pointer to state structure to assign any discovered parameters to
* @return non-0 if failed for some reason, 0 otherwise
*/
static int parse_cmdline(int argc, const char **argv, RASPISTILL_STATE *state)
{
// Parse the command line arguments.
// We are looking for --<something> or -<abreviation of something>
int valid = 1;
int i;
for (i = 1; i < argc && valid; i++)
{
int command_id, num_parameters;
if (!argv[i])
continue;
if (argv[i][0] != '-')
{
valid = 0;
continue;
}
// Assume parameter is valid until proven otherwise
valid = 1;
command_id = raspicli_get_command_id(cmdline_commands, cmdline_commands_size, &argv[i][1], &num_parameters);
// If we found a command but are missing a parameter, continue (and we will drop out of the loop)
if (command_id != -1 && num_parameters > 0 && (i + 1 >= argc) )
continue;
// We are now dealing with a command line option
switch (command_id)
{
case CommandHelp:
display_valid_parameters(basename(argv[0]));
// exit straight away if help requested
return -1;
case CommandWidth: // Width > 0
if (sscanf(argv[i + 1], "%u", &state->width) != 1)
valid = 0;
else
i++;
break;
case CommandHeight: // Height > 0
if (sscanf(argv[i + 1], "%u", &state->height) != 1)
valid = 0;
else
i++;
break;
case CommandQuality: // Quality = 1-100
if (sscanf(argv[i + 1], "%u", &state->quality) == 1)
{
if (state->quality > 100)
{
fprintf(stderr, "Setting max quality = 100\n");
state->quality = 100;
}
i++;
}
else
valid = 0;
break;
case CommandRaw: // Add raw bayer data in metadata
state->wantRAW = 1;
break;
case CommandOutput: // output filename
{
int len = strlen(argv[i + 1]);
if (len)
{
state->filename = malloc(len + 10); // leave enough space for any timelapse generated changes to filename
vcos_assert(state->filename);
if (state->filename)
strncpy(state->filename, argv[i + 1], len+1);
i++;
}
else
valid = 0;
break;
}
case CommandLink :
{
int len = strlen(argv[i+1]);
if (len)
{
state->linkname = malloc(len + 10);
vcos_assert(state->linkname);
if (state->linkname)
strncpy(state->linkname, argv[i + 1], len+1);
i++;
}
else
valid = 0;
break;
}
case CommandVerbose: // display lots of data during run
state->verbose = 1;
break;
case CommandDateTime: // use datetime
state->datetime = 1;
break;
case CommandTimeStamp: // use timestamp
state->timestamp = 1;
break;
case CommandTimeout: // Time to run viewfinder for before taking picture, in seconds
{
if (sscanf(argv[i + 1], "%u", &state->timeout) == 1)
{
// Ensure that if previously selected CommandKeypress we don't overwrite it
if (state->timeout == 0 && state->frameNextMethod == FRAME_NEXT_SINGLE)
state->frameNextMethod = FRAME_NEXT_FOREVER;
i++;
}
else
valid = 0;
break;
}
case CommandThumbnail : // thumbnail parameters - needs string "x:y:quality"
if ( strcmp( argv[ i + 1 ], "none" ) == 0 )
{
state->thumbnailConfig.enable = 0;
}
else
{
sscanf(argv[i + 1], "%d:%d:%d",
&state->thumbnailConfig.width,
&state->thumbnailConfig.height,
&state->thumbnailConfig.quality);
}
i++;
break;
case CommandDemoMode: // Run in demo mode - no capture
{
// Demo mode might have a timing parameter
// so check if a) we have another parameter, b) its not the start of the next option
if (i + 1 < argc && argv[i+1][0] != '-')
{
if (sscanf(argv[i + 1], "%u", &state->demoInterval) == 1)
{
// TODO : What limits do we need for timeout?
state->demoMode = 1;
i++;
}
else
valid = 0;
}
else
{
state->demoMode = 1;
}
break;
}
case CommandEncoding :
{
int len = strlen(argv[i + 1]);
valid = 0;
if (len)
{
int j;
for (j=0;j<encoding_xref_size;j++)
{
if (strcmp(encoding_xref[j].format, argv[i+1]) == 0)
{
state->encoding = encoding_xref[j].encoding;
valid = 1;
i++;
break;
}
}
}
break;
}
case CommandExifTag:
if ( strcmp( argv[ i + 1 ], "none" ) == 0 )
{
state->enableExifTags = 0;
}
else
{
store_exif_tag(state, argv[i+1]);
}
i++;
break;
case CommandTimelapse:
if (sscanf(argv[i + 1], "%u", &state->timelapse) != 1)
valid = 0;
else
{
if (state->timelapse)
state->frameNextMethod = FRAME_NEXT_TIMELAPSE;
else
state->frameNextMethod = FRAME_NEXT_IMMEDIATELY;
i++;
}
break;
case CommandFullResPreview:
state->fullResPreview = 1;
break;
case CommandKeypress: // Set keypress between capture mode
state->frameNextMethod = FRAME_NEXT_KEYPRESS;
break;
case CommandSignal: // Set SIGUSR1 between capture mode
state->frameNextMethod = FRAME_NEXT_SIGNAL;
// Reenable the signal
signal(SIGUSR1, signal_handler);
break;
case CommandGL:
state->useGL = 1;
break;
case CommandGLCapture:
state->glCapture = 1;
break;
case CommandSettings:
state->settings = 1;
break;
case CommandCamSelect: //Select camera input port
{
if (sscanf(argv[i + 1], "%u", &state->cameraNum) == 1)
{
i++;
}
else
valid = 0;
break;
}
case CommandBurstMode:
state->burstCaptureMode=1;
break;
case CommandSensorMode:
{
if (sscanf(argv[i + 1], "%u", &state->sensor_mode) == 1)
{
i++;
}
else
valid = 0;
break;
}
default:
{
// Try parsing for any image specific parameters
// result indicates how many parameters were used up, 0,1,2
// but we adjust by -1 as we have used one already
const char *second_arg = (i + 1 < argc) ? argv[i + 1] : NULL;
int parms_used = raspicamcontrol_parse_cmdline(&state->camera_parameters, &argv[i][1], second_arg);
// Still unused, try preview options
if (!parms_used)
parms_used = raspipreview_parse_cmdline(&state->preview_parameters, &argv[i][1], second_arg);
// Still unused, try GL preview options
if (!parms_used)
parms_used = raspitex_parse_cmdline(&state->raspitex_state, &argv[i][1], second_arg);
// If no parms were used, this must be a bad parameters
if (!parms_used)
valid = 0;
else
i += parms_used - 1;
break;
}
}
}
/* GL preview parameters use preview parameters as defaults unless overriden */
if (! state->raspitex_state.gl_win_defined)
{
state->raspitex_state.x = state->preview_parameters.previewWindow.x;
state->raspitex_state.y = state->preview_parameters.previewWindow.y;
state->raspitex_state.width = state->preview_parameters.previewWindow.width;
state->raspitex_state.height = state->preview_parameters.previewWindow.height;
}
/* Also pass the preview information through so GL renderer can determine
* the real resolution of the multi-media image */
state->raspitex_state.preview_x = state->preview_parameters.previewWindow.x;
state->raspitex_state.preview_y = state->preview_parameters.previewWindow.y;
state->raspitex_state.preview_width = state->preview_parameters.previewWindow.width;
state->raspitex_state.preview_height = state->preview_parameters.previewWindow.height;
state->raspitex_state.opacity = state->preview_parameters.opacity;
state->raspitex_state.verbose = state->verbose;
if (!valid)
{
fprintf(stderr, "Invalid command line option (%s)\n", argv[i-1]);
return 1;
}
return 0;
}
/**
* Display usage information for the application to stdout
*
* @param app_name String to display as the application name
*/
static void display_valid_parameters(char *app_name)
{
fprintf(stderr, "Runs camera for specific time, and take JPG capture at end if requested\n\n");
fprintf(stderr, "usage: %s [options]\n\n", app_name);
fprintf(stderr, "Image parameter commands\n\n");
raspicli_display_help(cmdline_commands, cmdline_commands_size);
// Help for preview options
raspipreview_display_help();
// Now display any help information from the camcontrol code
raspicamcontrol_display_help();
// Now display GL preview help
raspitex_display_help();
fprintf(stderr, "\n");
return;
}
/**
* buffer header callback function for camera control
*
* No actions taken in current version
*
* @param port Pointer to port from which callback originated
* @param buffer mmal buffer header pointer
*/
static void camera_control_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
{
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("Exposure now %u, analog gain %u/%u, digital gain %u/%u",
settings->exposure,
settings->analog_gain.num, settings->analog_gain.den,
settings->digital_gain.num, settings->digital_gain.den);
vcos_log_error("AWB R=%u/%u, B=%u/%u",
settings->awb_red_gain.num, settings->awb_red_gain.den,
settings->awb_blue_gain.num, settings->awb_blue_gain.den
);
}
break;
}
}
else if (buffer->cmd == MMAL_EVENT_ERROR)
{
vcos_log_error("No data received from sensor. Check all connections, including the Sunny one on the camera board");
}
else
vcos_log_error("Received unexpected camera control callback event, 0x%08x", buffer->cmd);
mmal_buffer_header_release(buffer);
}
/**
* buffer header callback function for encoder
*
* Callback will dump buffer data to the specific file
*
* @param port Pointer to port from which callback originated
* @param buffer mmal buffer header pointer
*/
static void encoder_buffer_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
{
int complete = 0;
// We pass our file handle and other stuff in via the userdata field.
PORT_USERDATA *pData = (PORT_USERDATA *)port->userdata;
printf("i am here!\n");
if (pData)
{
int bytes_written = buffer->length;
if (buffer->length && pData->file_handle)
{
mmal_buffer_header_mem_lock(buffer);
//
// *** PR : OPENCV Stuff here !
//
// create a CvMat empty structure, with size of the buffer.
CvMat* buf = cvCreateMat(1,buffer->length,CV_8UC1);
// copy buffer from cam to CvMat
buf->data.ptr = buffer->data;
// decode image (interpret jpg)
IplImage *img = cvDecodeImage(buf, CV_LOAD_IMAGE_COLOR);
// we can save it !
cvSaveImage("foobar.bmp", img, 0);
// or display it
cvNamedWindow("camcvWin", CV_WINDOW_AUTOSIZE);
cvShowImage("camcvWin", img );
cvWaitKey(0);
bytes_written = fwrite(buffer->data, 1, buffer->length, pData->file_handle);
mmal_buffer_header_mem_unlock(buffer);
}
// We need to check we wrote what we wanted - it's possible we have run out of storage.
if (bytes_written != buffer->length)
{
vcos_log_error("Unable to write buffer to file - aborting");
complete = 1;
}
// Now flag if we have completed
if (buffer->flags & (MMAL_BUFFER_HEADER_FLAG_FRAME_END | MMAL_BUFFER_HEADER_FLAG_TRANSMISSION_FAILED))
complete = 1;
}
else
{
vcos_log_error("Received a encoder buffer callback with no state");
}
// 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_SUCCESS;
MMAL_BUFFER_HEADER_T *new_buffer;
new_buffer = mmal_queue_get(pData->pstate->encoder_pool->queue);
if (new_buffer)
{
status = mmal_port_send_buffer(port, new_buffer);
}
if (!new_buffer || status != MMAL_SUCCESS)
vcos_log_error("Unable to return a buffer to the encoder port");
}
if (complete)
vcos_semaphore_post(&(pData->complete_semaphore));
}
/**
* Create the camera component, set up its ports
*
* @param state Pointer to state control struct. camera_component member set to the created camera_component if successfull.
*
* @return MMAL_SUCCESS if all OK, something else otherwise
*
*/
static MMAL_STATUS_T create_camera_component(RASPISTILL_STATE *state)
{
MMAL_COMPONENT_T *camera = 0;
MMAL_ES_FORMAT_T *format;
MMAL_PORT_T *preview_port = NULL, *video_port = NULL, *still_port = NULL;
MMAL_STATUS_T status;
/* Create the component */
status = mmal_component_create(MMAL_COMPONENT_DEFAULT_CAMERA, &camera);
if (status != MMAL_SUCCESS)
{
vcos_log_error("Failed to create camera component");
goto error;
}
status = raspicamcontrol_set_stereo_mode(camera->output[0], &state->camera_parameters.stereo_mode);
status += raspicamcontrol_set_stereo_mode(camera->output[1], &state->camera_parameters.stereo_mode);
status += raspicamcontrol_set_stereo_mode(camera->output[2], &state->camera_parameters.stereo_mode);
if (status != MMAL_SUCCESS)
{
vcos_log_error("Could not set stereo mode : error %d", status);
goto error;
}
MMAL_PARAMETER_INT32_T camera_num =
{{MMAL_PARAMETER_CAMERA_NUM, sizeof(camera_num)}, state->cameraNum};
status = mmal_port_parameter_set(camera->control, &camera_num.hdr);
if (status != MMAL_SUCCESS)
{
vcos_log_error("Could not select camera : error %d", status);
goto error;
}
if (!camera->output_num)
{
status = MMAL_ENOSYS;
vcos_log_error("Camera doesn't have output ports");
goto error;
}
status = mmal_port_parameter_set_uint32(camera->control, MMAL_PARAMETER_CAMERA_CUSTOM_SENSOR_CONFIG, state->sensor_mode);
if (status != MMAL_SUCCESS)
{
vcos_log_error("Could not set sensor mode : error %d", status);
goto error;
}
preview_port = camera->output[MMAL_CAMERA_PREVIEW_PORT];
video_port = camera->output[MMAL_CAMERA_VIDEO_PORT];
still_port = camera->output[MMAL_CAMERA_CAPTURE_PORT];
if (state->settings)
{
MMAL_PARAMETER_CHANGE_EVENT_REQUEST_T change_event_request =
{{MMAL_PARAMETER_CHANGE_EVENT_REQUEST, sizeof(MMAL_PARAMETER_CHANGE_EVENT_REQUEST_T)},
MMAL_PARAMETER_CAMERA_SETTINGS, 1};
status = mmal_port_parameter_set(camera->control, &change_event_request.hdr);
if ( status != MMAL_SUCCESS )
{
vcos_log_error("No camera settings events");
}
}
// Enable the camera, and tell it its control callback function
status = mmal_port_enable(camera->control, camera_control_callback);
if (status != MMAL_SUCCESS)
{
vcos_log_error("Unable to enable control port : error %d", status);
goto error;
}
// set up the camera configuration
{
MMAL_PARAMETER_CAMERA_CONFIG_T cam_config =
{
{ MMAL_PARAMETER_CAMERA_CONFIG, sizeof(cam_config) },
.max_stills_w = state->width,
.max_stills_h = state->height,
.stills_yuv422 = 0,
.one_shot_stills = 1,
.max_preview_video_w = state->preview_parameters.previewWindow.width,
.max_preview_video_h = state->preview_parameters.previewWindow.height,
.num_preview_video_frames = 3,
.stills_capture_circular_buffer_height = 0,
.fast_preview_resume = 0,
.use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RESET_STC
};
if (state->fullResPreview)
{
cam_config.max_preview_video_w = state->width;
cam_config.max_preview_video_h = state->height;
}
mmal_port_parameter_set(camera->control, &cam_config.hdr);
}
raspicamcontrol_set_all_parameters(camera, &state->camera_parameters);
// Now set up the port formats
format = preview_port->format;
format->encoding = MMAL_ENCODING_OPAQUE;
format->encoding_variant = MMAL_ENCODING_I420;
if(state->camera_parameters.shutter_speed > 6000000)
{
MMAL_PARAMETER_FPS_RANGE_T fps_range = {{MMAL_PARAMETER_FPS_RANGE, sizeof(fps_range)},
{ 50, 1000 }, {166, 1000}};
mmal_port_parameter_set(preview_port, &fps_range.hdr);
}
else if(state->camera_parameters.shutter_speed > 1000000)
{
MMAL_PARAMETER_FPS_RANGE_T fps_range = {{MMAL_PARAMETER_FPS_RANGE, sizeof(fps_range)},
{ 166, 1000 }, {999, 1000}};
mmal_port_parameter_set(preview_port, &fps_range.hdr);
}
if (state->fullResPreview)