VPI - Vision Programming Interface

1.1 Release

FormatUtils.h
1 /*
2  * Copyright 2020-2021 NVIDIA Corporation. All rights reserved.
3  *
4  * NOTICE TO LICENSEE:
5  *
6  * This source code and/or documentation ("Licensed Deliverables") are
7  * subject to NVIDIA intellectual property rights under U.S. and
8  * international Copyright laws.
9  *
10  * These Licensed Deliverables contained herein is PROPRIETARY and
11  * CONFIDENTIAL to NVIDIA and is being provided under the terms and
12  * conditions of a form of NVIDIA software license agreement by and
13  * between NVIDIA and Licensee ("License Agreement") or electronically
14  * accepted by Licensee. Notwithstanding any terms or conditions to
15  * the contrary in the License Agreement, reproduction or disclosure
16  * of the Licensed Deliverables to any third party without the express
17  * written consent of NVIDIA is prohibited.
18  *
19  * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
20  * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE
21  * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS
22  * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND.
23  * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED
24  * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY,
25  * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
26  * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
27  * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY
28  * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY
29  * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
30  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
31  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
32  * OF THESE LICENSED DELIVERABLES.
33  *
34  * U.S. Government End Users. These Licensed Deliverables are a
35  * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT
36  * 1995), consisting of "commercial computer software" and "commercial
37  * computer software documentation" as such terms are used in 48
38  * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government
39  * only as a commercial end item. Consistent with 48 C.F.R.12.212 and
40  * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all
41  * U.S. Government End Users acquire the Licensed Deliverables with
42  * only those rights set forth herein.
43  *
44  * Any use of the Licensed Deliverables in individual and commercial
45  * software must include, in the user documentation and internal
46  * comments to the code, the above Disclaimer and U.S. Government End
47  * Users Notice.
48  */
49 
50 #ifndef NV_VPI_DETAIL_FORMATUTILS_H
51 #define NV_VPI_DETAIL_FORMATUTILS_H
52 
53 #include <stdint.h>
54 
55 #define VPI_DETAIL_SET_BITFIELD(value, offset, size) (((uint64_t)(value) & ((1ULL << (size)) - 1)) << (offset))
56 #define VPI_DETAIL_GET_BITFIELD(value, offset, size) (((uint64_t)(value) >> (offset)) & ((1ULL << (size)) - 1))
57 
58 #define VPI_DETAIL_ENCODE_BPP(bpp) \
59  ((bpp) <= 8 ? 0 \
60  : ((bpp) <= 32 ? (bpp) / 8 - 1 \
61  : ((bpp) <= 64 ? (bpp) / 16 + 1 : ((bpp) <= 128 ? (bpp) / 32 + 3 : (bpp) / 64 + 5))))
62 
63 #define VPI_DETAIL_BPP_NCH(bpp, chcount) \
64  (VPI_DETAIL_SET_BITFIELD(VPI_DETAIL_ENCODE_BPP(bpp), 6, 4) | VPI_DETAIL_SET_BITFIELD((chcount)-1, 4, 2) | \
65  VPI_DETAIL_SET_BITFIELD((bpp) <= 2 ? (bpp) : ((bpp) == 4 ? 3 : ((bpp) == 8 ? 4 : 0)), 0, 4))
66 
67 #define VPI_DETAIL_MAKE_SWIZZLE(x, y, z, w) \
68  (VPI_DETAIL_SET_BITFIELD(x, 0, 3) | VPI_DETAIL_SET_BITFIELD(y, 3, 3) | VPI_DETAIL_SET_BITFIELD(z, 6, 3) | \
69  VPI_DETAIL_SET_BITFIELD(w, 9, 3))
70 
71 #define VPI_DETAIL_MAKE_SWZL(x, y, z, w) \
72  VPI_DETAIL_MAKE_SWIZZLE(VPI_CHANNEL_##x, VPI_CHANNEL_##y, VPI_CHANNEL_##z, VPI_CHANNEL_##w)
73 
74 #define VPI_DETAIL_DEF_SWIZZLE_ENUM(x, y, z, w) VPI_SWIZZLE_##x##y##z##w = VPI_DETAIL_MAKE_SWZL(x, y, z, w)
75 
76 #define VPI_DETAIL_ADJUST_BPP_ENCODING(PACK, BPP, PACKLEN) \
77  ((PACKLEN) == 0 && (BPP) == 0 && (PACK) == 4 ? (uint64_t)-1 : (BPP))
78 
79 #define VPI_DETAIL_ENCODE_PACKING(P, CHLEN, PACKLEN, BPPLEN) \
80  (VPI_DETAIL_SET_BITFIELD( \
81  VPI_DETAIL_ADJUST_BPP_ENCODING(VPI_DETAIL_GET_BITFIELD(P, 0, 4), VPI_DETAIL_GET_BITFIELD(P, 6, 4), PACKLEN), \
82  (PACKLEN) + (CHLEN), BPPLEN) | \
83  VPI_DETAIL_SET_BITFIELD(VPI_DETAIL_GET_BITFIELD(P, 4, 2), PACKLEN, CHLEN) | \
84  VPI_DETAIL_SET_BITFIELD(VPI_DETAIL_GET_BITFIELD(P, 0, 4), 0, PACKLEN))
85 
86 #define VPI_DETAIL_HANDLE_DATATYPE_SIGNEDNESS(DataType, rest) \
87  (VPI_DETAIL_GET_BITFIELD(DataType, 2, 1) ? -(1 + (int64_t)((~(uint64_t)(rest)) & ~(1ULL << 63))) : (int64_t)(rest))
88 
89 /* clang-format off */
90 #define VPI_DETAIL_MAKE_FMT_HELPER(ColorModel, ColorSpecOrRawPattern, Subsampling, MemLayout, DataType, Swizzle, \
91  Packing0, Packing1, Packing2, Packing3) \
92  ((int64_t)VPI_DETAIL_HANDLE_DATATYPE_SIGNEDNESS( \
93  DataType, \
94  VPI_DETAIL_SET_BITFIELD(((DataType)&0x3), 61, 2) | VPI_DETAIL_SET_BITFIELD(Swizzle, 0, 4 * 3) | \
95  VPI_DETAIL_SET_BITFIELD(MemLayout, 12, 3) | \
96  ((ColorModel) == VPI_COLOR_MODEL_YCbCr \
97  ? VPI_DETAIL_SET_BITFIELD(ColorSpecOrRawPattern, 20, 15) | VPI_DETAIL_SET_BITFIELD(Subsampling, 17, 3) \
98  : ((ColorModel) == VPI_COLOR_MODEL_UNDEFINED \
99  ? VPI_DETAIL_SET_BITFIELD((1U<<19)-1, 16, 19) \
100  : (VPI_DETAIL_SET_BITFIELD(1,16,1) | \
101  ((ColorModel)-2 < 0x7 \
102  ? VPI_DETAIL_SET_BITFIELD(ColorSpecOrRawPattern, 20, 15) \
103  | VPI_DETAIL_SET_BITFIELD((ColorModel)-2, 17, 3) \
104  : (VPI_DETAIL_SET_BITFIELD(0x7, 17, 3) | \
105  ((ColorModel) == VPI_COLOR_MODEL_RAW \
106  ? VPI_DETAIL_SET_BITFIELD(ColorSpecOrRawPattern, 21, 6) \
107  : (VPI_DETAIL_SET_BITFIELD(1, 20, 1) | VPI_DETAIL_SET_BITFIELD((ColorModel)-(7+2+1), 21, 6)) \
108  ) \
109  ) \
110  ) \
111  ) \
112  ) \
113  ) | \
114  VPI_DETAIL_SET_BITFIELD(VPI_DETAIL_ENCODE_PACKING(Packing0, 2, 3, 4), 35, 9) | \
115  VPI_DETAIL_SET_BITFIELD(VPI_DETAIL_ENCODE_PACKING(Packing1, 1, 3, 3), 44, 7) | \
116  VPI_DETAIL_SET_BITFIELD(VPI_DETAIL_ENCODE_PACKING(Packing2, 1, 3, 3), 51, 7) | \
117  VPI_DETAIL_SET_BITFIELD(VPI_DETAIL_ENCODE_PACKING(Packing3, 0, 0, 3), 58, 3)))
118 /* clang-format on */
119 
120 #define VPI_DETAIL_MAKE_COLOR_FMT_HELPER VPI_DETAIL_MAKE_FMT_HELPER
121 
122 #define VPI_DETAIL_MAKE_COLOR_FMT_HELPER_ABBREV(ColorModel, ColorSpec, Subsampling, MemLayout, DataType, Swizzle, \
123  Packing0, Packing1, Packing2, Packing3) \
124  VPI_DETAIL_MAKE_FMT_HELPER(VPI_COLOR_MODEL_##ColorModel, VPI_COLOR_SPEC_##ColorSpec, VPI_CSS_##Subsampling, \
125  VPI_MEM_LAYOUT_##MemLayout, VPI_DATA_TYPE_##DataType, VPI_SWIZZLE_##Swizzle, \
126  VPI_PACKING_##Packing0, VPI_PACKING_##Packing1, VPI_PACKING_##Packing2, \
127  VPI_PACKING_##Packing3)
128 
129 #define VPI_DETAIL_MAKE_COLOR_FMT_HELPER_4(TYPE, ...) VPI_DETAIL_MAKE_COLOR_FMT_HELPER##TYPE(__VA_ARGS__)
130 
131 #define VPI_DETAIL_MAKE_COLOR_FMT_HELPER_3(TYPE, ColorModel, ColorSpec, Subsampling, MemLayout, DataType, Swizzle, \
132  Packing0, Packing1, Packing2) \
133  VPI_DETAIL_MAKE_COLOR_FMT_HELPER_4(TYPE, ColorModel, ColorSpec, Subsampling, MemLayout, DataType, Swizzle, \
134  Packing0, Packing1, Packing2, 0)
135 
136 #define VPI_DETAIL_MAKE_COLOR_FMT_HELPER_2(TYPE, ColorModel, ColorSpec, Subsampling, MemLayout, DataType, Swizzle, \
137  Packing0, Packing1) \
138  VPI_DETAIL_MAKE_COLOR_FMT_HELPER_3(TYPE, ColorModel, ColorSpec, Subsampling, MemLayout, DataType, Swizzle, \
139  Packing0, Packing1, 0)
140 
141 #define VPI_DETAIL_MAKE_COLOR_FMT_HELPER_1(TYPE, ColorModel, ColorSpec, Subsampling, MemLayout, DataType, Swizzle, \
142  Packing0) \
143  VPI_DETAIL_MAKE_COLOR_FMT_HELPER_2(TYPE, ColorModel, ColorSpec, Subsampling, MemLayout, DataType, Swizzle, \
144  Packing0, 0)
145 
146 #define VPI_DETAIL_EXTRACT_PACKING_CHANNELS(Packing) (VPI_DETAIL_GET_BITFIELD(Packing, 4, 2) + 1)
147 
148 #define VPI_DETAIL_MAKE_PIXEL_TYPE(MemLayout, DataType, Packing) \
149  VPI_DETAIL_MAKE_FMT_HELPER( \
150  VPI_COLOR_MODEL_UNDEFINED, VPI_COLOR_SPEC_UNDEFINED, VPI_CSS_NONE, MemLayout, DataType, \
151  VPI_DETAIL_MAKE_SWIZZLE(VPI_CHANNEL_X, VPI_DETAIL_EXTRACT_PACKING_CHANNELS(Packing) >= 2 ? VPI_CHANNEL_Y : 0, \
152  VPI_DETAIL_EXTRACT_PACKING_CHANNELS(Packing) >= 3 ? VPI_CHANNEL_Z : 0, \
153  VPI_DETAIL_EXTRACT_PACKING_CHANNELS(Packing) >= 4 ? VPI_CHANNEL_W : 0), \
154  Packing, 0, 0, 0)
155 
156 #define VPI_DETAIL_MAKE_PIXEL_FMT(MemLayout, DataType, Packing) \
157  VPI_DETAIL_MAKE_PIXEL_TYPE(VPI_MEM_LAYOUT_##MemLayout, VPI_DATA_TYPE_##DataType, VPI_PACKING_##Packing)
158 
159 #define VPI_DETAIL_MAKE_COLOR_FMT(ColorModel, ColorSpec, MemLayout, DataType, Swizzle, PlaneCount, ...) \
160  VPI_DETAIL_MAKE_COLOR_FMT_HELPER_##PlaneCount(_ABBREV, ColorModel, ColorSpec, NONE, MemLayout, DataType, Swizzle, \
161  __VA_ARGS__)
162 
163 #define VPI_DETAIL_MAKE_COLOR_FORMAT(ColorModel, ColorSpec, MemLayout, DataType, Swizzle, PlaneCount, ...) \
164  VPI_DETAIL_MAKE_COLOR_FMT_HELPER_##PlaneCount(, ColorModel, ColorSpec, VPI_CSS_NONE, MemLayout, DataType, Swizzle, \
165  __VA_ARGS__)
166 
167 #define VPI_DETAIL_MAKE_NONCOLOR_FMT(MemLayout, DataType, Swizzle, PlaneCount, ...) \
168  VPI_DETAIL_MAKE_COLOR_FMT_HELPER_##PlaneCount(_ABBREV, UNDEFINED, UNDEFINED, NONE, MemLayout, DataType, Swizzle, \
169  __VA_ARGS__)
170 
171 #define VPI_DETAIL_MAKE_NONCOLOR_FORMAT(MemLayout, DataType, Swizzle, PlaneCount, ...) \
172  VPI_DETAIL_MAKE_COLOR_FMT_HELPER_##PlaneCount(, VPI_COLOR_MODEL_UNDEFINED, VPI_COLOR_SPEC_UNDEFINED, VPI_CSS_NONE, \
173  MemLayout, DataType, Swizzle, __VA_ARGS__)
174 
175 #define VPI_DETAIL_MAKE_RAW_FMT_HELPER(RawPattern, MemLayout, DataType, Swizzle, Packing0, Packing1, Packing2, \
176  Packing3) \
177  VPI_DETAIL_MAKE_FMT_HELPER(VPI_COLOR_MODEL_RAW, RawPattern, VPI_CSS_NONE, MemLayout, DataType, Swizzle, Packing0, \
178  Packing1, Packing2, Packing3)
179 
180 #define VPI_DETAIL_MAKE_RAW_FMT_HELPER_ABBREV(RawPattern, MemLayout, DataType, Swizzle, Packing0, Packing1, Packing2, \
181  Packing3) \
182  VPI_DETAIL_MAKE_RAW_FMT_HELPER(VPI_RAW_##RawPattern, VPI_MEM_LAYOUT_##MemLayout, VPI_DATA_TYPE_##DataType, \
183  VPI_SWIZZLE_##Swizzle, VPI_PACKING_##Packing0, VPI_PACKING_##Packing1, \
184  VPI_PACKING_##Packing2, VPI_PACKING_##Packing3)
185 
186 #define VPI_DETAIL_MAKE_RAW_FMT_HELPER_4(TYPE, RawPattern, MemLayout, DataType, Swizzle, Packing0, Packing1, Packing2, \
187  Packing3) \
188  VPI_DETAIL_MAKE_RAW_FMT_HELPER##TYPE(RawPattern, MemLayout, DataType, Swizzle, Packing0, Packing1, Packing2, \
189  Packing3)
190 
191 #define VPI_DETAIL_MAKE_RAW_FMT_HELPER_3(TYPE, RawPattern, MemLayout, DataType, Swizzle, Packing0, Packing1, Packing2) \
192  VPI_DETAIL_MAKE_RAW_FMT_HELPER_4(TYPE, RawPattern, MemLayout, DataType, Swizzle, Packing0, Packing1, Packing2, 0)
193 
194 #define VPI_DETAIL_MAKE_RAW_FMT_HELPER_2(TYPE, RawPattern, MemLayout, DataType, Swizzle, Packing0, Packing1) \
195  VPI_DETAIL_MAKE_RAW_FMT_HELPER_3(TYPE, RawPattern, MemLayout, DataType, Swizzle, Packing0, Packing1, 0)
196 
197 #define VPI_DETAIL_MAKE_RAW_FMT_HELPER_1(TYPE, RawPattern, MemLayout, DataType, Swizzle, Packing0) \
198  VPI_DETAIL_MAKE_RAW_FMT_HELPER_2(TYPE, RawPattern, MemLayout, DataType, Swizzle, Packing0, 0)
199 
200 #define VPI_DETAIL_MAKE_RAW_FMT(RawPattern, MemLayout, DataType, Swizzle, PlaneCount, ...) \
201  VPI_DETAIL_MAKE_RAW_FMT_HELPER_##PlaneCount(_ABBREV, RawPattern, MemLayout, DataType, Swizzle, __VA_ARGS__)
202 
203 #define VPI_DETAIL_MAKE_RAW_FORMAT(RawPattern, MemLayout, DataType, Swizzle, PlaneCount, ...) \
204  VPI_DETAIL_MAKE_RAW_FMT_HELPER_##PlaneCount(, RawPattern, MemLayout, DataType, Swizzle, __VA_ARGS__)
205 
206 #define VPI_DETAIL_MAKE_YCbCr_FORMAT(ColorSpec, ChromaSubsamp, MemLayout, DataType, Swizzle, PlaneCount, ...) \
207  VPI_DETAIL_MAKE_COLOR_FMT_HELPER_##PlaneCount(, VPI_COLOR_MODEL_YCbCr, ColorSpec, ChromaSubsamp, MemLayout, \
208  DataType, Swizzle, __VA_ARGS__)
209 
210 #define VPI_DETAIL_MAKE_YCbCr_FMT(ColorSpec, ChromaSubsamp, MemLayout, DataType, Swizzle, PlaneCount, ...) \
211  VPI_DETAIL_MAKE_COLOR_FMT_HELPER_##PlaneCount(_ABBREV, YCbCr, ColorSpec, ChromaSubsamp, MemLayout, DataType, \
212  Swizzle, __VA_ARGS__)
213 
214 #define VPI_DETAIL_MAKE_COLOR_SPEC(CSpace, Encoding, XferFunc, Range, LocHoriz, LocVert) \
215  (VPI_DETAIL_SET_BITFIELD((CSpace), 0, 3) | VPI_DETAIL_SET_BITFIELD(XferFunc, 3, 4) | \
216  VPI_DETAIL_SET_BITFIELD(Encoding, 7, 3) | VPI_DETAIL_SET_BITFIELD(LocHoriz, 10, 2) | \
217  VPI_DETAIL_SET_BITFIELD(LocVert, 12, 2) | VPI_DETAIL_SET_BITFIELD(Range, 14, 1))
218 
219 #define VPI_DETAIL_MAKE_CSPC(CSpace, Encoding, XferFunc, Range, LocHoriz, LocVert) \
220  VPI_DETAIL_MAKE_COLOR_SPEC(VPI_COLOR_##CSpace, VPI_YCbCr_##Encoding, VPI_COLOR_##XferFunc, VPI_COLOR_##Range, \
221  VPI_CHROMA_##LocHoriz, VPI_CHROMA_##LocVert)
222 
223 #endif /* NV_VPI_DETAIL_FORMATUTILS_H */