-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmrcam.c
1122 lines (946 loc) · 35.5 KB
/
mrcam.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
#include <stdlib.h>
#include <stdio.h>
#include <arv.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <libswscale/swscale.h>
#include <libavutil/pixfmt.h>
#include <libavutil/imgutils.h>
#include "mrcam.h"
#include "util.h"
#define DEFINE_INTERNALS(ctx) \
ArvCamera** camera __attribute__((unused)) = (ArvCamera**)(&(ctx)->camera); \
ArvBuffer** buffer __attribute__((unused)) = (ArvBuffer**)(&(ctx)->buffer); \
ArvStream** stream __attribute__((unused)) = (ArvStream**)(&(ctx)->stream)
mrcam_output_type_t mrcam_output_type(mrcam_pixfmt_t pixfmt)
{
switch(pixfmt)
{
// I just check one; MRCAM_PIXFMT_name and ..._name_genicam are identical
#define CHOOSE(name, name_genicam, T, ...) case MRCAM_PIXFMT_ ## name: return MRCAM_ ## T;
LIST_MRCAM_PIXFMT(CHOOSE)
default: break;
#undef CHOOSE
}
MSG("Unknown pixfmt. This is a bug");
return MRCAM_UNKNOWN;
}
static
ArvPixelFormat pixfmt__ArvPixelFormat(mrcam_pixfmt_t pixfmt)
{
switch(pixfmt)
{
// I just check one; MRCAM_PIXFMT_name and ..._name_genicam are identical
#define CHOOSE(name, name_genicam, ...) case MRCAM_PIXFMT_ ## name: return ARV_PIXEL_FORMAT_ ## name;
LIST_MRCAM_PIXFMT(CHOOSE)
default:
MSG("ERROR: unknown mrcam_pixfmt_t = %d", (int)pixfmt);
return 0;
}
#undef CHOOSE
}
static
const char* pixfmt__name(mrcam_pixfmt_t pixfmt)
{
switch(pixfmt)
{
#define CHOOSE(name, name_genicam, ...) case MRCAM_PIXFMT_ ## name: return #name;
LIST_MRCAM_PIXFMT(CHOOSE)
default:
MSG("ERROR: unknown mrcam_pixfmt_t = %d", (int)pixfmt);
return "UNKNOWN";
}
#undef CHOOSE
}
static
const char* pixfmt__name_from_ArvPixelFormat(ArvPixelFormat pixfmt)
{
switch(pixfmt)
{
#define CHOOSE(name, name_genicam, ...) case ARV_PIXEL_FORMAT_ ## name: return #name;
LIST_MRCAM_PIXFMT(CHOOSE)
default:
MSG("ERROR: unknown ArvPixelFormat = %d", (int)pixfmt);
return "UNKNOWN";
}
#undef CHOOSE
}
static
bool is_little_endian(void)
{
union
{
uint16_t u16;
uint8_t u8[2];
} u = {.u16 = 1};
return u.u8[0] == 1;
}
// Returns the input/output pixel formats, as denoted by ffmpeg. AV_PIX_FMT_NONE
// means "the data already comes in fully unpacked; no ffmpeg processing is
// needed"
static
bool pixfmt__av_pixfmt(// input
enum AVPixelFormat* av_pixfmt_input,
enum AVPixelFormat* av_pixfmt_output,
// output
mrcam_pixfmt_t pixfmt)
{
// output
switch(mrcam_output_type(pixfmt))
{
case MRCAM_uint8:
*av_pixfmt_output = AV_PIX_FMT_GRAY8;
break;
case MRCAM_uint16:
*av_pixfmt_output = is_little_endian() ?
AV_PIX_FMT_GRAY16LE :
AV_PIX_FMT_GRAY16BE;
break;
case MRCAM_bgr:
*av_pixfmt_output = AV_PIX_FMT_BGR24;
break;
default:
MSG("Unknown pixfmt. This is a bug");
return false;
}
switch(pixfmt)
{
// I just check one; MRCAM_PIXFMT_name and ..._name_genicam are identical
#define CHOOSE(name, name_genicam, T, av_pixfmt) \
case MRCAM_PIXFMT_ ## name: \
*av_pixfmt_input = av_pixfmt; \
break;
LIST_MRCAM_PIXFMT(CHOOSE)
default:
MSG("ERROR: unknown mrcam_pixfmt_t = %d; this is a bug!",
(int)pixfmt);
return false;
#undef CHOOSE
}
return true;
}
static
int pixfmt__output_bytes_per_pixel(mrcam_pixfmt_t pixfmt)
{
switch(mrcam_output_type(pixfmt))
{
case MRCAM_uint8: return 1;
case MRCAM_uint16: return 2;
case MRCAM_bgr: return 3;
default: ;
}
return 0;
}
static bool open_serial_device(mrcam_t* ctx)
{
bool result = false;
// open the serial device, and make it as raw as possible
const char* device = "/dev/ttyS0";
const speed_t baud = B2400;
try( (ctx->fd_tty_trigger = open(device, O_WRONLY|O_NOCTTY)) >= 0 );
try( tcflush(ctx->fd_tty_trigger, TCIOFLUSH) == 0 );
struct termios options = {.c_iflag = IGNBRK,
.c_cflag = CS8 | CREAD | CLOCAL};
try(cfsetspeed(&options, baud) == 0);
try(tcsetattr(ctx->fd_tty_trigger, TCSANOW, &options) == 0);
result = true;
done:
return result;
}
static void
callback_arv(void* cookie, ArvStreamCallbackType type, ArvBuffer* buffer);
static bool
request(mrcam_t* ctx,
mrcam_callback_image_uint8_t* callback,
void* cookie);
static void report_available_pixel_formats(ArvCamera* camera,
mrcam_t* ctx)
{
GError* error = NULL;
const char** available_pixel_formats = NULL;
guint n_pixel_formats;
MSG("Available pixel formats supported by the camera:");
try_arv( available_pixel_formats =
arv_camera_dup_available_pixel_formats_as_strings(camera, &n_pixel_formats, &error) );
for(guint i=0; i<n_pixel_formats; i++)
MSG(" %s",
available_pixel_formats[i]);
done:
g_free(available_pixel_formats);
}
static bool
init_stream(mrcam_t* ctx)
{
bool result = false;
GError* error = NULL;
DEFINE_INTERNALS(ctx);
// The frame de-init happens in receive_image() after I captured the image.
// If I'm asynchronous, receive_image() happens from the callback_arv(),
// which is called from the stream thread, which means I cannot stop the
// stream thread in receive_image(). So I just let the stream thread run,
// and restart it when I need a new frame
g_clear_object(stream);
try_arv_and( *stream = arv_camera_create_stream(*camera,
callback_arv, ctx,
&error),
ARV_IS_STREAM(*stream) );
/*
For the Emergent HR-20000 cameras. I get lost packets if I don't do this.
Running as root is another workaround: it enables the "Packet socket
method". Either of these are needed in addition to the GevSCPSPacketSize
setting
Ultimately we need to set the SO_RCVBUF setting on the socket. Described
like this in socket(7):
SO_RCVBUF
Sets or gets the maximum socket receive buffer in bytes. The
kernel doubles this value (to allow space for bookkeeping
overhead) when it is set using setsockopt(2), and this doubled
value is returned by getsockopt(2). The default value is set by
the /proc/sys/net/core/rmem_default file, and the maximum allowed
value is set by the /proc/sys/net/core/rmem_max file. The minimum
(doubled) value for this option is 256.
The setting ends up in sk_rcvbuf in the kernel:
https://lxr.linux.no/#linux+v6.7.1/net/core/sock.c#L1250
https://lxr.linux.no/#linux+v6.7.1/net/core/sock.c#L960
Which is used in multiple places in
https://lxr.linux.no/#linux+v6.7.1/net/ipv4/udp.c
If the data comes in faster than it can be read, it's stored in this buffer;
if the buffer is too small, the packets are thrown away. The logic and
available diagnostics are in the udp.c file linked above. /proc/net/snmp
records the dropped packets and there's a "udp_fail_queue_rcv_skb"
tracepoint to catch some paths
In aravis it is sufficient to ask for an "auto" buffer size, and it will be
large-enough to hold a single buffer
*/
if(ARV_IS_GV_STREAM(*stream))
g_object_set (*stream,
"socket-buffer", ARV_GV_STREAM_SOCKET_BUFFER_AUTO,
NULL);
// If ARV_ACQUISITION_MODE_MULTI_FRAME, I ask for just one frame
if(ctx->acquisition_mode == MRCAM_ACQUISITION_MODE_MULTI_FRAME)
try_arv(arv_camera_set_integer(*camera, "AcquisitionFrameCount", 1, &error));
if(error != NULL)
g_clear_error(&error);
#define SET(name, ...) \
else if(ctx->acquisition_mode == MRCAM_ACQUISITION_MODE_ ## name) \
try_arv(arv_camera_set_acquisition_mode(*camera, ARV_ACQUISITION_MODE_ ## name, &error));
if(0); LIST_MRCAM_ACQUISITION_MODE(SET)
else
{
MSG("Unknown acquisition_mode mode '%d'; I know about:", (int)(ctx->acquisition_mode));
#define SAY(name, ...) MSG(" " #name);
LIST_MRCAM_ACQUISITION_MODE(SAY);
#undef SAY
goto done;
}
#undef SET
result = true;
done:
if(!result)
g_clear_object(stream);
return result;
}
// camera_name = NULL means "first available camera"
bool mrcam_init(// out
mrcam_t* ctx,
// in
const char* camera_name,
const mrcam_options_t* options)
{
bool result = false;
GError* error = NULL;
*ctx = (mrcam_t){ .recreate_stream_with_each_frame = options->recreate_stream_with_each_frame,
.pixfmt = options->pixfmt,
.trigger = options->trigger,
.acquisition_mode = options->acquisition_mode,
.verbose = options->verbose,
.time_decimation_factor = options->time_decimation_factor,
.fd_tty_trigger = -1};
DEFINE_INTERNALS(ctx);
if(ctx->trigger == MRCAM_TRIGGER_HARDWARE_TTYS0)
try(open_serial_device(ctx));
try_arv_and( *camera = arv_camera_new (camera_name,
&error),
ARV_IS_CAMERA(*camera) );
int width = options->width;
int height = options->height;
if(width <= 0 || height <= 0)
{
// Use WidthMax and HeightMax
gint dummy,_width,_height;
try_arv( arv_camera_get_width_bounds( *camera, &dummy, &_width, &error) );
try_arv( arv_camera_get_height_bounds(*camera, &dummy, &_height, &error) );
width = (int)_width;
height = (int)_height;
}
try_arv(arv_camera_set_integer(*camera, "Width", width, &error));
try_arv(arv_camera_set_integer(*camera, "Height", height, &error));
ArvPixelFormat arv_pixfmt = pixfmt__ArvPixelFormat(ctx->pixfmt);
if(arv_pixfmt == 0)
goto done;
try_arv_extra_reporting( arv_camera_set_pixel_format(*camera, arv_pixfmt, &error),
{
MSG(" Setting pixel format: '%s'", pixfmt__name(ctx->pixfmt));
},
{},
{
MSG("Couldn't set the requested pixel format: '%s'",
pixfmt__name(ctx->pixfmt));
report_available_pixel_formats(*camera, ctx);
});
// Some cameras start up with the test-pattern enabled. So I turn it off
// unconditionally. This setting doesn't exist on all cameras; if it
// doesn't, I ignore that failure
try_arv_or(arv_camera_set_string (*camera, "TestPattern", "Off", &error),
error->code == ARV_DEVICE_ERROR_FEATURE_NOT_FOUND);
if(error != NULL)
{
// No TestPattern setting exists. I ignore the error
g_clear_error(&error);
}
gint payload_size;
try_arv(payload_size = arv_camera_get_payload(*camera, &error));
try(*buffer = arv_buffer_new(payload_size, NULL));
enum AVPixelFormat av_pixfmt_input, av_pixfmt_output;
try(pixfmt__av_pixfmt(&av_pixfmt_input, &av_pixfmt_output,
ctx->pixfmt));
if(av_pixfmt_input == AV_PIX_FMT_NONE)
{
// The pixel format is already unpacked. We don't need to do any
// conversions
}
else
{
// We need to convert stuff. Use libswscale (ffmpeg) to set up the
// converter
try(NULL !=
(ctx->sws_context =
sws_getContext(// source
width,height,
av_pixfmt_input,
// destination
width,height,
av_pixfmt_output,
// misc stuff
SWS_POINT, NULL, NULL, NULL)));
try(NULL != (ctx->output_image_buffer =
malloc(pixfmt__output_bytes_per_pixel(ctx->pixfmt) *
width * height)));
}
// Set the triggering strategy
if(ctx->trigger != MRCAM_TRIGGER_NONE)
{
try_arv_or( arv_camera_set_string(*camera, "TriggerMode", "On", &error),
error->code == ARV_DEVICE_ERROR_FEATURE_NOT_FOUND );
if(error != NULL)
{
// No TriggerMode is available at all. I ignore the error. If there WAS
// a TriggerMode, and I couldn't set it to "On", then I DO flag an error
g_clear_error(&error);
}
// If either the feature or the requested enum don't exist, I let it go
try_arv_or( arv_camera_set_string(*camera, "TriggerSelector", "FrameStart", &error),
error->code == ARV_DEVICE_ERROR_FEATURE_NOT_FOUND ||
error->code == ARV_GC_ERROR_ENUM_ENTRY_NOT_FOUND );
if(error != NULL)
g_clear_error(&error);
}
if(ctx->trigger == MRCAM_TRIGGER_SOFTWARE)
{
try_arv_or( arv_camera_set_string(*camera, "TriggerSource", "Software", &error),
error->code == ARV_DEVICE_ERROR_FEATURE_NOT_FOUND ||
error->code == ARV_GC_ERROR_ENUM_ENTRY_NOT_FOUND );
if(error != NULL)
g_clear_error(&error);
}
else if(ctx->trigger == MRCAM_TRIGGER_HARDWARE_TTYS0 ||
ctx->trigger == MRCAM_TRIGGER_HARDWARE_EXTERNAL)
{
try_arv_or( arv_camera_set_string(*camera, "TriggerSource", "Line0", &error),
error->code == ARV_DEVICE_ERROR_FEATURE_NOT_FOUND ||
error->code == ARV_GC_ERROR_ENUM_ENTRY_NOT_FOUND );
if(error != NULL)
g_clear_error(&error);
}
else if(ctx->trigger == MRCAM_TRIGGER_NONE)
{}
else
{
MSG("Unknown trigger enum value '%d'", ctx->trigger);
goto done;
}
// High-res cameras need BIG packets to maintain the signal integrity. Here
// I ask for packaets 9kB in size; that's about the bigger we can have
try_arv_or( arv_camera_set_integer(*camera, "GevSCPSPacketSize", 9000, &error),
error->code == ARV_DEVICE_ERROR_FEATURE_NOT_FOUND );
if(error != NULL)
g_clear_error(&error);
if(!ctx->recreate_stream_with_each_frame)
if(!init_stream(ctx))
goto done;
result = true;
done:
if(!result)
{
mrcam_free(ctx);
// ctx is {} now
}
return result;
}
// deallocates everything, and sets all the pointers in ctx to NULL
void mrcam_free(mrcam_t* ctx)
{
DEFINE_INTERNALS(ctx);
g_clear_object(stream);
g_clear_object(buffer);
g_clear_object(camera);
if(ctx->sws_context)
{
sws_freeContext(ctx->sws_context);
ctx->sws_context = NULL;
}
free(ctx->output_image_buffer);
ctx->output_image_buffer = NULL;
if(ctx->fd_tty_trigger >= 0)
{
close(ctx->fd_tty_trigger);
ctx->fd_tty_trigger = -1;
}
}
bool mrcam_is_inited(mrcam_t* ctx)
{
DEFINE_INTERNALS(ctx);
return *camera != NULL;
}
static uint64_t gettimeofday_uint64()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (uint64_t) tv.tv_sec * 1000000ULL + (uint64_t) tv.tv_usec;
}
// Fill in the image. Assumes that the buffer has valid data
static
bool fill_image_unpacked(// out
mrcal_image_uint8_t* image, // type doesn't matter in this function
// in
ArvBuffer* buffer,
const size_t sizeof_pixel)
{
bool result = false;
size_t size;
image->width = arv_buffer_get_image_width (buffer);
image->height = arv_buffer_get_image_height(buffer);
image->data = (uint8_t*)arv_buffer_get_image_data(buffer, &size);
image->stride = image->width * sizeof_pixel;
// The payload is sometimes larger than expected, if the camera wants to pad
// the data. In that case I can simply ignore the trailing bits. If it's
// smaller than expected, however, then I don't know what to do, and I give
// up
if((size_t)(image->height*image->stride) > size)
{
MSG("Unexpected image dimensions: insufficient data returned. Expected buffersize = height*stride = height*width*bytes_per_pixel = %zd * %zd * %zd = %zd, but got %zd",
(size_t)(image->height), (size_t)(image->width), sizeof_pixel,
(size_t)(image->height*image->stride),
size);
goto done;
}
result = true;
done:
return result;
}
static
bool fill_image_swscale(// out
mrcal_image_uint8_t* image, // any type
// in
ArvBuffer* buffer,
mrcam_t* ctx)
{
bool result = false;
const unsigned int width = (unsigned int)arv_buffer_get_image_width (buffer);
const unsigned int height = (unsigned int)arv_buffer_get_image_height(buffer);
const int output_stride = width*pixfmt__output_bytes_per_pixel(ctx->pixfmt);
enum AVPixelFormat av_pixfmt_input, av_pixfmt_output;
try(pixfmt__av_pixfmt(&av_pixfmt_input, &av_pixfmt_output,
ctx->pixfmt));
size_t size;
const uint8_t* bytes_frame = (uint8_t*)arv_buffer_get_image_data(buffer, &size);
// This is overly-complex because ffmpeg supports lots of weird
// pixel formats, including planar ones
int scale_stride_value[4];
uint8_t* scale_source_value[4];
try(0 < av_image_fill_arrays(scale_source_value, scale_stride_value,
bytes_frame,
av_pixfmt_input,
width,height,
1) );
const int input_stride = scale_stride_value[0];
// The payload is sometimes larger than expected, if the camera wants to pad
// the data. In that case I can simply ignore the trailing bits. If it's
// smaller than expected, however, then I don't know what to do, and I give
// up
if((size_t)(height*input_stride) > size)
{
MSG("Unexpected image dimensions: insufficient data returned. Expected buffersize = height*stride = %zd * %zd = %zd, but got %zd",
(size_t)(height), (size_t)(input_stride),
(size_t)(height)* (size_t)(input_stride),
size);
goto done;
}
sws_scale(ctx->sws_context,
// source
(const uint8_t*const*)scale_source_value,
scale_stride_value,
0, height,
// destination buffer, stride
(uint8_t*const*)&ctx->output_image_buffer,
&output_stride);
image->width = width;
image->height = height;
image->data = (uint8_t*)ctx->output_image_buffer;
image->stride = output_stride;
result = true;
done:
return result;
}
static bool is_pixfmt_matching(ArvPixelFormat pixfmt,
mrcam_pixfmt_t mrcam_pixfmt)
{
#define CHECK(name, name_genicam, T, ...) if(pixfmt == ARV_PIXEL_FORMAT_ ## name && mrcam_pixfmt == MRCAM_PIXFMT_ ## name) return true;
LIST_MRCAM_PIXFMT(CHECK);
#undef CHECK
MSG("Mismatched pixel format! I asked for '%s', but got ArvPixelFormat 0x%x",
pixfmt__name(mrcam_pixfmt), (unsigned int)pixfmt);
return false;
}
static
bool fill_image_uint8(// out
mrcal_image_uint8_t* image,
// in
mrcam_t* ctx)
{
if(ctx->verbose) MSG("%s()", __func__);
DEFINE_INTERNALS(ctx);
ArvPixelFormat pixfmt = arv_buffer_get_image_pixel_format(*buffer);
if(!is_pixfmt_matching(pixfmt, ctx->pixfmt))
return false;
if(mrcam_output_type(ctx->pixfmt) != MRCAM_uint8)
{
MSG("%s() unexpected image type", __func__);
return false;
}
return fill_image_unpacked((mrcal_image_uint8_t*)image, *buffer, sizeof(uint8_t));
}
static
bool fill_image_uint16(// out
mrcal_image_uint16_t* image,
// in
mrcam_t* ctx)
{
if(ctx->verbose) MSG("%s()", __func__);
DEFINE_INTERNALS(ctx);
ArvPixelFormat pixfmt = arv_buffer_get_image_pixel_format(*buffer);
if(!is_pixfmt_matching(pixfmt, ctx->pixfmt))
return false;
if(mrcam_output_type(ctx->pixfmt) != MRCAM_uint16)
{
MSG("%s() unexpected image type", __func__);
return false;
}
return fill_image_unpacked((mrcal_image_uint8_t*)image, *buffer, sizeof(uint16_t));
}
static
bool fill_image_bgr(// out
mrcal_image_bgr_t* image,
// in
mrcam_t* ctx)
{
if(ctx->verbose) MSG("%s()", __func__);
DEFINE_INTERNALS(ctx);
ArvPixelFormat pixfmt = arv_buffer_get_image_pixel_format(*buffer);
if(!is_pixfmt_matching(pixfmt, ctx->pixfmt))
return false;
if(mrcam_output_type(ctx->pixfmt) != MRCAM_bgr)
{
MSG("%s() unexpected image type", __func__);
return false;
}
const size_t sizeof_pixel = 3;
bool result = false;
// I have SOME color format. Today I don't actually support them all, and I
// put what I have so far
if(pixfmt == ARV_PIXEL_FORMAT_BGR_8_PACKED)
return fill_image_unpacked((mrcal_image_uint8_t*)image, *buffer, sizeof(mrcal_bgr_t));
if(ctx->sws_context != NULL)
{
if(!fill_image_swscale((mrcal_image_uint8_t*)image,
*buffer, ctx))
goto done;
result = true;
goto done;
}
MSG("%s() doesn't yet know how to handle pixfmt '%s'",
__func__,
pixfmt__name_from_ArvPixelFormat(pixfmt));
goto done;
done:
return result;
}
// meant to be called after request()
static
bool receive_image(// out
uint64_t* timestamp_us,
// in
const uint64_t timeout_us,
mrcam_t* ctx)
{
if(ctx->verbose) MSG("%s(timeout_us=%"PRIu64")", __func__, timeout_us);
DEFINE_INTERNALS(ctx);
bool result = false;
GError* error = NULL;
ArvBuffer* buffer_here = NULL;
if(!ctx->acquiring)
{
MSG("No acquisition in progress. Nothing to receive");
goto done;
}
if(ctx->verbose)
{
if (timeout_us > 0) MSG("Evaluating arv_stream_timeout_pop_buffer(timeout_us = %"PRIu64")", timeout_us);
else MSG("Evaluating arv_stream_pop_buffer()");
}
// May block. If we don't want to block, it's our job to make sure to have
// called receive_image() when an output image has already been buffered
if (timeout_us > 0) buffer_here = arv_stream_timeout_pop_buffer(*stream, timeout_us);
else buffer_here = arv_stream_pop_buffer (*stream);
// This MUST have been done by this function, regardless of things failing
if(ctx->acquisition_mode != MRCAM_ACQUISITION_MODE_CONTINUOUS)
{
try_arv(arv_camera_stop_acquisition(*camera, &error));
// if it failed, ctx->acquiring will remain at true. Probably there's no way
// to recover anyway
ctx->acquiring = false;
}
try(buffer_here == *buffer);
try(ARV_IS_BUFFER(*buffer));
ArvBufferStatus status = arv_buffer_get_status(*buffer);
// All the statuses from arvbuffer.h, as of aravis 0.8.29
#define LIST_STATUS(_) \
_(ARV_BUFFER_STATUS_UNKNOWN) \
_(ARV_BUFFER_STATUS_SUCCESS) \
_(ARV_BUFFER_STATUS_CLEARED) \
_(ARV_BUFFER_STATUS_TIMEOUT) \
_(ARV_BUFFER_STATUS_MISSING_PACKETS) \
_(ARV_BUFFER_STATUS_WRONG_PACKET_ID) \
_(ARV_BUFFER_STATUS_SIZE_MISMATCH) \
_(ARV_BUFFER_STATUS_FILLING) \
_(ARV_BUFFER_STATUS_ABORTED)
#define CHECK(s) else if(status == s) { MSG("ERROR: arv_stream_pop_buffer() says " #s); goto done; }
if(status == ARV_BUFFER_STATUS_SUCCESS) ;
LIST_STATUS(CHECK)
else
{
MSG("ERROR: arv_stream_pop_buffer() returned unknown status %d", status);
goto done;
}
#undef LIST_STATUS
#undef CHECK
ArvBufferPayloadType payload_type = arv_buffer_get_payload_type(*buffer);
// All the payload_types from arvbuffer.h, as of aravis 0.8.30
#define LIST_PAYLOAD_TYPE(_) \
_(ARV_BUFFER_PAYLOAD_TYPE_UNKNOWN) \
_(ARV_BUFFER_PAYLOAD_TYPE_NO_DATA) \
_(ARV_BUFFER_PAYLOAD_TYPE_IMAGE) \
_(ARV_BUFFER_PAYLOAD_TYPE_RAWDATA) \
_(ARV_BUFFER_PAYLOAD_TYPE_FILE) \
_(ARV_BUFFER_PAYLOAD_TYPE_CHUNK_DATA) \
_(ARV_BUFFER_PAYLOAD_TYPE_EXTENDED_CHUNK_DATA) \
_(ARV_BUFFER_PAYLOAD_TYPE_JPEG) \
_(ARV_BUFFER_PAYLOAD_TYPE_JPEG2000) \
_(ARV_BUFFER_PAYLOAD_TYPE_H264) \
_(ARV_BUFFER_PAYLOAD_TYPE_MULTIZONE_IMAGE) \
_(ARV_BUFFER_PAYLOAD_TYPE_MULTIPART)
#define CHECK(s) else if(payload_type == s) { MSG("ERROR: arv_stream_pop_buffer() says " #s "; I only know about ARV_BUFFER_PAYLOAD_TYPE_IMAGE"); goto done; }
if(payload_type == ARV_BUFFER_PAYLOAD_TYPE_IMAGE) ;
LIST_PAYLOAD_TYPE(CHECK)
else
{
MSG("ERROR: arv_stream_pop_buffer() returned unknown payload_type %d", payload_type);
goto done;
}
#undef LIST_PAYLOAD_TYPE
#undef CHECK
*timestamp_us = gettimeofday_uint64();
result = true;
done:
// I want to make sure that arv_camera_stop_acquisition() was called by this
// function. That happened on top. If it failed, there isn't anything I can
// do about it.
return result;
}
static void
callback_arv(void* cookie, ArvStreamCallbackType type, ArvBuffer* buffer)
{
mrcam_t* ctx = (mrcam_t*)cookie;
// This is going to be called from a different thread than the rest of the
// application. The sequence SHOULD be:
//
// - request()
// - internal machinery causes this callback_arv() to be called
// - this callback does its thing to call receive_image(), which should
// disable future callbacks until the next request
//
// Something COULD break this though. Extra calls of callback_arv() should
// be benign: ctx->active_callback == NULL will be true. Insufficient calls
// of callback_arv() will cause us to wait forever for the callback, and
// will permanently break stuff. We'll see if this happens.
//
// Furthermore, while this function is called from another thread, it was
// set up to work synchronously, so no locking of any sort should be needed.
// We'll see if that happens also
if(ctx->active_callback == NULL)
return;
switch (type)
{
case ARV_STREAM_CALLBACK_TYPE_INIT:
if(ctx->verbose)
MSG("ARV_STREAM_CALLBACK_TYPE_INIT: Stream thread started");
/* Here you may want to change the thread priority arv_make_thread_realtime() or
* arv_make_thread_high_priority() */
break;
case ARV_STREAM_CALLBACK_TYPE_START_BUFFER:
if(ctx->verbose)
MSG("ARV_STREAM_CALLBACK_TYPE_START_BUFFER: The first packet of a new frame was received");
break;
case ARV_STREAM_CALLBACK_TYPE_BUFFER_DONE:
/* The buffer is received, successfully or not. It is already pushed in
* the output FIFO.
*
* You could here signal the new buffer to another thread than the main
* one, and pull/push the buffer from this another thread.
*
* Or use the buffer here. We need to pull it, process it, then push it
* back for reuse by the stream receiving thread */
{
if(ctx->verbose)
MSG("ARV_STREAM_CALLBACK_TYPE_BUFFER_DONE");
// type may not be right; it doesn't matter
mrcal_image_uint8_t image = (mrcal_image_uint8_t){};
uint64_t timestamp_us = 0;
if( receive_image(×tamp_us,
0, ctx) )
{
switch(mrcam_output_type(ctx->pixfmt))
{
case MRCAM_uint8:
if(!fill_image_uint8((mrcal_image_uint8_t*)&image, ctx))
image = (mrcal_image_uint8_t){}; // indicate error
break;
case MRCAM_uint16:
if(!fill_image_uint16((mrcal_image_uint16_t*)&image, ctx))
image = (mrcal_image_uint8_t){}; // indicate error
break;
case MRCAM_bgr:
if(!fill_image_bgr((mrcal_image_bgr_t*)&image, ctx))
image = (mrcal_image_uint8_t){}; // indicate error
break;
default:
MSG("Unknown pixfmt. This is a bug");
}
}
// On error image is {0}, which indicates an error. We invoke the
// callback regardless. I want to make sure that the caller can be
// sure to expect ONE callback with each request
if(ctx->time_decimation_factor <= 1 ||
++ctx->time_decimation_index == ctx->time_decimation_factor)
{
MSG("decimated callback");
ctx->active_callback(image, timestamp_us, ctx->active_callback_cookie);
ctx->active_callback = NULL;
ctx->time_decimation_index = 0;
}
else
{
ctx->active_callback = NULL;
request(ctx,
ctx->active_callback,
ctx->active_callback_cookie);
}
}
break;
case ARV_STREAM_CALLBACK_TYPE_EXIT:
if(ctx->verbose)
MSG("ARV_STREAM_CALLBACK_TYPE_EXIT");
break;
}
}
static
bool request(mrcam_t* ctx,
mrcam_callback_image_uint8_t* callback,
void* cookie)
{
if(ctx->verbose) MSG("%s()", __func__);
DEFINE_INTERNALS(ctx);
bool result = false;
GError* error = NULL;
if((ctx->acquiring && !ctx->acquiring_continuous) || ctx->active_callback != NULL)
{
MSG("Acquisition already in progress: acquiring=%d, active_callback_exists=%d. If mrcam_request_...() was called, wait for the callback or call mrcam_cancel_request()",
ctx->acquiring, !!ctx->active_callback);
goto done;
}
if(ctx->recreate_stream_with_each_frame)
if(!init_stream(ctx))
goto done;
ctx->active_callback = callback;
ctx->active_callback_cookie = cookie;
if(ctx->verbose)
MSG("arv_stream_push_buffer()");
arv_stream_push_buffer(*stream, *buffer);
if(ctx->acquisition_mode != MRCAM_ACQUISITION_MODE_CONTINUOUS ||
!ctx->acquiring_continuous)
{
try_arv( arv_camera_start_acquisition(*camera, &error));
ctx->acquiring = true;
if(ctx->acquisition_mode == MRCAM_ACQUISITION_MODE_CONTINUOUS)
ctx->acquiring_continuous = true;
}
if(ctx->trigger == MRCAM_TRIGGER_SOFTWARE)
{
// For the Emergent HR-20000 cameras; the others should be able to
// free-run. If the feature doesn't exist, I let it go
try_arv_or(arv_camera_execute_command(*camera, "TriggerSoftware", &error),
error->code == ARV_DEVICE_ERROR_FEATURE_NOT_FOUND );
if(error != NULL)
g_clear_error(&error);
}
else if(ctx->trigger == MRCAM_TRIGGER_HARDWARE_TTYS0)
{
// If the previous trigger pulse is still high for some reason, wait for
// it to drop
try(0 == tcdrain(ctx->fd_tty_trigger));
try(1 == write(ctx->fd_tty_trigger, &((char){'\xff'}), 1));
}
else if(ctx->trigger == MRCAM_TRIGGER_HARDWARE_EXTERNAL)
{
// A trigger signal will magically come from somewhere. I don't worry
// about it here; nothing to do
}
else if(ctx->trigger == MRCAM_TRIGGER_NONE)
{}
else
{
MSG("Unknown trigger enum value '%d'", ctx->trigger);
goto done;
}
result = true;