/
mat.h
1904 lines (1570 loc) · 55.5 KB
/
mat.h
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
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#ifndef NCNN_MAT_H
#define NCNN_MAT_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if __ARM_NEON
#include <arm_neon.h>
#endif
#include "platform.h"
#include "allocator.h"
#include "option.h"
#include "gpu.h"
#if NCNN_VULKAN
#include <vulkan/vulkan.h>
#endif // NCNN_VULKAN
#if NCNN_PIXEL
#if __ANDROID_API__ >= 9
#include <jni.h>
#include <android/bitmap.h>
#endif // __ANDROID_API__ >= 9
#endif // NCNN_PIXEL
namespace ncnn {
#if NCNN_VULKAN
class VkMat;
#endif // NCNN_VULKAN
// the three dimension matrix
class Mat
{
public:
// empty
Mat();
// vec
Mat(int w, size_t elemsize = 4u, Allocator* allocator = 0);
// image
Mat(int w, int h, size_t elemsize = 4u, Allocator* allocator = 0);
// dim
Mat(int w, int h, int c, size_t elemsize = 4u, Allocator* allocator = 0);
// packed vec
Mat(int w, size_t elemsize, int elempack, Allocator* allocator = 0);
// packed image
Mat(int w, int h, size_t elemsize, int elempack, Allocator* allocator = 0);
// packed dim
Mat(int w, int h, int c, size_t elemsize, int elempack, Allocator* allocator = 0);
// copy
Mat(const Mat& m);
// external vec
Mat(int w, void* data, size_t elemsize = 4u, Allocator* allocator = 0);
// external image
Mat(int w, int h, void* data, size_t elemsize = 4u, Allocator* allocator = 0);
// external dim
Mat(int w, int h, int c, void* data, size_t elemsize = 4u, Allocator* allocator = 0);
// external packed vec
Mat(int w, void* data, size_t elemsize, int elempack, Allocator* allocator = 0);
// external packed image
Mat(int w, int h, void* data, size_t elemsize, int elempack, Allocator* allocator = 0);
// external packed dim
Mat(int w, int h, int c, void* data, size_t elemsize, int elempack, Allocator* allocator = 0);
// release
~Mat();
// assign
Mat& operator=(const Mat& m);
// set all
void fill(float v);
void fill(int v);
#if __ARM_NEON
void fill(float32x4_t _v);
#endif // __ARM_NEON
template <typename T> void fill(T v);
// deep copy
Mat clone(Allocator* allocator = 0) const;
// reshape vec
Mat reshape(int w, Allocator* allocator = 0) const;
// reshape image
Mat reshape(int w, int h, Allocator* allocator = 0) const;
// reshape dim
Mat reshape(int w, int h, int c, Allocator* allocator = 0) const;
// allocate vec
void create(int w, size_t elemsize = 4u, Allocator* allocator = 0);
// allocate image
void create(int w, int h, size_t elemsize = 4u, Allocator* allocator = 0);
// allocate dim
void create(int w, int h, int c, size_t elemsize = 4u, Allocator* allocator = 0);
// allocate packed vec
void create(int w, size_t elemsize, int elempack, Allocator* allocator = 0);
// allocate packed image
void create(int w, int h, size_t elemsize, int elempack, Allocator* allocator = 0);
// allocate packed dim
void create(int w, int h, int c, size_t elemsize, int elempack, Allocator* allocator = 0);
// allocate like
void create_like(const Mat& m, Allocator* allocator = 0);
#if NCNN_VULKAN
// allocate like
void create_like(const VkMat& m, Allocator* allocator = 0);
#endif // NCNN_VULKAN
// refcount++
void addref();
// refcount--
void release();
bool empty() const;
size_t total() const;
// shape only
Mat shape() const;
// data reference
Mat channel(int c);
const Mat channel(int c) const;
float* row(int y);
const float* row(int y) const;
template<typename T> T* row(int y);
template<typename T> const T* row(int y) const;
// range reference
Mat channel_range(int c, int channels);
const Mat channel_range(int c, int channels) const;
Mat row_range(int y, int rows);
const Mat row_range(int y, int rows) const;
Mat range(int x, int n);
const Mat range(int x, int n) const;
// access raw data
template<typename T> operator T*();
template<typename T> operator const T*() const;
// convenient access float vec element
float& operator[](size_t i);
const float& operator[](size_t i) const;
#if NCNN_PIXEL
enum PixelType
{
PIXEL_CONVERT_SHIFT = 16,
PIXEL_FORMAT_MASK = 0x0000ffff,
PIXEL_CONVERT_MASK = 0xffff0000,
PIXEL_RGB = 1,
PIXEL_BGR = 2,
PIXEL_GRAY = 3,
PIXEL_RGBA = 4,
PIXEL_BGRA = 5,
PIXEL_RGB2BGR = PIXEL_RGB | (PIXEL_BGR << PIXEL_CONVERT_SHIFT),
PIXEL_RGB2GRAY = PIXEL_RGB | (PIXEL_GRAY << PIXEL_CONVERT_SHIFT),
PIXEL_RGB2RGBA = PIXEL_RGB | (PIXEL_RGBA << PIXEL_CONVERT_SHIFT),
PIXEL_RGB2BGRA = PIXEL_RGB | (PIXEL_BGRA << PIXEL_CONVERT_SHIFT),
PIXEL_BGR2RGB = PIXEL_BGR | (PIXEL_RGB << PIXEL_CONVERT_SHIFT),
PIXEL_BGR2GRAY = PIXEL_BGR | (PIXEL_GRAY << PIXEL_CONVERT_SHIFT),
PIXEL_BGR2RGBA = PIXEL_BGR | (PIXEL_RGBA << PIXEL_CONVERT_SHIFT),
PIXEL_BGR2BGRA = PIXEL_BGR | (PIXEL_BGRA << PIXEL_CONVERT_SHIFT),
PIXEL_GRAY2RGB = PIXEL_GRAY | (PIXEL_RGB << PIXEL_CONVERT_SHIFT),
PIXEL_GRAY2BGR = PIXEL_GRAY | (PIXEL_BGR << PIXEL_CONVERT_SHIFT),
PIXEL_GRAY2RGBA = PIXEL_GRAY | (PIXEL_RGBA << PIXEL_CONVERT_SHIFT),
PIXEL_GRAY2BGRA = PIXEL_GRAY | (PIXEL_BGRA << PIXEL_CONVERT_SHIFT),
PIXEL_RGBA2RGB = PIXEL_RGBA | (PIXEL_RGB << PIXEL_CONVERT_SHIFT),
PIXEL_RGBA2BGR = PIXEL_RGBA | (PIXEL_BGR << PIXEL_CONVERT_SHIFT),
PIXEL_RGBA2GRAY = PIXEL_RGBA | (PIXEL_GRAY << PIXEL_CONVERT_SHIFT),
PIXEL_RGBA2BGRA = PIXEL_RGBA | (PIXEL_BGRA << PIXEL_CONVERT_SHIFT),
PIXEL_BGRA2RGB = PIXEL_BGRA | (PIXEL_RGB << PIXEL_CONVERT_SHIFT),
PIXEL_BGRA2BGR = PIXEL_BGRA | (PIXEL_BGR << PIXEL_CONVERT_SHIFT),
PIXEL_BGRA2GRAY = PIXEL_BGRA | (PIXEL_GRAY << PIXEL_CONVERT_SHIFT),
PIXEL_BGRA2RGBA = PIXEL_BGRA | (PIXEL_RGBA << PIXEL_CONVERT_SHIFT),
};
// convenient construct from pixel data
static Mat from_pixels(const unsigned char* pixels, int type, int w, int h, Allocator* allocator = 0);
// convenient construct from pixel data with stride(bytes-per-row) parameter
static Mat from_pixels(const unsigned char* pixels, int type, int w, int h, int stride, Allocator* allocator = 0);
// convenient construct from pixel data and resize to specific size
static Mat from_pixels_resize(const unsigned char* pixels, int type, int w, int h, int target_width, int target_height, Allocator* allocator = 0);
// convenient construct from pixel data and resize to specific size with stride(bytes-per-row) parameter
static Mat from_pixels_resize(const unsigned char* pixels, int type, int w, int h, int stride, int target_width, int target_height, Allocator* allocator = 0);
// convenient export to pixel data
void to_pixels(unsigned char* pixels, int type) const;
// convenient export to pixel data with stride(bytes-per-row) parameter
void to_pixels(unsigned char* pixels, int type, int stride) const;
// convenient export to pixel data and resize to specific size
void to_pixels_resize(unsigned char* pixels, int type, int target_width, int target_height) const;
// convenient export to pixel data and resize to specific size with stride(bytes-per-row) parameter
void to_pixels_resize(unsigned char* pixels, int type, int target_width, int target_height, int target_stride) const;
#if __ANDROID_API__ >= 9
// convenient construct from android Bitmap
static Mat from_android_bitmap(JNIEnv* env, jobject bitmap, int type_to, Allocator* allocator = 0);
// convenient construct from android Bitmap and resize to specific size
static Mat from_android_bitmap_resize(JNIEnv* env, jobject bitmap, int type_to, int target_width, int target_height, Allocator* allocator = 0);
// convenient export to android Bitmap and resize to the android Bitmap size
void to_android_bitmap(JNIEnv* env, jobject bitmap, int type_from) const;
#endif // __ANDROID_API__ >= 9
#endif // NCNN_PIXEL
// substract channel-wise mean values, then multiply by normalize values, pass 0 to skip
void substract_mean_normalize(const float* mean_vals, const float* norm_vals);
// convenient construct from half precisoin floating point data
static Mat from_float16(const unsigned short* data, int size);
// pointer to the data
void* data;
// pointer to the reference counter
// when points to user-allocated data, the pointer is NULL
int* refcount;
// element size in bytes
// 4 = float32/int32
// 2 = float16
// 1 = int8/uint8
// 0 = empty
size_t elemsize;
// packed count inside element
// c/1-h-w-1 h/1-w-1 w/1-1 scalar
// c/4-h-w-4 h/4-w-4 w/4-4 sse/neon
// c/8-h-w-8 h/8-w-8 w/8-8 avx/fp16
int elempack;
// the allocator
Allocator* allocator;
// the dimension rank
int dims;
int w;
int h;
int c;
size_t cstep;
};
#if NCNN_VULKAN
// the three dimension matrix, vulkan version
class VkMat
{
public:
// empty
VkMat();
// vec
VkMat(int w, size_t elemsize, VkAllocator* allocator, VkAllocator* staging_allocator);
// image
VkMat(int w, int h, size_t elemsize, VkAllocator* allocator, VkAllocator* staging_allocator);
// dim
VkMat(int w, int h, int c, size_t elemsize, VkAllocator* allocator, VkAllocator* staging_allocator);
// packed vec
VkMat(int w, size_t elemsize, int elempack, VkAllocator* allocator, VkAllocator* staging_allocator);
// packed image
VkMat(int w, int h, size_t elemsize, int elempack, VkAllocator* allocator, VkAllocator* staging_allocator);
// packed dim
VkMat(int w, int h, int c, size_t elemsize, int elempack, VkAllocator* allocator, VkAllocator* staging_allocator);
// copy
VkMat(const VkMat& m);
// external vec
VkMat(int w, VkBufferMemory* data, size_t offset, size_t elemsize, VkAllocator* allocator, VkAllocator* staging_allocator);
// external image
VkMat(int w, int h, VkBufferMemory* data, size_t offset, size_t elemsize, VkAllocator* allocator, VkAllocator* staging_allocator);
// external dim
VkMat(int w, int h, int c, VkBufferMemory* data, size_t offset, size_t elemsize, VkAllocator* allocator, VkAllocator* staging_allocator);
// external packed vec
VkMat(int w, VkBufferMemory* data, size_t offset, size_t elemsize, int elempack, VkAllocator* allocator, VkAllocator* staging_allocator);
// external packed image
VkMat(int w, int h, VkBufferMemory* data, size_t offset, size_t elemsize, int elempack, VkAllocator* allocator, VkAllocator* staging_allocator);
// external packed dim
VkMat(int w, int h, int c, VkBufferMemory* data, size_t offset, size_t elemsize, int elempack, VkAllocator* allocator, VkAllocator* staging_allocator);
// release
~VkMat();
// assign
VkMat& operator=(const VkMat& m);
// allocate vec
void create(int w, size_t elemsize, VkAllocator* allocator, VkAllocator* staging_allocator);
// allocate image
void create(int w, int h, size_t elemsize, VkAllocator* allocator, VkAllocator* staging_allocator);
// allocate dim
void create(int w, int h, int c, size_t elemsize, VkAllocator* allocator, VkAllocator* staging_allocator);
// allocate packed vec
void create(int w, size_t elemsize, int elempack, VkAllocator* allocator, VkAllocator* staging_allocator);
// allocate packed image
void create(int w, int h, size_t elemsize, int elempack, VkAllocator* allocator, VkAllocator* staging_allocator);
// allocate packed dim
void create(int w, int h, int c, size_t elemsize, int elempack, VkAllocator* allocator, VkAllocator* staging_allocator);
// allocate like
void create_like(const Mat& m, VkAllocator* allocator, VkAllocator* staging_allocator);
// allocate like
void create_like(const VkMat& m, VkAllocator* allocator, VkAllocator* staging_allocator);
// staging buffer
void prepare_staging_buffer();
void discard_staging_buffer();
// copy
void upload(const Mat& m);
void download(Mat& m) const;
// mapped
Mat mapped() const;
void* mapped_ptr() const;
// refcount++
void addref();
// refcount--
void release();
bool empty() const;
size_t total() const;
// shape only
Mat shape() const;
// data reference
VkMat channel(int c);
const VkMat channel(int c) const;
// range reference
VkMat channel_range(int c, int channels);
const VkMat channel_range(int c, int channels) const;
VkMat row_range(int y, int rows);
const VkMat row_range(int y, int rows) const;
VkMat range(int x, int n);
const VkMat range(int x, int n) const;
// low-level reference
VkBuffer buffer() const;
size_t buffer_offset() const;
VkBuffer staging_buffer() const;
size_t staging_buffer_offset() const;
// device buffer
VkBufferMemory* data;
// subrange offset
size_t offset;
// staging buffer
VkBufferMemory* staging_data;
// pointer to the reference counter
// when points to user-allocated data, the pointer is NULL
int* refcount;
int* staging_refcount;
// element size in bytes
// 4 = float32/int32
// 2 = float16
// 1 = int8/uint8
// 0 = empty
size_t elemsize;
// packed count inside element
// c/1-h-w-1 h/1-w-1 w/1-1 scalar
// c/4-h-w-4 h/4-w-4 w/4-4 sse/neon
// c/8-h-w-8 h/8-w-8 w/8-8 avx/fp16
int elempack;
// the allocator
VkAllocator* allocator;
VkAllocator* staging_allocator;
// the dimension rank
int dims;
int w;
int h;
int c;
size_t cstep;
};
class VkImageMat
{
public:
// empty
VkImageMat();
// image
VkImageMat(int width, int height, VkFormat format, VkImageAllocator* allocator);
// copy
VkImageMat(const VkImageMat& m);
// external image
VkImageMat(int width, int height, VkImageMemory* data, VkFormat format, VkImageAllocator* allocator);
// release
~VkImageMat();
// assign
VkImageMat& operator=(const VkImageMat& m);
// allocate image
void create(int width, int height, VkFormat format, VkImageAllocator* allocator);
// refcount++
void addref();
// refcount--
void release();
bool empty() const;
size_t total() const;
// low-level reference
VkImage image() const;
VkImageView imageview() const;
#if __ANDROID_API__ >= 26
// convenient construct from android hardware buffer
static VkImageMat from_android_hardware_buffer(AHardwareBuffer* hb, VkAndroidHardwareBufferImageAllocator* allocator);
#endif // __ANDROID_API__ >= 26
// device image
VkImageMemory* data;
// pointer to the reference counter
// when points to user-allocated data, the pointer is NULL
int* refcount;
// the allocator
VkImageAllocator* allocator;
int width;
int height;
VkFormat format;
};
// type for vulkan specialization constant and push constant
union vk_specialization_type { int i; float f; uint32_t u32; };
union vk_constant_type { int i; float f; };
#endif // NCNN_VULKAN
// misc function
#if NCNN_PIXEL
// convert yuv420sp(nv21) to rgb, the fast approximate version
void yuv420sp2rgb(const unsigned char* yuv420sp, int w, int h, unsigned char* rgb);
// image pixel bilinear resize
void resize_bilinear_c1(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h);
void resize_bilinear_c2(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h);
void resize_bilinear_c3(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h);
void resize_bilinear_c4(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h);
// image pixel bilinear resize with stride(bytes-per-row) parameter
void resize_bilinear_c1(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride);
void resize_bilinear_c2(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride);
void resize_bilinear_c3(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride);
void resize_bilinear_c4(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride);
// image pixel bilinear resize, convenient wrapper for yuv420sp(nv21)
void resize_bilinear_yuv420sp(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h);
#endif // NCNN_PIXEL
#if NCNN_PIXEL_ROTATE
// type is the from type, 6 means rotating from 6 to 1
//
// 1 2 3 4 5 6 7 8
//
// 888888 888888 88 88 8888888888 88 88 8888888888
// 88 88 88 88 88 88 88 88 88 88 88 88
// 8888 8888 8888 8888 88 8888888888 8888888888 88
// 88 88 88 88
// 88 88 888888 888888
//
// ref http://sylvana.net/jpegcrop/exif_orientation.html
// image pixel kanna rotate
void kanna_rotate_c1(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, int type);
void kanna_rotate_c2(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, int type);
void kanna_rotate_c3(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, int type);
void kanna_rotate_c4(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, int type);
// image pixel kanna rotate with stride(bytes-per-row) parameter
void kanna_rotate_c1(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride, int type);
void kanna_rotate_c2(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride, int type);
void kanna_rotate_c3(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride, int type);
void kanna_rotate_c4(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride, int type);
// image pixel kanna rotate, convenient wrapper for yuv420sp(nv21)
void kanna_rotate_yuv420sp(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h, int type);
#endif // NCNN_PIXEL_ROTATE
// type conversion
// convert float to half precision floating point
unsigned short float32_to_float16(float value);
// convert half precision floating point to float
float float16_to_float32(unsigned short value);
// convert float to brain half
inline unsigned short float32_to_bfloat16(float value)
{
// 16 : 16
union { unsigned int u; float f; } tmp;
tmp.f = value;
return tmp.u >> 16;
}
// convert brain half to float
inline float bfloat16_to_float32(unsigned short value)
{
// 16 : 16
union { unsigned int u; float f; } tmp;
tmp.u = value << 16;
return tmp.f;
}
// mat process
enum BorderType
{
BORDER_CONSTANT = 0,
BORDER_REPLICATE = 1,
};
void copy_make_border(const Mat& src, Mat& dst, int top, int bottom, int left, int right, int type, float v, const Option& opt = Option());
void copy_cut_border(const Mat& src, Mat& dst, int top, int bottom, int left, int right, const Option& opt = Option());
void resize_bilinear(const Mat& src, Mat& dst, int w, int h, const Option& opt = Option());
void resize_bicubic(const Mat& src, Mat& dst, int w, int h, const Option& opt = Option());
void convert_packing(const Mat& src, Mat& dst, int elempack, const Option& opt = Option());
void cast_float32_to_float16(const Mat& src, Mat& dst, const Option& opt = Option());
void cast_float16_to_float32(const Mat& src, Mat& dst, const Option& opt = Option());
void cast_int8_to_float32(const Mat& src, Mat& dst, const Option& opt = Option());
void cast_float32_to_bfloat16(const Mat& src, Mat& dst, const Option& opt = Option());
void cast_bfloat16_to_float32(const Mat& src, Mat& dst, const Option& opt = Option());
void quantize_float32_to_int8(const Mat& src, Mat& dst, float scale, const Option& opt = Option());
void dequantize_int32_to_float32(Mat& m, float scale, const float* bias, int bias_data_size, const Option& opt = Option());
void requantize_int8_to_int8(const Mat& src, Mat& dst, float scale_in, float scale_out, const float* bias, int bias_data_size, int fusion_relu, const Option& opt = Option());
inline Mat::Mat()
: data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0)
{
}
inline Mat::Mat(int _w, size_t _elemsize, Allocator* _allocator)
: data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0)
{
create(_w, _elemsize, _allocator);
}
inline Mat::Mat(int _w, int _h, size_t _elemsize, Allocator* _allocator)
: data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0)
{
create(_w, _h, _elemsize, _allocator);
}
inline Mat::Mat(int _w, int _h, int _c, size_t _elemsize, Allocator* _allocator)
: data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0)
{
create(_w, _h, _c, _elemsize, _allocator);
}
inline Mat::Mat(int _w, size_t _elemsize, int _elempack, Allocator* _allocator)
: data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0)
{
create(_w, _elemsize, _elempack, _allocator);
}
inline Mat::Mat(int _w, int _h, size_t _elemsize, int _elempack, Allocator* _allocator)
: data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0)
{
create(_w, _h, _elemsize, _elempack, _allocator);
}
inline Mat::Mat(int _w, int _h, int _c, size_t _elemsize, int _elempack, Allocator* _allocator)
: data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), c(0), cstep(0)
{
create(_w, _h, _c, _elemsize, _elempack, _allocator);
}
inline Mat::Mat(const Mat& m)
: data(m.data), refcount(m.refcount), elemsize(m.elemsize), elempack(m.elempack), allocator(m.allocator), dims(m.dims), w(m.w), h(m.h), c(m.c), cstep(m.cstep)
{
if (refcount)
NCNN_XADD(refcount, 1);
}
inline Mat::Mat(int _w, void* _data, size_t _elemsize, Allocator* _allocator)
: data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(1), w(_w), h(1), c(1)
{
cstep = w;
}
inline Mat::Mat(int _w, int _h, void* _data, size_t _elemsize, Allocator* _allocator)
: data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(2), w(_w), h(_h), c(1)
{
cstep = w * h;
}
inline Mat::Mat(int _w, int _h, int _c, void* _data, size_t _elemsize, Allocator* _allocator)
: data(_data), refcount(0), elemsize(_elemsize), elempack(1), allocator(_allocator), dims(3), w(_w), h(_h), c(_c)
{
cstep = alignSize(w * h * elemsize, 16) / elemsize;
}
inline Mat::Mat(int _w, void* _data, size_t _elemsize, int _elempack, Allocator* _allocator)
: data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(1), w(_w), h(1), c(1)
{
cstep = w;
}
inline Mat::Mat(int _w, int _h, void* _data, size_t _elemsize, int _elempack, Allocator* _allocator)
: data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(2), w(_w), h(_h), c(1)
{
cstep = w * h;
}
inline Mat::Mat(int _w, int _h, int _c, void* _data, size_t _elemsize, int _elempack, Allocator* _allocator)
: data(_data), refcount(0), elemsize(_elemsize), elempack(_elempack), allocator(_allocator), dims(3), w(_w), h(_h), c(_c)
{
cstep = alignSize(w * h * elemsize, 16) / elemsize;
}
inline Mat::~Mat()
{
release();
}
inline Mat& Mat::operator=(const Mat& m)
{
if (this == &m)
return *this;
if (m.refcount)
NCNN_XADD(m.refcount, 1);
release();
data = m.data;
refcount = m.refcount;
elemsize = m.elemsize;
elempack = m.elempack;
allocator = m.allocator;
dims = m.dims;
w = m.w;
h = m.h;
c = m.c;
cstep = m.cstep;
return *this;
}
inline void Mat::fill(float _v)
{
int size = (int)total();
float* ptr = (float*)data;
#if __ARM_NEON
int nn = size >> 2;
int remain = size - (nn << 2);
#else
int remain = size;
#endif // __ARM_NEON
#if __ARM_NEON
float32x4_t _c = vdupq_n_f32(_v);
#if __aarch64__
if (nn > 0)
{
asm volatile (
"0: \n"
"subs %w0, %w0, #1 \n"
"st1 {%4.4s}, [%1], #16 \n"
"bne 0b \n"
: "=r"(nn), // %0
"=r"(ptr) // %1
: "0"(nn),
"1"(ptr),
"w"(_c) // %4
: "cc", "memory"
);
}
#else
if (nn > 0)
{
asm volatile(
"0: \n"
"subs %0, #1 \n"
"vst1.f32 {%e4-%f4}, [%1 :128]!\n"
"bne 0b \n"
: "=r"(nn), // %0
"=r"(ptr) // %1
: "0"(nn),
"1"(ptr),
"w"(_c) // %4
: "cc", "memory"
);
}
#endif // __aarch64__
#endif // __ARM_NEON
for (; remain>0; remain--)
{
*ptr++ = _v;
}
}
inline void Mat::fill(int _v)
{
int size = (int)total();
int* ptr = (int*)data;
#if __ARM_NEON
int nn = size >> 2;
int remain = size - (nn << 2);
#else
int remain = size;
#endif // __ARM_NEON
#if __ARM_NEON
int32x4_t _c = vdupq_n_s32(_v);
#if __aarch64__
if (nn > 0)
{
asm volatile (
"0: \n"
"subs %w0, %w0, #1 \n"
"st1 {%4.4s}, [%1], #16 \n"
"bne 0b \n"
: "=r"(nn), // %0
"=r"(ptr) // %1
: "0"(nn),
"1"(ptr),
"w"(_c) // %4
: "cc", "memory"
);
}
#else
if (nn > 0)
{
asm volatile(
"0: \n"
"subs %0, #1 \n"
"vst1.s32 {%e4-%f4}, [%1 :128]!\n"
"bne 0b \n"
: "=r"(nn), // %0
"=r"(ptr) // %1
: "0"(nn),
"1"(ptr),
"w"(_c) // %4
: "cc", "memory"
);
}
#endif // __aarch64__
#endif // __ARM_NEON
for (; remain>0; remain--)
{
*ptr++ = _v;
}
}
#if __ARM_NEON
inline void Mat::fill(float32x4_t _v)
{
int size = total();
float* ptr = (float*)data;
for (int i=0; i<size; i++)
{
vst1q_f32(ptr, _v);
ptr += 4;
}
}
#endif // __ARM_NEON
template <typename T>
inline void Mat::fill(T _v)
{
int size = total();
T* ptr = (T*)data;
for (int i=0; i<size; i++)
{
ptr[i] = _v;
}
}
inline Mat Mat::clone(Allocator* allocator) const
{
if (empty())
return Mat();
Mat m;
if (dims == 1)
m.create(w, elemsize, elempack, allocator);
else if (dims == 2)
m.create(w, h, elemsize, elempack, allocator);
else if (dims == 3)
m.create(w, h, c, elemsize, elempack, allocator);
if (total() > 0)
{
memcpy(m.data, data, total() * elemsize);
}
return m;
}
inline Mat Mat::reshape(int _w, Allocator* _allocator) const
{
if (w * h * c != _w)
return Mat();
if (dims == 3 && cstep != (size_t)w * h)
{
Mat m;
m.create(_w, elemsize, elempack, _allocator);
// flatten
for (int i=0; i<c; i++)
{
const void* ptr = (unsigned char*)data + i * cstep * elemsize;
void* mptr = (unsigned char*)m.data + i * w * h * elemsize;
memcpy(mptr, ptr, w * h * elemsize);
}
return m;
}
Mat m = *this;
m.dims = 1;
m.w = _w;
m.h = 1;
m.c = 1;
m.cstep = _w;
return m;
}
inline Mat Mat::reshape(int _w, int _h, Allocator* _allocator) const
{
if (w * h * c != _w * _h)
return Mat();
if (dims == 3 && cstep != (size_t)w * h)
{
Mat m;
m.create(_w, _h, elemsize, elempack, _allocator);
// flatten
for (int i=0; i<c; i++)
{
const void* ptr = (unsigned char*)data + i * cstep * elemsize;
void* mptr = (unsigned char*)m.data + i * w * h * elemsize;
memcpy(mptr, ptr, w * h * elemsize);
}
return m;
}
Mat m = *this;
m.dims = 2;
m.w = _w;
m.h = _h;
m.c = 1;
m.cstep = _w * _h;
return m;
}
inline Mat Mat::reshape(int _w, int _h, int _c, Allocator* _allocator) const
{
if (w * h * c != _w * _h * _c)
return Mat();
if (dims < 3)
{
if ((size_t)_w * _h != alignSize(_w * _h * elemsize, 16) / elemsize)
{
Mat m;
m.create(_w, _h, _c, elemsize, elempack, _allocator);
// align channel
for (int i=0; i<_c; i++)
{
const void* ptr = (unsigned char*)data + i * _w * _h * elemsize;
void* mptr = (unsigned char*)m.data + i * m.cstep * m.elemsize;
memcpy(mptr, ptr, _w * _h * elemsize);
}
return m;
}
}
else if (c != _c)
{
// flatten and then align
Mat tmp = reshape(_w * _h * _c, _allocator);
return tmp.reshape(_w, _h, _c, _allocator);
}
Mat m = *this;
m.dims = 3;
m.w = _w;
m.h = _h;
m.c = _c;
m.cstep = alignSize(_w * _h * elemsize, 16) / elemsize;
return m;
}
inline void Mat::create(int _w, size_t _elemsize, Allocator* _allocator)
{
if (dims == 1 && w == _w && elemsize == _elemsize && elempack == 1 && allocator == _allocator)
return;
release();
elemsize = _elemsize;
elempack = 1;
allocator = _allocator;
dims = 1;
w = _w;
h = 1;
c = 1;
cstep = w;
if (total() > 0)
{
size_t totalsize = alignSize(total() * elemsize, 4);
if (allocator)
data = allocator->fastMalloc(totalsize + (int)sizeof(*refcount));
else
data = fastMalloc(totalsize + (int)sizeof(*refcount));
refcount = (int*)(((unsigned char*)data) + totalsize);
*refcount = 1;
}
}
inline void Mat::create(int _w, int _h, size_t _elemsize, Allocator* _allocator)
{
if (dims == 2 && w == _w && h == _h && elemsize == _elemsize && elempack == 1 && allocator == _allocator)
return;
release();
elemsize = _elemsize;
elempack = 1;
allocator = _allocator;
dims = 2;
w = _w;
h = _h;
c = 1;
cstep = w * h;
if (total() > 0)
{
size_t totalsize = alignSize(total() * elemsize, 4);
if (allocator)
data = allocator->fastMalloc(totalsize + (int)sizeof(*refcount));
else
data = fastMalloc(totalsize + (int)sizeof(*refcount));
refcount = (int*)(((unsigned char*)data) + totalsize);
*refcount = 1;
}
}
inline void Mat::create(int _w, int _h, int _c, size_t _elemsize, Allocator* _allocator)
{
if (dims == 3 && w == _w && h == _h && c == _c && elemsize == _elemsize && elempack == 1 && allocator == _allocator)
return;
release();
elemsize = _elemsize;
elempack = 1;
allocator = _allocator;
dims = 3;
w = _w;
h = _h;
c = _c;
cstep = alignSize(w * h * elemsize, 16) / elemsize;
if (total() > 0)
{
size_t totalsize = alignSize(total() * elemsize, 4);
if (allocator)
data = allocator->fastMalloc(totalsize + (int)sizeof(*refcount));
else
data = fastMalloc(totalsize + (int)sizeof(*refcount));
refcount = (int*)(((unsigned char*)data) + totalsize);
*refcount = 1;
}
}
inline void Mat::create(int _w, size_t _elemsize, int _elempack, Allocator* _allocator)
{
if (dims == 1 && w == _w && elemsize == _elemsize && elempack == _elempack && allocator == _allocator)
return;