VPI - Vision Programming Interface

0.4.4 Release

FormatUtils.h
1 /*
2  * Copyright 2020 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 /* |10 09 08 07 06|05 04|03 02 01 00|
59  * | BPP/8 |#CH-1| PACK |
60  */
61 #define VPI_DETAIL_FORMAT_SET_BPP_CHCOUNT(bpp, chcount) \
62  (VPI_DETAIL_SET_BITFIELD((bpp) / 8, 6, 5) | VPI_DETAIL_SET_BITFIELD((chcount)-1, 4, 2) | \
63  VPI_DETAIL_SET_BITFIELD((bpp) % 8, 0, 4))
64 
65 #define VPI_DETAIL_MAKE_SWIZZLE_HELPER(x, y, z, w) \
66  (VPI_DETAIL_SET_BITFIELD(x, 0, 3) | VPI_DETAIL_SET_BITFIELD(y, 3, 3) | VPI_DETAIL_SET_BITFIELD(z, 6, 3) | \
67  VPI_DETAIL_SET_BITFIELD(w, 9, 3))
68 
69 #define VPI_DETAIL_MAKE_SWIZZLE(x, y, z, w) \
70  VPI_DETAIL_MAKE_SWIZZLE_HELPER(VPI_SWIZZLE_CHANNEL_##x, VPI_SWIZZLE_CHANNEL_##y, VPI_SWIZZLE_CHANNEL_##z, \
71  VPI_SWIZZLE_CHANNEL_##w)
72 
73 #define VPI_DETAIL_DEF_SWIZZLE_ENUM(x, y, z, w) VPI_SWIZZLE_##x##y##z##w = VPI_DETAIL_MAKE_SWIZZLE(x, y, z, w)
74 
75 #define VPI_DETAIL_FORMAT_SET_COLOR_MODEL_HELPER(model) VPI_DETAIL_SET_BITFIELD((model), 4, 2)
76 
77 #define VPI_DETAIL_FORMAT_SET_COLOR_MODEL(model) VPI_DETAIL_FORMAT_SET_COLOR_MODEL_HELPER(VPI_COLOR_MODEL_##model)
78 
79 #define VPI_DETAIL_MAKE_CHROMA_SUBSAMPLING(DivHoriz, DivVert, CenterSampleHoriz, CenterSampleVert) \
80  (VPI_DETAIL_SET_BITFIELD((DivHoriz)-1, 0, 2) | VPI_DETAIL_SET_BITFIELD((DivVert - 1), 2, 2) | \
81  VPI_DETAIL_SET_BITFIELD((CenterSampleHoriz), 4, 1) | VPI_DETAIL_SET_BITFIELD((CenterSampleVert), 5, 1))
82 
83 /* |63 62 61 60|59 58 57 56|55|54 53 52 51|50 49 48 47|46 45|44 43 42 41|40 39 38 37 36|35 34|33 32 -
84  * | BPP3 | BPP2 +C2+ Pack2 | BPP1 +Chan1+ Pack1 | BPP0 +Chan0+Pack0 -
85  *
86  * - 31 30|29|28|27 26|25 24|23 22|21 20 19 18|17 16 15|14 13 12|11 10 09|08 07 06|05 04 03|02 01 00|
87  * - Pack0|CV+CH+DivV +DivH |Model+Colorspace | MemLay |SwizzleW|SwizzleZ|SwizzleY|SwizzleX| Data |
88  */
89 
90 #define VPI_DETAIL_ENCODE_PACKING(P, BPPLEN, CHLEN, PACKLEN) \
91  (VPI_DETAIL_SET_BITFIELD(VPI_DETAIL_GET_BITFIELD(P, 6, 5), (PACKLEN) + (CHLEN), BPPLEN) | \
92  VPI_DETAIL_SET_BITFIELD(VPI_DETAIL_GET_BITFIELD(P, 4, 2), PACKLEN, CHLEN) | \
93  VPI_DETAIL_SET_BITFIELD(VPI_DETAIL_GET_BITFIELD(P, 0, 4), 0, PACKLEN))
94 
95 #define VPI_DETAIL_MAKE_FORMAT_HELPER(ColorSpace, Subsampling, MemLayout, DataType, Swizzle, Packing0, Packing1, \
96  Packing2, Packing3) \
97  (VPI_DETAIL_SET_BITFIELD(DataType, 0, 3) | VPI_DETAIL_SET_BITFIELD(Swizzle, 3, 4 * 3) | \
98  VPI_DETAIL_SET_BITFIELD(MemLayout, 15, 3) | VPI_DETAIL_SET_BITFIELD(ColorSpace, 18, 6) | \
99  VPI_DETAIL_SET_BITFIELD(Subsampling, 24, 6) | \
100  VPI_DETAIL_SET_BITFIELD(VPI_DETAIL_ENCODE_PACKING(Packing0, 5, 2, 4), 30, 11) | \
101  VPI_DETAIL_SET_BITFIELD(VPI_DETAIL_ENCODE_PACKING(Packing1, 4, 2, 4), 41, 10) | \
102  VPI_DETAIL_SET_BITFIELD(VPI_DETAIL_ENCODE_PACKING(Packing2, 4, 1, 4), 51, 9) | \
103  VPI_DETAIL_SET_BITFIELD(VPI_DETAIL_ENCODE_PACKING(Packing3, 4, 0, 0), 60, 4))
104 
105 #define VPI_DETAIL_MAKE_FORMAT_4(ColorSpace, Subsampling, MemLayout, DataType, Swizzle, Packing0, Packing1, Packing2, \
106  Packing3) \
107  VPI_DETAIL_MAKE_FORMAT_HELPER(VPI_COLOR_SPACE_##ColorSpace, VPI_CHROMA_SUBSAMPLING_##Subsampling, \
108  VPI_MEM_LAYOUT_##MemLayout, VPI_FORMAT_DATA_TYPE_##DataType, VPI_SWIZZLE_##Swizzle, \
109  VPI_FORMAT_PACKING_##Packing0, VPI_FORMAT_PACKING_##Packing1, \
110  VPI_FORMAT_PACKING_##Packing2, VPI_FORMAT_PACKING_##Packing3)
111 
112 #define VPI_DETAIL_MAKE_FORMAT_3(ColorSpace, Subsampling, MemLayout, DataType, Swizzle, Packing0, Packing1, Packing2) \
113  VPI_DETAIL_MAKE_FORMAT_4(ColorSpace, Subsampling, MemLayout, DataType, Swizzle, Packing0, Packing1, Packing2, 0)
114 
115 #define VPI_DETAIL_MAKE_FORMAT_2(ColorSpace, Subsampling, MemLayout, DataType, Swizzle, Packing0, Packing1) \
116  VPI_DETAIL_MAKE_FORMAT_3(ColorSpace, Subsampling, MemLayout, DataType, Swizzle, Packing0, Packing1, 0)
117 
118 #define VPI_DETAIL_MAKE_FORMAT_1(ColorSpace, Subsampling, MemLayout, DataType, Swizzle, Packing0) \
119  VPI_DETAIL_MAKE_FORMAT_2(ColorSpace, Subsampling, MemLayout, DataType, Swizzle, Packing0, 0)
120 
121 #define VPI_DETAIL_EXTRACT_PACKING_CHANNELS(Packing) (VPI_DETAIL_GET_BITFIELD(Packing, 4, 2) + 1)
122 
123 #define VPI_DETAIL_MAKE_PIXEL_FORMAT_HELPER(MemLayout, DataType, Packing) \
124  VPI_DETAIL_MAKE_FORMAT_HELPER( \
125  VPI_COLOR_SPACE_NONCOLOR, VPI_CHROMA_SUBSAMPLING_NONE, MemLayout, DataType, \
126  VPI_DETAIL_MAKE_SWIZZLE_HELPER(VPI_SWIZZLE_CHANNEL_X, \
127  VPI_DETAIL_EXTRACT_PACKING_CHANNELS(Packing) >= 2 ? VPI_SWIZZLE_CHANNEL_Y : 0, \
128  VPI_DETAIL_EXTRACT_PACKING_CHANNELS(Packing) >= 3 ? VPI_SWIZZLE_CHANNEL_Z : 0, \
129  VPI_DETAIL_EXTRACT_PACKING_CHANNELS(Packing) >= 4 ? VPI_SWIZZLE_CHANNEL_W : 0), \
130  Packing, 0, 0, 0)
131 
132 #define VPI_DETAIL_MAKE_PIXEL_FORMAT(MemLayout, DataType, Packing) \
133  VPI_DETAIL_MAKE_PIXEL_FORMAT_HELPER(VPI_MEM_LAYOUT_##MemLayout, VPI_FORMAT_DATA_TYPE_##DataType, \
134  VPI_FORMAT_PACKING_##Packing)
135 
136 #define VPI_DETAIL_MAKE_IMAGE_FORMAT(ColorSpace, Subsampling, MemLayout, DataType, Swizzle, PlaneCount, ...) \
137  VPI_DETAIL_MAKE_FORMAT_##PlaneCount(ColorSpace, Subsampling, MemLayout, DataType, Swizzle, __VA_ARGS__)
138 
139 #endif // NV_VPI_DETAIL_FORMATUTILS_H