1 /*
2 * Copyright (c) 2021 Rockchip Electronics Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef __MPP_COMMON_H__
17 #define __MPP_COMMON_H__
18
19 #include "rk_type.h"
20
21 #define MPP_TAG_SIZE 32
22
23 #define MPP_ABS(x) ((x) < (0) ? -(x) : (x))
24
25 #define MPP_MAX(a, b) ((a) > (b) ? (a) : (b))
26 #define MPP_MAX3(a, b, c) MPP_MAX(MPP_MAX(a, b), c)
27 #define MPP_MAX4(a, b, c, d) MPP_MAX((a), MPP_MAX3((b), (c), (d)))
28
29 #define MPP_MIN(a, b) ((a) > (b) ? (b) : (a))
30 #define MPP_MIN3(a, b, c) MPP_MIN(MPP_MIN(a, b), c)
31 #define MPP_MIN4(a, b, c, d) MPP_MIN((a), MPP_MIN3((b), (c), (d)))
32
33 #define MPP_DIV(a, b) ((b) ? (a) / (b) : (a))
34
35 #define MPP_CLIP3(l, h, v) ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
36 #define MPP_SIGN(a) ((a) < (0) ? (-1) : (1))
37 #define MPP_DIV_SIGN(a, b) (((a) + (MPP_SIGN(a) * (b)) / 2) / (b))
38
39 #define MPP_SWAP(type, a, b) do {type SWAP_tmp = b; b = a; a = SWAP_tmp; } while (0)
40 #define MPP_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
41 #define MPP_ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
42 #define MPP_VSWAP(a, b) { (a) ^= (b); (b) ^= (a); (a) ^= (b); }
43
44 #define MPP_RB16(x) ((((const RK_U8*)(x))[0] << 8) | ((const RK_U8*)(x))[1])
45 #define MPP_WB16(p, d) do { \
46 ((RK_U8*)(p))[1] = (d); \
47 ((RK_U8*)(p))[0] = (d)>>8; } while (0)
48
49 #define MPP_RL16(x) ((((const RK_U8*)(x))[1] << 8) | \
50 ((const RK_U8*)(x))[0])
51 #define MPP_WL16(p, d) do { \
52 ((RK_U8*)(p))[0] = (d); \
53 ((RK_U8*)(p))[1] = (d)>>8; } while (0)
54
55 #define MPP_RB32(x) ((((const RK_U8*)(x))[0] << 24) | \
56 (((const RK_U8*)(x))[1] << 16) | \
57 (((const RK_U8*)(x))[2] << 8) | \
58 ((const RK_U8*)(x))[3])
59 #define MPP_WB32(p, d) do { \
60 ((RK_U8*)(p))[3] = (d); \
61 ((RK_U8*)(p))[2] = (d)>>8; \
62 ((RK_U8*)(p))[1] = (d)>>16; \
63 ((RK_U8*)(p))[0] = (d)>>24; } while (0)
64
65 #define MPP_RL32(x) ((((const RK_U8*)(x))[3] << 24) | \
66 (((const RK_U8*)(x))[2] << 16) | \
67 (((const RK_U8*)(x))[1] << 8) | \
68 ((const RK_U8*)(x))[0])
69 #define MPP_WL32(p, d) do { \
70 ((RK_U8*)(p))[0] = (d); \
71 ((RK_U8*)(p))[1] = (d)>>8; \
72 ((RK_U8*)(p))[2] = (d)>>16; \
73 ((RK_U8*)(p))[3] = (d)>>24; } while (0)
74
75 #define MPP_RB64(x) (((RK_U64)((const RK_U8*)(x))[0] << 56) | \
76 ((RK_U64)((const RK_U8*)(x))[1] << 48) | \
77 ((RK_U64)((const RK_U8*)(x))[2] << 40) | \
78 ((RK_U64)((const RK_U8*)(x))[3] << 32) | \
79 ((RK_U64)((const RK_U8*)(x))[4] << 24) | \
80 ((RK_U64)((const RK_U8*)(x))[5] << 16) | \
81 ((RK_U64)((const RK_U8*)(x))[6] << 8) | \
82 (RK_U64)((const RK_U8*)(x))[7])
83 #define MPP_WB64(p, d) do { \
84 ((RK_U8*)(p))[7] = (d); \
85 ((RK_U8*)(p))[6] = (d)>>8; \
86 ((RK_U8*)(p))[5] = (d)>>16; \
87 ((RK_U8*)(p))[4] = (d)>>24; \
88 ((RK_U8*)(p))[3] = (d)>>32; \
89 ((RK_U8*)(p))[2] = (d)>>40; \
90 ((RK_U8*)(p))[1] = (d)>>48; \
91 ((RK_U8*)(p))[0] = (d)>>56; } while (0)
92
93 #define MPP_RL64(x) (((RK_U64)((const RK_U8*)(x))[7] << 56) | \
94 ((RK_U64)((const RK_U8*)(x))[6] << 48) | \
95 ((RK_U64)((const RK_U8*)(x))[5] << 40) | \
96 ((RK_U64)((const RK_U8*)(x))[4] << 32) | \
97 ((RK_U64)((const RK_U8*)(x))[3] << 24) | \
98 ((RK_U64)((const RK_U8*)(x))[2] << 16) | \
99 ((RK_U64)((const RK_U8*)(x))[1] << 8) | \
100 (RK_U64)((const RK_U8*)(x))[0])
101 #define MPP_WL64(p, d) do { \
102 ((RK_U8*)(p))[0] = (d); \
103 ((RK_U8*)(p))[1] = (d)>>8; \
104 ((RK_U8*)(p))[2] = (d)>>16; \
105 ((RK_U8*)(p))[3] = (d)>>24; \
106 ((RK_U8*)(p))[4] = (d)>>32; \
107 ((RK_U8*)(p))[5] = (d)>>40; \
108 ((RK_U8*)(p))[6] = (d)>>48; \
109 ((RK_U8*)(p))[7] = (d)>>56; } while (0)
110
111 #define MPP_RB24(x) ((((const RK_U8*)(x))[0] << 16) | \
112 (((const RK_U8*)(x))[1] << 8) | \
113 ((const RK_U8*)(x))[2])
114 #define MPP_WB24(p, d) do { \
115 ((RK_U8*)(p))[2] = (d); \
116 ((RK_U8*)(p))[1] = (d)>>8; \
117 ((RK_U8*)(p))[0] = (d)>>16; } while (0)
118
119 #define MPP_RL24(x) ((((const RK_U8*)(x))[2] << 16) | \
120 (((const RK_U8*)(x))[1] << 8) | \
121 ((const RK_U8*)(x))[0])
122
123 #define MPP_WL24(p, d) do { \
124 ((RK_U8*)(p))[0] = (d); \
125 ((RK_U8*)(p))[1] = (d)>>8; \
126 ((RK_U8*)(p))[2] = (d)>>16; } while (0)
127
128 #include <stdio.h>
129 #if defined(_WIN32) && !defined(__MINGW32CE__)
130 #define fseeko _fseeki64
131
132 #include <direct.h>
133 #include <io.h>
134 #include <sys/stat.h>
135 #define chdir _chdir
136 #define mkdir _mkdir
137 #define access _access
138 #define off_t _off_t
139
140 #define R_OK 4 /* Test for read permission. */
141 #define W_OK 2 /* Test for write permission. */
142 #define X_OK 1 /* Test for execute permission. */
143 #define F_OK 0 /* Test for existence. */
144
145 #elif defined(__MINGW32CE__)
146 #define fseeko fseeko64
147 #else
148 #include <unistd.h>
149 #include <stddef.h>
150 #include <sys/stat.h>
151 #include <sys/types.h>
152 #define mkdir(x) mkdir(x, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
153 #endif
154
155 #define container_of(ptr, type, member) \
156 ((type *)((char *)(ptr) - offsetof(type, member)))
157
158 #define RETURN __Return
159 #define FAILED __Failed
160
161 #define ARG_T(t) t
162 #define ARG_N(a, b, c, d, N, ...) N
163 #define ARG_N_HELPER(...) ARG_T(ARG_N(__VA_ARGS__))
164 #define COUNT_ARG(...) ARG_N_HELPER(__VA_ARGS__, 4, 3, 2, 1, 0)
165 #define SZ_POINTER (4)
166 #define SZ_1K (1024)
167 #define SZ_2K (SZ_1K*2)
168 #define SZ_4K (SZ_1K*4)
169 #define SZ_8K (SZ_1K*8)
170 #define SZ_16K (SZ_1K*16)
171 #define SZ_32K (SZ_1K*32)
172 #define SZ_64K (SZ_1K*64)
173 #define SZ_128K (SZ_1K*128)
174 #define SZ_256K (SZ_1K*256)
175 #define SZ_512K (SZ_1K*512)
176 #define SZ_1M (SZ_1K*SZ_1K)
177 #define SZ_2M (SZ_1M*2)
178 #define SZ_4M (SZ_1M*4)
179 #define SZ_8M (SZ_1M*8)
180 #define SZ_16M (SZ_1M*16)
181 #define SZ_32M (SZ_1M*32)
182 #define SZ_64M (SZ_1M*64)
183 #define SZ_80M (SZ_1M*80)
184 #define SZ_128M (SZ_1M*128)
185
186 #ifdef __cplusplus
187 extern "C" {
188 #endif
189
190 RK_S32 mpp_log2(RK_U32 v);
191 RK_S32 mpp_log2_16bit(RK_U32 v);
192
mpp_ceil_log2(RK_S32 x)193 static __inline RK_S32 mpp_ceil_log2(RK_S32 x)
194 {
195 return mpp_log2((x - 1) << 1);
196 }
197
mpp_clip(RK_S32 a,RK_S32 amin,RK_S32 amax)198 static __inline RK_S32 mpp_clip(RK_S32 a, RK_S32 amin, RK_S32 amax)
199 {
200 if (a < amin) {
201 return amin;
202 } else if (a > amax) {
203 return amax;
204 } else {
205 return a;
206 }
207 }
208
mpp_is_32bit(void)209 static __inline RK_U32 mpp_is_32bit(void)
210 {
211 return ((sizeof(void *) == SZ_POINTER) ? (1) : (0));
212 }
213
214 RK_S32 axb_div_c(RK_S32 a, RK_S32 b, RK_S32 c);
215
216 #ifdef __cplusplus
217 }
218 #endif
219
220 #endif /* __MPP_COMMON_H__ */
221