forked from ROCm/MIVisionX
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlive_stitch_api.cpp
4063 lines (3927 loc) · 226 KB
/
live_stitch_api.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
/*
Copyright (c) 2015 - 2022 Advanced Micro Devices, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#define _CRT_SECURE_NO_WARNINGS
#include "profiler.h"
#include "kernels.h"
#include "lens_distortion_remap.h"
#include "warp.h"
#include "merge.h"
#include "seam_find.h"
#include "exposure_compensation.h"
#include "multiband_blender.h"
#include <sstream>
#include <stdarg.h>
#include <map>
#include <string>
// Version
#define LS_VERSION "0.9.9"
//////////////////////////////////////////////////////////////////////
//! \brief The magic number for validation
#define LIVE_STITCH_MAGIC 0x600df00d
//////////////////////////////////////////////////////////////////////
//! \brief The stitching modes
enum {
stitching_mode_normal = 0, // normal mode
stitching_mode_quick_and_dirty = 1, // quick and dirty mode
};
//////////////////////////////////////////////////////////////////////
//! \brief The LoomIO module info
#define LOOMIO_MAX_LENGTH_MODULE_NAME 64
#define LOOMIO_MAX_LENGTH_KERNEL_NAME VX_MAX_KERNEL_NAME
#define LOOMIO_MAX_LENGTH_KERNEL_ARGUMENTS 1024
#define LOOMIO_MIN_AUX_DATA_CAPACITY 256
#define LOOMIO_DEFAULT_AUX_DATA_CAPACITY 1024
struct ls_loomio_info {
char module[LOOMIO_MAX_LENGTH_MODULE_NAME];
char kernelName[LOOMIO_MAX_LENGTH_KERNEL_NAME];
char kernelArguments[LOOMIO_MAX_LENGTH_KERNEL_ARGUMENTS];
};
//////////////////////////////////////////////////////////////////////
//! \brief The internal table buffer sizes
struct ls_internal_table_size_info {
vx_size warpTableSize;
vx_size expCompOverlapTableSize;
vx_size expCompValidTableSize;
vx_size blendOffsetTableSize;
vx_size seamFindValidTableSize;
vx_size seamFindWeightTableSize;
vx_size seamFindAccumTableSize;
vx_size seamFindPrefInfoTableSize;
vx_size seamFindPathTableSize;
};
//////////////////////////////////////////////////////////////////////
//! \brief The stitch handle
struct ls_context_t {
// magic word and status
int magic; // should be LIVE_STITCH_MAGIC
bool feature_enable_reinitialize; // true if reinitialize feature is enabled
bool initialized; // true if initialized
bool scheduled; // true if scheduled
bool reinitialize_required; // true if reinitialize required
bool rig_params_updated; // true if rig parameters updated
bool camera_params_updated; // true if camera parameters updated
bool overlay_params_updated; // true if overlay parameters updated
// configuration parameters
vx_int32 stitching_mode; // stitching mode
vx_uint32 num_cameras; // number of cameras
vx_uint32 num_camera_rows; // camera buffer number of rows
vx_uint32 num_camera_columns; // camera buffer number of cols
vx_df_image camera_buffer_format; // camera buffer format (VX_DF_IMAGE_UYVY/YUYV/RGB)
vx_uint32 camera_buffer_width; // camera buffer width
vx_uint32 camera_buffer_height; // camera buffer height
camera_params * camera_par; // individual camera parameters
vx_uint32 camera_rgb_buffer_width; // camera buffer width after color conversion
vx_uint32 camera_rgb_buffer_height; // camera buffer height after color conversion
vx_uint32 num_overlays; // number of overlays
vx_uint32 num_overlay_rows; // overlay buffer width
vx_uint32 num_overlay_columns; // overlay buffer height
vx_uint32 overlay_buffer_width; // overlay buffer width
vx_uint32 overlay_buffer_height; // overlay buffer height
camera_params * overlay_par; // individual overlay parameters
rig_params rig_par; // rig parameters
vx_uint32 output_buffer_width; // output equirectangular image width
vx_uint32 output_buffer_height; // output equirectangular image height
vx_df_image output_buffer_format; // output image format (VX_DF_IMAGE_UYVY/YUYV/RGB/NV12/IYUV)
vx_uint32 output_rgb_buffer_width; // camera buffer width after color conversion
vx_uint32 output_rgb_buffer_height; // camera buffer height after color conversion
cl_context opencl_context; // OpenCL context for DGMA interop
vx_uint32 camera_buffer_stride_in_bytes; // stride of each row in input opencl buffer
vx_uint32 overlay_buffer_stride_in_bytes; // stride of each row in overlay opencl buffer (optional)
vx_uint32 output_buffer_stride_in_bytes; // stride of each row in output opencl buffer
// global options
vx_uint32 EXPO_COMP, SEAM_FIND; // exposure comp seam find flags from environment variable
vx_uint32 SEAM_COST_SELECT; // seam find cost generation flag from environment variable
vx_uint32 SEAM_REFRESH, SEAM_FLAGS; // seamfind seam refresh flag from environment variable
vx_uint32 MULTIBAND_BLEND; // multiband blend flag from environment variable
vx_uint32 EXPO_COMP_GAINW, EXPO_COMP_GAINH;// exposure comp module gain image width and height
vx_uint32 EXPO_COMP_GAINC; // exposure comp gain array number of gain values per camera. For mode 4, this should be 12 which is default if not specified.
// global OpenVX objects
bool context_is_external; // To avoid releaseing external OpenVX context
vx_context context; // OpenVX context
vx_graph graphStitch; // OpenVX graph for stitching
// internal buffer sizes
ls_internal_table_size_info table_sizes; // internal table sizes
vx_image rgb_input, rgb_output; // internal images
// data objects
vx_remap overlay_remap; // remap table for overlay
vx_remap camera_remap; // remap table for camera (in simple stitch mode)
vx_image Img_input, Img_output, Img_overlay;
vx_image Img_input_rgb, Img_output_rgb, Img_overlay_rgb, Img_overlay_rgba;
vx_node InputColorConvertNode, SimpleStitchRemapNode, OutputColorConvertNode;
vx_array ValidPixelEntry, WarpRemapEntry, OverlapPixelEntry, valid_array, gain_array;
vx_matrix overlap_matrix, A_matrix;
vx_image RGBY1, RGBY2, weight_image, cam_id_image, group1_image, group2_image;
vx_node WarpNode, ExpcompComputeGainNode, ExpcompSolveGainNode, ExpcompApplyGainNode, MergeNode;
vx_node nodeOverlayRemap, nodeOverlayBlend;
vx_float32 alpha, beta; // needed for expcomp
vx_int32 * A_matrix_initial_value; // needed for expcomp
// seamfind data & node elements
vx_array overlap_rect_array, seamfind_valid_array, seamfind_weight_array, seamfind_accum_array,
seamfind_pref_array, seamfind_info_array, seamfind_path_array, seamfind_scene_array;
vx_image valid_mask_image, warp_luma_image, sobelx_image, sobely_image, sobel_magnitude_s16_image,
sobel_magnitude_image, sobel_phase_image, seamfind_weight_image;
vx_node SobelNode, MagnitudeNode, PhaseNode, ConvertDepthNode, SeamfindStep1Node, SeamfindStep2Node,
SeamfindStep3Node, SeamfindStep4Node, SeamfindStep5Node, SeamfindAnalyzeNode;
vx_scalar current_frame, scene_threshold, seam_cost_enable;
vx_int32 current_frame_value;
vx_uint32 scene_threshold_value, SEAM_FIND_TARGET;
// multiband data elements
vx_int32 num_bands;
vx_array blend_offsets;
vx_image blend_mask_image;
StitchMultibandData * pStitchMultiband;
vx_size * multibandBlendOffsetIntoBuffer;
// LoomIO support
vx_uint32 loomioOutputAuxSelection, loomioCameraAuxDataLength, loomioOverlayAuxDataLength, loomioOutputAuxDataLength;
vx_scalar cameraMediaConfig, overlayMediaConfig, outputMediaConfig, viewingMediaConfig;
vx_array loomioCameraAuxData, loomioOverlayAuxData, loomioOutputAuxData, loomioViewingAuxData;
vx_node nodeLoomIoCamera, nodeLoomIoOverlay, nodeLoomIoOutput, nodeLoomIoViewing;
ls_loomio_info loomio_camera, loomio_output, loomio_overlay, loomio_viewing;
FILE * loomioAuxDumpFile;
// internal buffers for input camera lens models
vx_uint32 paddingPixelCount, overlapCount;
StitchCoord2dFloat * camSrcMap;
vx_float32 * camIndexTmpBuf;
vx_uint8 * camIndexBuf;
vx_uint32 * validPixelCamMap, *paddedPixelCamMap;
vx_rectangle_t * overlapRectBuf;
vx_rectangle_t * overlapValid[LIVE_STITCH_MAX_CAMERAS], *overlapPadded[LIVE_STITCH_MAX_CAMERAS];
vx_uint32 validCamOverlapInfo[LIVE_STITCH_MAX_CAMERAS], paddedCamOverlapInfo[LIVE_STITCH_MAX_CAMERAS];
vx_int32 * overlapMatrixBuf;
// internal buffers for overlay models
StitchCoord2dFloat * overlaySrcMap;
vx_uint32 * validPixelOverlayMap;
vx_float32 * overlayIndexTmpBuf;
vx_uint8 * overlayIndexBuf;
// internal buffers for frame encode
#define MAX_TILE_IMG 16
vx_uint32 output_encode_buffer_width; // buffer width after encode conversion
vx_uint32 output_encode_buffer_height; // buffer height after encode conversion
vx_uint32 output_encode_tiles; // total number of encode tiles
vx_uint32 num_encode_sections; // total number of encode sectional images
vx_image encode_src_rgb_imgs[MAX_TILE_IMG]; // encode intermediate images
vx_image encode_dst_imgs[MAX_TILE_IMG]; // encode intermediate images
vx_image encodetileOutput[MAX_TILE_IMG]; // encode tile output NV12 images
vx_rectangle_t src_encode_tile_rect[MAX_TILE_IMG]; // src encode rectangles
vx_rectangle_t dst_encode_tile_rect[MAX_TILE_IMG]; // dst encode rectangles
vx_node encode_color_convert_nodes[MAX_TILE_IMG];// nodes to color convert each of the sectional ROI images
// chroma key
vx_uint32 CHROMA_KEY; // chroma key flag variable
vx_uint32 CHROMA_KEY_EED; // chroma key flag variable
vx_image chroma_key_input_img; // chroma key input RGB intermediate images
vx_image chroma_key_mask_img; // chroma key U8 mask intermediate images
vx_image chroma_key_input_RGB_img; // intermediate images
vx_image chroma_key_dilate_mask_img; // chroma key U8 mask dilate intermediate images
vx_image chroma_key_erode_mask_img; // chroma key U8 mask dilate intermediate images
vx_node chromaKey_mask_generation_node; // nodes to generate chroma key mask
vx_node chromaKey_dilate_node; // nodes to dilate chroma key mask
vx_node chromaKey_erode_node; // nodes to erode chroma key mask
vx_node chromaKey_merge_node; // nodes to merge chroma input and stitch output
// temporal filter
vx_uint32 NOISE_FILTER; // temporal noise filter enable/disable environment variable
vx_float32 noiseFilterLambda; // temporal noise filter variable
vx_scalar filterLambda; // temporal noise filter scalar lambda variable from user
vx_delay noiseFilterImageDelay; // temporal noise filter delay element
vx_image noiseFilterInput_image; // temporal noise filter delay input image
vx_node noiseFilterNode; // temporal noise filter node
// quick setup load
vx_uint32 SETUP_LOAD; // quick setup load flag variable
vx_bool SETUP_LOAD_FILES_FOUND; // quick setup load files found flag variable
// data for Initialize tables
vx_uint32 USE_CPU_INIT;
StitchInitializeData *stitchInitData;
// attributes
vx_float32 live_stitch_attr[LIVE_STITCH_ATTR_MAX_COUNT];
};
//////////////////////////////////////////////////////////////////////
//! \brief The global attributes with default values
static bool g_live_stitch_attr_initialized = false;
static vx_float32 g_live_stitch_attr[LIVE_STITCH_ATTR_MAX_COUNT] = { 0 };
static stitch_log_callback_f g_live_stitch_log_message_callback = nullptr;
//////////////////////////////////////////////////////////////////////
//! \brief The macro for object creation error checking and reporting.
#define ERROR_CHECK_OBJECT_(call) { vx_reference obj = (vx_reference)(call); vx_status status = vxGetStatus(obj); if(status != VX_SUCCESS) { ls_printf("ERROR: OpenVX object creation failed at " __FILE__ "#%d\n", __LINE__); return status; } }
//! \brief The macro for status error checking and reporting.
#define ERROR_CHECK_STATUS_(call) {vx_status status = (call); if(status != VX_SUCCESS) { ls_printf("ERROR: OpenVX call failed with status = (%d) at " __FILE__ "#%d\n", status, __LINE__); return status; } }
//! \brief The macro for type error checking and reporting.
#define ERROR_CHECK_TYPE_(call) { vx_enum type_ = (call); if(type_ == VX_TYPE_INVALID) { ls_printf("ERROR: OpenVX call failed with type = VX_TYPE_INVALID at " __FILE__ "#%d\n", __LINE__); return VX_ERROR_NOT_SUFFICIENT; } }
//! \brief The macro for object creation error checking and reporting.
#define ERROR_CHECK_ALLOC_(call) { void * obj = (call); if(!obj) { ls_printf("ERROR: memory allocation failed at " __FILE__ "#%d\n", __LINE__); return VX_ERROR_NOT_ALLOCATED; } }
//! \brief The macro for fread error checking and reporting.
#define ERROR_CHECK_FREAD_(call,value) {size_t retVal = (call); if(retVal != (size_t)value) { ls_printf("ERROR: fread call expected to return [ %d elements ] but returned [ %d elements ] at " __FILE__ "#%d\n", (int)value, (int)retVal, __LINE__); return VX_FAILURE; } }
//! \brief The log callback.
void ls_printf(const char * format, ...)
{
char buffer[1024];
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer) - 1, format, args);
if (g_live_stitch_log_message_callback) {
g_live_stitch_log_message_callback(buffer);
}
else {
printf("%s", buffer);
fflush(stdout);
}
va_end(args);
}
//! \brief The log callback.
static void VX_CALLBACK log_callback(vx_context context, vx_reference ref, vx_status status, const vx_char string[])
{
if (g_live_stitch_log_message_callback) {
g_live_stitch_log_message_callback(string);
if (!(string[0] && string[strlen(string) - 1] == '\n')) g_live_stitch_log_message_callback("\n");
}
else {
printf("LOG:[status=%d] %s", status, string);
if (!(string[0] && string[strlen(string) - 1] == '\n')) printf("\n");
fflush(stdout);
}
}
//! \brief Dump utilities.
vx_status DumpBuffer(const vx_uint8 * buf, vx_size size, const char * fileName)
{
FILE * fp = fopen(fileName, "wb");
if (!fp) {
printf("ERROR: DumpBuffer: unable to create: %s\n", fileName);
if (fp != NULL) fclose(fp);
return VX_FAILURE;
}
fwrite(buf, size, 1, fp);
fclose(fp);
printf("OK: DumpBuffer: %d bytes into %s\n", (int)size, fileName);
return VX_SUCCESS;
}
vx_status DumpImage(vx_image img, const char * fileName)
{
FILE * fp = fopen(fileName, "wb");
if (!fp) {
printf("ERROR: DumpImage: unable to create: %s\n", fileName);
if (fp != NULL) fclose(fp);
return VX_FAILURE;
}
vx_df_image format = VX_DF_IMAGE_VIRT;
vx_size num_planes = 0;
vx_rectangle_t rectFull = { 0, 0, 0, 0 };
int stride_y;
ERROR_CHECK_STATUS(vxQueryImage(img, VX_IMAGE_ATTRIBUTE_FORMAT, &format, sizeof(format)));
ERROR_CHECK_STATUS(vxQueryImage(img, VX_IMAGE_ATTRIBUTE_PLANES, &num_planes, sizeof(num_planes)));
ERROR_CHECK_STATUS(vxQueryImage(img, VX_IMAGE_ATTRIBUTE_WIDTH, &rectFull.end_x, sizeof(rectFull.end_x)));
ERROR_CHECK_STATUS(vxQueryImage(img, VX_IMAGE_ATTRIBUTE_HEIGHT, &rectFull.end_y, sizeof(rectFull.end_y)));
// write all image planes from vx_image
for (vx_uint32 plane = 0; plane < (vx_uint32)num_planes; plane++){
vx_imagepatch_addressing_t addr = { 0 };
vx_uint8 * src = NULL;
ERROR_CHECK_STATUS(vxAccessImagePatch(img, &rectFull, plane, &addr, (void **)&src, VX_READ_ONLY));
vx_size width = (addr.dim_x * addr.scale_x) / VX_SCALE_UNITY;
vx_size width_in_bytes = (format == VX_DF_IMAGE_U1_AMD) ? ((width + 7) >> 3) : (width * addr.stride_x);
stride_y = addr.stride_y;
for (vx_uint32 y = 0; y < addr.dim_y; y += addr.step_y){
vx_uint8 *srcp = (vx_uint8 *)vxFormatImagePatchAddress2d(src, 0, y, &addr);
fwrite(srcp, 1, width_in_bytes, fp);
}
ERROR_CHECK_STATUS(vxCommitImagePatch(img, &rectFull, plane, &addr, src));
}
fclose(fp);
printf("OK: Dump: Image %dx%d of stride %d %4.4s image into %s\n", rectFull.end_x, rectFull.end_y, stride_y, (const char *)&format, fileName);
return VX_SUCCESS;
}
vx_status DumpArray(vx_array arr, const char * fileName)
{
FILE * fp = fopen(fileName, "wb"); if (!fp) {
printf("ERROR: DumpArray: unable to create: %s\n", fileName);
if (fp != NULL) fclose(fp);
return VX_FAILURE;
}
vx_size numItems, itemSize;
ERROR_CHECK_STATUS_(vxQueryArray(arr, VX_ARRAY_ITEMSIZE, &itemSize, sizeof(itemSize)));
ERROR_CHECK_STATUS_(vxQueryArray(arr, VX_ARRAY_NUMITEMS, &numItems, sizeof(numItems)));
vx_map_id map_id;
vx_uint8 * ptr;
vx_size stride;
ERROR_CHECK_STATUS_(vxMapArrayRange(arr, 0, numItems, &map_id, &stride, (void **)&ptr, VX_READ_ONLY, VX_MEMORY_TYPE_HOST, VX_NOGAP_X));
fwrite(ptr, itemSize, numItems, fp);
ERROR_CHECK_STATUS_(vxUnmapArrayRange(arr, map_id));
fclose(fp);
printf("OK: Dump: Array [%d][%d] into %s\n", (int)numItems, (int)itemSize, fileName);
return VX_SUCCESS;
}
static vx_status DumpMatrix(vx_matrix mat, const char * fileName)
{
FILE * fp = fopen(fileName, "wb");
if (!fp) {
printf("ERROR: DumpMatrix: unable to create: %s\n", fileName);
if (fp != NULL) fclose(fp);
return VX_FAILURE;
}
vx_size size;
ERROR_CHECK_STATUS_(vxQueryMatrix(mat, VX_MATRIX_SIZE, &size, sizeof(size)));
vx_uint8 * buf = new vx_uint8[size];
ERROR_CHECK_STATUS_(vxCopyMatrix(mat, buf, VX_READ_ONLY, VX_MEMORY_TYPE_HOST));
fwrite(buf, size, 1, fp);
delete[] buf;
fclose(fp);
vx_size rows, columns;
ERROR_CHECK_STATUS_(vxQueryMatrix(mat, VX_MATRIX_ROWS, &rows, sizeof(rows)));
ERROR_CHECK_STATUS_(vxQueryMatrix(mat, VX_MATRIX_COLUMNS, &columns, sizeof(columns)));
printf("OK: Dump: Matrix %dx%d (%d bytes) into %s\n", (int)rows, (int)columns, (int)size, fileName);
return VX_SUCCESS;
}
static vx_status DumpRemap(vx_remap remap, const char * fileName)
{
FILE * fp = fopen(fileName, "wb");
if (!fp) {
printf("ERROR: DumpRemap: unable to create: %s\n", fileName);
if (fp != NULL) fclose(fp);
return VX_FAILURE;
}
vx_uint32 dstWidth, dstHeight;
ERROR_CHECK_STATUS_(vxQueryRemap(remap, VX_REMAP_DESTINATION_WIDTH, &dstWidth, sizeof(dstWidth)));
ERROR_CHECK_STATUS_(vxQueryRemap(remap, VX_REMAP_DESTINATION_HEIGHT, &dstHeight, sizeof(dstHeight)));
for (vx_uint32 y = 0; y < dstHeight; y++){
for (vx_uint32 x = 0; x < dstWidth; x++){
vx_float32 src_xy[2];
ERROR_CHECK_STATUS_(vxGetRemapPoint(remap, x, y, &src_xy[0], &src_xy[1]));
fwrite(src_xy, sizeof(src_xy), 1, fp);
}
}
fclose(fp);
printf("OK: Dump: Remap %dx%d into %s\n", dstWidth, dstHeight, fileName);
return VX_SUCCESS;
}
static vx_status DumpReference(vx_reference ref, const char * fileName)
{
vx_enum type;
ERROR_CHECK_STATUS_(vxQueryReference(ref, VX_REFERENCE_TYPE, &type, sizeof(type)));
if (type == VX_TYPE_IMAGE) return DumpImage((vx_image)ref, fileName);
else if (type == VX_TYPE_ARRAY) return DumpArray((vx_array)ref, fileName);
else if (type == VX_TYPE_MATRIX) return DumpMatrix((vx_matrix)ref, fileName);
else if (type == VX_TYPE_REMAP) return DumpRemap((vx_remap)ref, fileName);
else return VX_ERROR_NOT_SUPPORTED;
}
static vx_image CreateAlignedImage(ls_context stitch, vx_uint32 width, vx_uint32 height, vx_uint32 alignpixels, vx_df_image format, vx_enum mem_type)
{
if (mem_type == VX_MEMORY_TYPE_OPENCL){
cl_context opencl_context = nullptr;
vx_imagepatch_addressing_t addr_in = { 0 };
void *ptr[1] = { nullptr };
addr_in.dim_x = width;
addr_in.dim_y = height;
addr_in.stride_x = ((format == VX_DF_IMAGE_RGBX) | (format == VX_DF_IMAGE_U32) | (format==VX_DF_IMAGE_S32)) ? 4 : (format == VX_DF_IMAGE_RGB4_AMD) ? 6 : 1;
if (alignpixels == 0)
addr_in.stride_y = addr_in.dim_x *addr_in.stride_x;
else
addr_in.stride_y = ((addr_in.dim_x + alignpixels - 1) & ~(alignpixels - 1))*addr_in.stride_x;
// allocate opencl buffer with required dim
vx_status status = vxQueryContext(stitch->context, VX_CONTEXT_ATTRIBUTE_AMD_OPENCL_CONTEXT, &opencl_context, sizeof(opencl_context));
if (status != VX_SUCCESS){
ls_printf("vxQueryContext of failed(%d)\n", status);
return nullptr;
}
vx_size size = (addr_in.dim_y + 1) * addr_in.stride_y;
cl_int err = CL_SUCCESS;
cl_mem clImg = clCreateBuffer(opencl_context, CL_MEM_READ_WRITE, size, NULL, &err);
if (!clImg || err){
ls_printf("clCreateBuffer of size %d failed(%d)\n", (int)size, err);
return nullptr;
}
ptr[0] = clImg;
return vxCreateImageFromHandle(stitch->context, format, &addr_in, ptr, mem_type);
}
else
{
return vxCreateImage(stitch->context, width, height, format);
}
}
//! \brief Function to set default values to global attributes
static void ResetLiveStitchGlobalAttributes()
{
// set default settings only once
if (!g_live_stitch_attr_initialized) {
g_live_stitch_attr_initialized = true;
memset(g_live_stitch_attr, 0, sizeof(g_live_stitch_attr));
g_live_stitch_attr[LIVE_STITCH_ATTR_EXPCOMP] = 1;
g_live_stitch_attr[LIVE_STITCH_ATTR_EXPCOMP_GAIN_IMG_W] = 1;
g_live_stitch_attr[LIVE_STITCH_ATTR_EXPCOMP_GAIN_IMG_H] = 1;
g_live_stitch_attr[LIVE_STITCH_ATTR_EXPCOMP_GAIN_IMG_C] = 1;
g_live_stitch_attr[LIVE_STITCH_ATTR_EXPCOMP_ALPHA_VALUE] = 0.01f;
g_live_stitch_attr[LIVE_STITCH_ATTR_EXPCOMP_BETA_VALUE] = 100.0f;
g_live_stitch_attr[LIVE_STITCH_ATTR_SEAMFIND] = 1;
g_live_stitch_attr[LIVE_STITCH_ATTR_SEAM_COST_SELECT] = 1;
g_live_stitch_attr[LIVE_STITCH_ATTR_SEAM_REFRESH] = 1;
g_live_stitch_attr[LIVE_STITCH_ATTR_SEAM_THRESHOLD] = 25;
g_live_stitch_attr[LIVE_STITCH_ATTR_SEAM_VERT_PRIORITY] = 1;
g_live_stitch_attr[LIVE_STITCH_ATTR_SEAM_HORT_PRIORITY] = -1;
g_live_stitch_attr[LIVE_STITCH_ATTR_SEAM_FREQUENCY] = 6000;
g_live_stitch_attr[LIVE_STITCH_ATTR_SEAM_QUALITY] = 1;
g_live_stitch_attr[LIVE_STITCH_ATTR_SEAM_STAGGER] = 1;
g_live_stitch_attr[LIVE_STITCH_ATTR_SEAM_COEQUSH_ENABLE] = 1;
g_live_stitch_attr[LIVE_STITCH_ATTR_SEAM_COEQUSH_HFOV_MIN] = 120;
g_live_stitch_attr[LIVE_STITCH_ATTR_SEAM_COEQUSH_PITCH_TOL] = 5;
g_live_stitch_attr[LIVE_STITCH_ATTR_SEAM_COEQUSH_YAW_TOL] = 5;
g_live_stitch_attr[LIVE_STITCH_ATTR_SEAM_COEQUSH_OVERLAP_HR] = 0.15f;
g_live_stitch_attr[LIVE_STITCH_ATTR_SEAM_COEQUSH_OVERLAP_VD] = 20;
g_live_stitch_attr[LIVE_STITCH_ATTR_SEAM_COEQUSH_TOPBOT_TOL] = 5;
g_live_stitch_attr[LIVE_STITCH_ATTR_SEAM_COEQUSH_TOPBOT_VGD] = 46;
g_live_stitch_attr[LIVE_STITCH_ATTR_MULTIBAND] = 1;
g_live_stitch_attr[LIVE_STITCH_ATTR_MULTIBAND_NUMBANDS] = 4;
g_live_stitch_attr[LIVE_STITCH_ATTR_STITCH_MODE] = (float)stitching_mode_normal;
// frame encoding default attributes
g_live_stitch_attr[LIVE_STITCH_ATTR_OUTPUT_TILE_NUM_X] = 1;
g_live_stitch_attr[LIVE_STITCH_ATTR_OUTPUT_TILE_NUM_Y] = 1;
g_live_stitch_attr[LIVE_STITCH_ATTR_OUTPUT_src_tile_overlap] = 0;
g_live_stitch_attr[LIVE_STITCH_ATTR_OUTPUT_TILE_BUFFER_VALUE] = 0;
g_live_stitch_attr[LIVE_STITCH_ATTR_OUTPUT_ENCODER_WIDTH] = 3840;
g_live_stitch_attr[LIVE_STITCH_ATTR_OUTPUT_ENCODER_HEIGHT] = 2160;
g_live_stitch_attr[LIVE_STITCH_ATTR_OUTPUT_ENCODER_STRIDE_Y] = 3840;
// chroma key default
g_live_stitch_attr[LIVE_STITCH_ATTR_CHROMA_KEY] = 0;
g_live_stitch_attr[LIVE_STITCH_ATTR_CHROMA_KEY_VALUE] = 8454016;
g_live_stitch_attr[LIVE_STITCH_ATTR_CHROMA_KEY_TOL] = 25;
g_live_stitch_attr[LIVE_STITCH_ATTR_CHROMA_KEY_EED] = 0;
// LoomIO specific attributes
g_live_stitch_attr[LIVE_STITCH_ATTR_IO_CAMERA_AUX_DATA_SIZE] = (float)LOOMIO_DEFAULT_AUX_DATA_CAPACITY;
g_live_stitch_attr[LIVE_STITCH_ATTR_IO_OVERLAY_AUX_DATA_SIZE] = (float)LOOMIO_DEFAULT_AUX_DATA_CAPACITY;
g_live_stitch_attr[LIVE_STITCH_ATTR_IO_OUTPUT_AUX_DATA_SIZE] = (float)LOOMIO_DEFAULT_AUX_DATA_CAPACITY;
// Temporal Filter
g_live_stitch_attr[LIVE_STITCH_ATTR_NOISE_FILTER] = 0;
g_live_stitch_attr[LIVE_STITCH_ATTR_NOISE_FILTER_LAMBDA] = 1;
g_live_stitch_attr[LIVE_STITCH_ATTR_SAVE_AND_LOAD_INIT] = 0;
}
}
static std::vector<std::string> split(std::string str, char delimiter) {
std::stringstream ss(str);
std::string tok;
std::vector<std::string> internal;
while (std::getline(ss, tok, delimiter)) {
internal.push_back(tok);
}
return internal;
}
static vx_status IsValidContext(ls_context stitch)
{
if (!stitch || stitch->magic != LIVE_STITCH_MAGIC) return VX_ERROR_INVALID_REFERENCE;
return VX_SUCCESS;
}
static vx_status IsValidContextAndInitialized(ls_context stitch)
{
if (!stitch || stitch->magic != LIVE_STITCH_MAGIC) return VX_ERROR_INVALID_REFERENCE;
if (!stitch->initialized) return VX_ERROR_NOT_ALLOCATED;
return VX_SUCCESS;
}
static vx_status IsValidContextAndNotInitialized(ls_context stitch)
{
if (!stitch || stitch->magic != LIVE_STITCH_MAGIC) return VX_ERROR_INVALID_REFERENCE;
if (stitch->initialized) return VX_ERROR_NOT_SUPPORTED;
return VX_SUCCESS;
}
static const char * GetFileNameSuffix(ls_context stitch, vx_reference ref, bool& isIntermediateTmpData, bool& isForCpuUseOnly)
{
struct {
vx_reference ref;
bool isIntermediateTmpData;
bool isForCpuUseOnly;
const char * fileNameSuffix;
} refList[] = {
// intermediate tables and data that needs initialization
{ (vx_reference)stitch->ValidPixelEntry, false, false, "warp-valid.bin" },
{ (vx_reference)stitch->WarpRemapEntry, false, false, "warp-remap.bin" },
{ (vx_reference)stitch->RGBY1, false, false, "warp-rgby.raw" },
{ (vx_reference)stitch->cam_id_image, false, false, "merge-camid.raw" },
{ (vx_reference)stitch->group1_image, false, false, "merge-group1.raw" },
{ (vx_reference)stitch->group2_image, false, false, "merge-group2.raw" },
{ (vx_reference)stitch->weight_image, false, false, "merge-weight.raw" },
{ (vx_reference)stitch->valid_array, false, false, "exp-valid.bin" },
{ (vx_reference)stitch->OverlapPixelEntry, false, false, "exp-overlap.bin" },
{ (vx_reference)stitch->overlap_matrix, false, true, "exp-count.bin" },
{ (vx_reference)stitch->RGBY2, false, false, "exp-rgby.raw" },
{ (vx_reference)stitch->valid_mask_image, false, false, "valid-mask.raw" },
{ (vx_reference)stitch->seamfind_valid_array, false, false, "seam-valid.bin" },
{ (vx_reference)stitch->seamfind_weight_array, false, false, "seam-weight.bin" },
{ (vx_reference)stitch->seamfind_accum_array, false, false, "seam-accum.bin" },
{ (vx_reference)stitch->seamfind_pref_array, false, false, "seam-pref.bin" },
{ (vx_reference)stitch->seamfind_info_array, false, false, "seam-info.bin" },
{ (vx_reference)stitch->seamfind_path_array, false, false, "seam-path.bin" },
{ (vx_reference)stitch->seamfind_scene_array, false, false, "seam-scene.bin" },
{ (vx_reference)stitch->seamfind_weight_image, false, false, "seam-mask.raw" },
{ (vx_reference)stitch->blend_mask_image, false, false, "blend-mask.raw" },
{ (vx_reference)stitch->blend_offsets, false, false, "blend-offsets.bin" },
{ (vx_reference)stitch->camera_remap, false, false, "remap-input.raw" },
{ (vx_reference)stitch->overlay_remap, false, false, "remap-overlay.raw" },
// intermediate temporary data
{ (vx_reference)stitch->Img_input, true, false, "camera-input.raw" },
{ (vx_reference)stitch->Img_overlay, true, false, "overlay-input.raw" },
{ (vx_reference)stitch->Img_overlay_rgba, true, false, "overlay-warped.raw" },
{ (vx_reference)stitch->Img_output, true, false, "stitch-output.raw" },
};
for (vx_size i = 0; i < dimof(refList); i++) {
if (refList[i].ref && refList[i].ref == ref) {
isIntermediateTmpData = refList[i].isIntermediateTmpData;
isForCpuUseOnly = refList[i].isForCpuUseOnly;
return refList[i].fileNameSuffix;
}
}
return nullptr;
}
static vx_status DumpInternalTables(ls_context stitch, const char * fileNamePrefix, bool dumpIntermediateTmpDataToo)
{
vx_reference refList[] = {
// intermediate tables and data that needs initialization
(vx_reference)stitch->ValidPixelEntry,
(vx_reference)stitch->WarpRemapEntry,
(vx_reference)stitch->RGBY1,
(vx_reference)stitch->cam_id_image,
(vx_reference)stitch->group1_image,
(vx_reference)stitch->group2_image,
(vx_reference)stitch->weight_image,
(vx_reference)stitch->valid_array,
(vx_reference)stitch->OverlapPixelEntry,
(vx_reference)stitch->overlap_matrix,
(vx_reference)stitch->RGBY2,
(vx_reference)stitch->valid_mask_image,
(vx_reference)stitch->seamfind_valid_array,
(vx_reference)stitch->seamfind_weight_array,
(vx_reference)stitch->seamfind_accum_array,
(vx_reference)stitch->seamfind_pref_array,
(vx_reference)stitch->seamfind_info_array,
(vx_reference)stitch->seamfind_path_array,
(vx_reference)stitch->seamfind_scene_array,
(vx_reference)stitch->seamfind_weight_image,
(vx_reference)stitch->blend_mask_image,
(vx_reference)stitch->blend_offsets,
(vx_reference)stitch->camera_remap,
(vx_reference)stitch->overlay_remap,
// intermediate temporary data
(vx_reference)stitch->Img_input,
(vx_reference)stitch->Img_overlay,
(vx_reference)stitch->Img_overlay_rgba,
(vx_reference)stitch->Img_output,
};
for (vx_size i = 0; i < dimof(refList); i++) {
if (refList[i]) {
bool isIntermediateTmpData = false, isForCpuUseOnly = false;
const char * fileNameSuffix = GetFileNameSuffix(stitch, refList[i], isIntermediateTmpData, isForCpuUseOnly);
if (fileNameSuffix && (!isIntermediateTmpData || dumpIntermediateTmpDataToo)) {
char fileName[1024]; sprintf(fileName, "%s-%s", fileNamePrefix, fileNameSuffix);
vx_status status = DumpReference(refList[i], fileName);
if (status != VX_SUCCESS)
return status;
}
}
}
if (dumpIntermediateTmpDataToo && stitch->MULTIBAND_BLEND) {
for (vx_int32 level = 0; level < stitch->num_bands; level++) {
vx_status status;
char fileName[1024];
sprintf(fileName, "%s-blend-pyr-mask-%d.raw", fileNamePrefix, level);
status = DumpImage(stitch->pStitchMultiband[level].WeightPyrImgGaussian, fileName);
if (status != VX_SUCCESS)
return status;
sprintf(fileName, "%s-blend-pyr-gauss-%d.raw", fileNamePrefix, level);
status = DumpImage(stitch->pStitchMultiband[level].DstPyrImgGaussian, fileName);
if (status != VX_SUCCESS)
return status;
sprintf(fileName, "%s-blend-pyr-lap-%d.raw", fileNamePrefix, level);
status = DumpImage(stitch->pStitchMultiband[level].DstPyrImgLaplacian, fileName);
if (status != VX_SUCCESS)
return status;
sprintf(fileName, "%s-blend-pyr-lap-rec-%d.raw", fileNamePrefix, level);
status = DumpImage(stitch->pStitchMultiband[level].DstPyrImgLaplacianRec, fileName);
if (status != VX_SUCCESS)
return status;
}
}
return VX_SUCCESS;
}
static vx_status SyncInternalTables(ls_context stitch)
{
vx_reference refList[] = {
(vx_reference)stitch->ValidPixelEntry,
(vx_reference)stitch->WarpRemapEntry,
(vx_reference)stitch->cam_id_image,
(vx_reference)stitch->group1_image,
(vx_reference)stitch->group2_image,
(vx_reference)stitch->weight_image,
(vx_reference)stitch->valid_mask_image,
(vx_reference)stitch->valid_array,
(vx_reference)stitch->OverlapPixelEntry,
(vx_reference)stitch->seamfind_valid_array,
(vx_reference)stitch->seamfind_weight_array,
(vx_reference)stitch->seamfind_accum_array,
(vx_reference)stitch->seamfind_pref_array,
(vx_reference)stitch->seamfind_info_array,
(vx_reference)stitch->seamfind_path_array,
(vx_reference)stitch->seamfind_weight_image,
(vx_reference)stitch->seamfind_scene_array,
(vx_reference)stitch->blend_mask_image,
(vx_reference)stitch->RGBY1,
(vx_reference)stitch->RGBY2,
(vx_reference)stitch->overlay_remap,
(vx_reference)stitch->camera_remap,
};
for (vx_size i = 0; i < dimof(refList); i++) {
if (refList[i]) {
vx_status status = vxDirective(refList[i], VX_DIRECTIVE_AMD_COPY_TO_OPENCL);
if (status != VX_SUCCESS) {
ls_printf("ERROR: SyncInternalTables: vxDirective([%d], VX_DIRECTIVE_AMD_COPY_TO_OPENCL) failed (%d)\n", (int)i, status);
return status;
}
}
}
return VX_SUCCESS;
}
static vx_status quickSetupFilesLookup(ls_context stitch)
{
FILE * fp = fopen("StitchTableSizes.txt", "r");
if (!fp) { stitch->SETUP_LOAD_FILES_FOUND = vx_false_e; }
else{ stitch->SETUP_LOAD_FILES_FOUND = vx_true_e; }
if (fp != NULL) fclose(fp);
return VX_SUCCESS;
}
static vx_status quickSetupDumpTableSizes(ls_context stitch)
{
FILE * fp = fopen("StitchTableSizes.txt", "w+");
if (!fp) {
ls_printf("ERROR: quickSetupDumpTableSize: unable to create: StitchTableSizes.txt\n");
if (fp != NULL) fclose(fp);
return VX_FAILURE;
}
fprintf(fp, VX_FMT_SIZE, stitch->table_sizes.blendOffsetTableSize); fprintf(fp, "\n");
fprintf(fp, VX_FMT_SIZE, stitch->table_sizes.expCompOverlapTableSize); fprintf(fp, "\n");
fprintf(fp, VX_FMT_SIZE, stitch->table_sizes.expCompValidTableSize); fprintf(fp, "\n");
fprintf(fp, VX_FMT_SIZE, stitch->table_sizes.seamFindAccumTableSize); fprintf(fp, "\n");
fprintf(fp, VX_FMT_SIZE, stitch->table_sizes.seamFindPathTableSize); fprintf(fp, "\n");
fprintf(fp, VX_FMT_SIZE, stitch->table_sizes.seamFindPrefInfoTableSize); fprintf(fp, "\n");
fprintf(fp, VX_FMT_SIZE, stitch->table_sizes.seamFindValidTableSize); fprintf(fp, "\n");
fprintf(fp, VX_FMT_SIZE, stitch->table_sizes.seamFindWeightTableSize); fprintf(fp, "\n");
fprintf(fp, VX_FMT_SIZE, stitch->table_sizes.warpTableSize); fprintf(fp, "\n");
fclose(fp);
return VX_SUCCESS;
}
static vx_status quickSetupDumpTables(ls_context stitch)
{
vx_reference refList[] = {
(vx_reference)stitch->ValidPixelEntry,
(vx_reference)stitch->WarpRemapEntry,
(vx_reference)stitch->RGBY1,
(vx_reference)stitch->cam_id_image,
(vx_reference)stitch->group1_image,
(vx_reference)stitch->group2_image,
(vx_reference)stitch->weight_image,
(vx_reference)stitch->valid_array,
(vx_reference)stitch->OverlapPixelEntry,
(vx_reference)stitch->overlap_matrix,
(vx_reference)stitch->RGBY2,
(vx_reference)stitch->valid_mask_image,
(vx_reference)stitch->seamfind_valid_array,
(vx_reference)stitch->seamfind_weight_array,
(vx_reference)stitch->seamfind_accum_array,
(vx_reference)stitch->seamfind_pref_array,
(vx_reference)stitch->seamfind_info_array,
(vx_reference)stitch->seamfind_path_array,
(vx_reference)stitch->seamfind_scene_array,
(vx_reference)stitch->seamfind_weight_image,
(vx_reference)stitch->blend_mask_image,
(vx_reference)stitch->blend_offsets,
(vx_reference)stitch->camera_remap,
(vx_reference)stitch->overlay_remap,
};
for (vx_size i = 0; i < dimof(refList); i++) {
if (refList[i]) {
bool isIntermediateTmpData = false, isForCpuUseOnly = false;
const char * fileNameSuffix = GetFileNameSuffix(stitch, refList[i], isIntermediateTmpData, isForCpuUseOnly);
if (fileNameSuffix && (!isIntermediateTmpData)) {
char fileName[1024]; sprintf(fileName, "%s",fileNameSuffix);
vx_status status = DumpReference(refList[i], fileName);
if (status != VX_SUCCESS)
return status;
}
}
}
return VX_SUCCESS;
}
vx_status loadImage(vx_image img, const char * fileName)
{
FILE * fp = fopen(fileName, "r");
if (!fp) {
ls_printf("ERROR: loadImage: unable to open: %s\n", fileName);
if (fp != NULL) fclose(fp);
return VX_FAILURE;
}
vx_df_image format = VX_DF_IMAGE_VIRT;
vx_size num_planes = 0;
vx_rectangle_t rectFull = { 0, 0, 0, 0 };
ERROR_CHECK_STATUS(vxQueryImage(img, VX_IMAGE_ATTRIBUTE_FORMAT, &format, sizeof(format)));
ERROR_CHECK_STATUS(vxQueryImage(img, VX_IMAGE_ATTRIBUTE_PLANES, &num_planes, sizeof(num_planes)));
ERROR_CHECK_STATUS(vxQueryImage(img, VX_IMAGE_ATTRIBUTE_WIDTH, &rectFull.end_x, sizeof(rectFull.end_x)));
ERROR_CHECK_STATUS(vxQueryImage(img, VX_IMAGE_ATTRIBUTE_HEIGHT, &rectFull.end_y, sizeof(rectFull.end_y)));
// write all image planes from vx_image
for (vx_uint32 plane = 0; plane < (vx_uint32)num_planes; plane++){
vx_imagepatch_addressing_t addr = { 0 };
vx_uint8 * src = NULL;
ERROR_CHECK_STATUS(vxAccessImagePatch(img, &rectFull, plane, &addr, (void **)&src, VX_WRITE_ONLY));
vx_size width = (addr.dim_x * addr.scale_x) / VX_SCALE_UNITY;
vx_size width_in_bytes = (format == VX_DF_IMAGE_U1_AMD) ? ((width + 7) >> 3) : (width * addr.stride_x);
for (vx_uint32 y = 0; y < addr.dim_y; y += addr.step_y){
vx_uint8 *srcp = (vx_uint8 *)vxFormatImagePatchAddress2d(src, 0, y, &addr);
ERROR_CHECK_FREAD_(fread(srcp, 1, width_in_bytes, fp), width_in_bytes);
}
ERROR_CHECK_STATUS(vxCommitImagePatch(img, &rectFull, plane, &addr, src));
}
fclose(fp);
return VX_SUCCESS;
}
vx_status loadArray(vx_array arr, const char * fileName)
{
FILE * fp = fopen(fileName, "r");
if (!fp) {
ls_printf("ERROR: loadArray: unable to open: %s\n", fileName);
if (fp != NULL) fclose(fp);
return VX_FAILURE;
}
vx_size numItems, itemSize;
ERROR_CHECK_STATUS_(vxQueryArray(arr, VX_ARRAY_ITEMSIZE, &itemSize, sizeof(itemSize)));
ERROR_CHECK_STATUS_(vxQueryArray(arr, VX_ARRAY_CAPACITY, &numItems, sizeof(numItems)));
StitchWarpRemapEntry validPixelEntry = { 0 };
ERROR_CHECK_STATUS_(vxTruncateArray(arr, 0));
ERROR_CHECK_STATUS_(vxAddArrayItems(arr, numItems, &validPixelEntry, 0));
vx_map_id map_id;
vx_uint8 * ptr;
vx_size stride;
ERROR_CHECK_STATUS_(vxMapArrayRange(arr, 0, numItems, &map_id, &stride, (void **)&ptr, VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST, 0));
ERROR_CHECK_FREAD_(fread(ptr, itemSize, numItems, fp),numItems);
ERROR_CHECK_STATUS_(vxUnmapArrayRange(arr, map_id));
fclose(fp);
return VX_SUCCESS;
}
static vx_status loadMatrix(vx_matrix mat, const char * fileName)
{
FILE * fp = fopen(fileName, "r");
if (!fp) {
ls_printf("ERROR: loadMatrix: unable to read: %s\n", fileName);
if (fp != NULL) fclose(fp);
return VX_FAILURE;
}
vx_size size;
ERROR_CHECK_STATUS_(vxQueryMatrix(mat, VX_MATRIX_SIZE, &size, sizeof(size)));
vx_uint8 * buf = new vx_uint8[size];
ERROR_CHECK_STATUS_(vxCopyMatrix(mat, buf, VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST));
ERROR_CHECK_FREAD_(fread(buf, 4, size, fp),size);
delete[] buf;
fclose(fp);
vx_size rows, columns;
ERROR_CHECK_STATUS_(vxQueryMatrix(mat, VX_MATRIX_ROWS, &rows, sizeof(rows)));
ERROR_CHECK_STATUS_(vxQueryMatrix(mat, VX_MATRIX_COLUMNS, &columns, sizeof(columns)));
return VX_SUCCESS;
}
static vx_status loadRemap(vx_remap remap, const char * fileName)
{
FILE * fp = fopen(fileName, "r");
if (!fp) {
ls_printf("ERROR: loadRemap: unable to read: %s\n", fileName);
if (fp != NULL) fclose(fp);
return VX_FAILURE;
}
vx_uint32 dstWidth, dstHeight;
ERROR_CHECK_STATUS_(vxQueryRemap(remap, VX_REMAP_DESTINATION_WIDTH, &dstWidth, sizeof(dstWidth)));
ERROR_CHECK_STATUS_(vxQueryRemap(remap, VX_REMAP_DESTINATION_HEIGHT, &dstHeight, sizeof(dstHeight)));
for (vx_uint32 y = 0; y < dstHeight; y++){
for (vx_uint32 x = 0; x < dstWidth; x++){
vx_float32 src_xy[2];
ERROR_CHECK_STATUS_(vxGetRemapPoint(remap, x, y, &src_xy[0], &src_xy[1]));
ERROR_CHECK_FREAD_(fread(src_xy, sizeof(src_xy), 1, fp),1);
}
}
fclose(fp);
return VX_SUCCESS;
}
static vx_status loadReference(vx_reference ref, const char * fileName)
{
vx_enum type;
ERROR_CHECK_STATUS_(vxQueryReference(ref, VX_REFERENCE_TYPE, &type, sizeof(type)));
if (type == VX_TYPE_IMAGE) return loadImage((vx_image)ref, fileName);
else if (type == VX_TYPE_ARRAY) return loadArray((vx_array)ref, fileName);
else if (type == VX_TYPE_MATRIX) return loadMatrix((vx_matrix)ref, fileName);
else if (type == VX_TYPE_REMAP) return loadRemap((vx_remap)ref, fileName);
else return VX_ERROR_NOT_SUPPORTED;
}
static vx_status quickSetupLoadTableSizes(ls_context stitch)
{
FILE * fp = fopen("StitchTableSizes.txt", "r");
if (!fp) {
ls_printf("ERROR: quickSetupLoadTableSizes: unable to open: StitchTableSizes.txt\n");
if (fp != NULL) fclose(fp);
return VX_FAILURE;
}
int readValue = 0;
readValue = fscanf(fp, VX_FMT_SIZE, &stitch->table_sizes.blendOffsetTableSize);
if (!readValue) { ls_printf("ERROR: quickSetupLoadTableSizes: unable to read file\n"); return VX_FAILURE; }
readValue = fscanf(fp, VX_FMT_SIZE, &stitch->table_sizes.expCompOverlapTableSize);
if (!readValue) { ls_printf("ERROR: quickSetupLoadTableSizes: unable to read file\n"); return VX_FAILURE; }
readValue = fscanf(fp, VX_FMT_SIZE, &stitch->table_sizes.expCompValidTableSize);
if (!readValue) { ls_printf("ERROR: quickSetupLoadTableSizes: unable to read file\n"); return VX_FAILURE; }
readValue = fscanf(fp, VX_FMT_SIZE, &stitch->table_sizes.seamFindAccumTableSize);
if (!readValue) { ls_printf("ERROR: quickSetupLoadTableSizes: unable to read file\n"); return VX_FAILURE; }
readValue = fscanf(fp, VX_FMT_SIZE, &stitch->table_sizes.seamFindPathTableSize);
if (!readValue) { ls_printf("ERROR: quickSetupLoadTableSizes: unable to read file\n"); return VX_FAILURE; }
readValue = fscanf(fp, VX_FMT_SIZE, &stitch->table_sizes.seamFindPrefInfoTableSize);
if (!readValue) { ls_printf("ERROR: quickSetupLoadTableSizes: unable to read file\n"); return VX_FAILURE; }
readValue = fscanf(fp, VX_FMT_SIZE, &stitch->table_sizes.seamFindValidTableSize);
if (!readValue) { ls_printf("ERROR: quickSetupLoadTableSizes: unable to read file\n"); return VX_FAILURE; }
readValue = fscanf(fp, VX_FMT_SIZE, &stitch->table_sizes.seamFindWeightTableSize);
if (!readValue) { ls_printf("ERROR: quickSetupLoadTableSizes: unable to read file\n"); return VX_FAILURE; }
readValue = fscanf(fp, VX_FMT_SIZE, &stitch->table_sizes.warpTableSize);
if (!readValue) { ls_printf("ERROR: quickSetupLoadTableSizes: unable to read file\n"); return VX_FAILURE; }
return VX_SUCCESS;
}
static vx_status quickSetupLoadTables(ls_context stitch)
{
vx_reference refList[] = {
// intermediate tables and data that needs initialization
(vx_reference)stitch->ValidPixelEntry,
(vx_reference)stitch->WarpRemapEntry,
(vx_reference)stitch->RGBY1,
(vx_reference)stitch->cam_id_image,
(vx_reference)stitch->group1_image,
(vx_reference)stitch->group2_image,
(vx_reference)stitch->weight_image,
(vx_reference)stitch->valid_array,
(vx_reference)stitch->OverlapPixelEntry,
(vx_reference)stitch->overlap_matrix,
(vx_reference)stitch->RGBY2,
(vx_reference)stitch->valid_mask_image,
(vx_reference)stitch->seamfind_valid_array,
(vx_reference)stitch->seamfind_weight_array,
(vx_reference)stitch->seamfind_accum_array,
(vx_reference)stitch->seamfind_pref_array,
(vx_reference)stitch->seamfind_info_array,
(vx_reference)stitch->seamfind_path_array,
(vx_reference)stitch->seamfind_scene_array,
(vx_reference)stitch->seamfind_weight_image,
(vx_reference)stitch->blend_mask_image,
(vx_reference)stitch->blend_offsets,
(vx_reference)stitch->camera_remap,
(vx_reference)stitch->overlay_remap,
};
for (vx_size i = 0; i < dimof(refList); i++) {
if (refList[i]) {
bool isIntermediateTmpData = false, isForCpuUseOnly = false;
const char * fileNameSuffix = GetFileNameSuffix(stitch, refList[i], isIntermediateTmpData, isForCpuUseOnly);
if (fileNameSuffix && (!isIntermediateTmpData)) {
char fileName[1024]; sprintf(fileName, "%s", fileNameSuffix);
vx_status status = loadReference(refList[i], fileName);
if (status != VX_SUCCESS)
return status;
}
}
}
return VX_SUCCESS;
}
static vx_status setupQuickInitializeParams(ls_context stitch)
{
vx_uint32 camWidth = stitch->camera_rgb_buffer_width / stitch->num_camera_columns;
vx_uint32 camHeight = stitch->camera_rgb_buffer_height / stitch->num_camera_rows;
vx_uint32 numCamera = stitch->num_camera_rows * stitch->num_camera_columns;
vx_uint32 eqrWidth = stitch->output_rgb_buffer_width;
vx_uint32 eqrHeight = stitch->output_rgb_buffer_height;
// compute camera warp parameters and check for supported lens types
float Mcam[32 * 9], Tcam[32 * 3], fcam[32 * 2], Mr[3 * 3];
vx_status status = CalculateCameraWarpParameters(numCamera, camWidth, camHeight, &stitch->rig_par, stitch->camera_par, Mcam, Tcam, fcam, Mr);
if (status != VX_SUCCESS) return status;
vx_uint32 paddingPixelCount = stitch->stitchInitData->paddingPixelCount;
const camera_lens_params * lens = &stitch->camera_par[0].lens;
// add nodes to the graph and verify.
stitch->stitchInitData->params = { 0 };
stitch->stitchInitData->params.camWidth = camWidth;
stitch->stitchInitData->params.camHeight = camHeight;
stitch->stitchInitData->params.paddingPixelCount = paddingPixelCount;
float cam_params_val = 0.0f;
bool lens_fish_eye = 0;
vx_map_id mapIdCamPar;
vx_size stride = sizeof(vx_size);
float *cam_params;
ERROR_CHECK_STATUS(vxTruncateArray(stitch->stitchInitData->CameraParamsArr, 0));
ERROR_CHECK_STATUS(vxAddArrayItems(stitch->stitchInitData->CameraParamsArr, 32 * numCamera, &cam_params_val, 0));
ERROR_CHECK_STATUS_(vxMapArrayRange(stitch->stitchInitData->CameraParamsArr, 0, 32 * numCamera, &mapIdCamPar, &stride, (void **)&cam_params, VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST, VX_NOGAP_X));
const float * T = Tcam, *M = Mcam, *f = fcam;
for (vx_uint32 cam = 0; cam < numCamera; cam++, T += 3, M += 9, f += 2) {
// perform lens distortion and warp for each pixel in the equirectangular destination image
const camera_lens_params * lens = &stitch->camera_par[cam].lens;
float k0 = 1.0f - (lens->k1 + lens->k2 + lens->k3);
float left = 0, top = 0, right = (float)camWidth, bottom = (float)camHeight;
if (lens->lens_type <= ptgui_lens_fisheye_circ && (lens->reserved[3] != 0 || lens->reserved[4] != 0 || lens->reserved[5] != 0 || lens->reserved[6] != 0)) {
left = std::max(left, lens->reserved[3]);
top = std::max(top, lens->reserved[4]);
right = std::min(right, lens->reserved[5]);
bottom = std::min(bottom, lens->reserved[6]);
}
stitch->stitchInitData->params.camId = cam;
stitch->stitchInitData->params.lens_type = lens->lens_type;
stitch->stitchInitData->params.F0 = f[0]; stitch->stitchInitData->params.F1 = f[1];
stitch->stitchInitData->params.TCam0 = T[0];
stitch->stitchInitData->params.TCam1 = T[1];
stitch->stitchInitData->params.TCam2 = T[2];
cam_params[0] = left; cam_params[1] = top;
cam_params[2] = right; cam_params[3] = bottom;
cam_params[4] = lens->k1; cam_params[5] = lens->k2;
cam_params[6] = lens->k3; cam_params[7] = k0;
cam_params[8] = lens->du0; cam_params[9] = lens->dv0;
cam_params[10] = lens->r_crop;
cam_params[11] = f[0]; cam_params[12] = f[1];
cam_params[13] = T[0];
cam_params[14] = T[1];
cam_params[15] = T[2];
memcpy(&cam_params[16], (void*)M, sizeof(float) * 9);
cam_params[25] = (float)lens->lens_type;
lens_fish_eye = (lens->lens_type == ptgui_lens_fisheye_circ);
cam_params += 32;
}
ERROR_CHECK_STATUS_(vxUnmapArrayRange(stitch->stitchInitData->CameraParamsArr, mapIdCamPar));
stitch->stitchInitData->params.camId = numCamera;
stitch->stitchInitData->lens_fish_eye = lens_fish_eye;
return VX_SUCCESS;
}
static vx_status setupQuickInitializeGraph(ls_context stitch)
{
vx_uint32 numCamera = stitch->num_camera_rows * stitch->num_camera_columns;
vx_uint32 eqrWidth = stitch->output_rgb_buffer_width;
vx_uint32 eqrHeight = stitch->output_rgb_buffer_height;
vx_uint32 paddingPixelCount = stitch->stitchInitData->paddingPixelCount;
bool lens_fish_eye = stitch->stitchInitData->lens_fish_eye;
if (lens_fish_eye)
{
ERROR_CHECK_OBJECT_(stitch->stitchInitData->calc_warp_maps_node = stitchInitCalcCamWarpMaps(stitch->stitchInitData->graphInitialize, &stitch->stitchInitData->params,
stitch->stitchInitData->CameraParamsArr, stitch->stitchInitData->ValidPixelMap,
NULL, stitch->stitchInitData->SrcCoordMap, stitch->stitchInitData->CameraZBuffArr));
}
else
{
ERROR_CHECK_OBJECT_(stitch->stitchInitData->calc_warp_maps_node = stitchInitCalcCamWarpMaps(stitch->stitchInitData->graphInitialize, &stitch->stitchInitData->params,
stitch->stitchInitData->CameraParamsArr, stitch->stitchInitData->ValidPixelMap,
stitch->stitchInitData->PaddedPixMap, stitch->stitchInitData->SrcCoordMap, stitch->stitchInitData->CameraZBuffArr));
}
ERROR_CHECK_OBJECT_(stitch->stitchInitData->calc_default_idx_node = stitchInitCalcDefCamIdxNode(stitch->stitchInitData->graphInitialize, numCamera,
eqrWidth, eqrHeight, stitch->stitchInitData->CameraZBuffArr, stitch->stitchInitData->DefaultCamMap));