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) \
40 do { \
41 type SWAP_tmp = b; \
42 b = a; \
43 a = SWAP_tmp; \
44 } while (0)
45 #define MPP_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
46 #define MPP_ALIGN(x, a) (((x) + (a)-1) & ~((a)-1))
47 #define MPP_VSWAP(a, b) \
48 do { \
49 (a) ^= (b); \
50 (b) ^= (a); \
51 (a) ^= (b); \
52 } while (0)
53
54 #define MPP_RB16(x) ((((const unsigned char *)(x))[0] << 8) | ((const unsigned char *)(x))[1])
55 #define MPP_WB16(p, d) \
56 do { \
57 ((unsigned char *)(p))[1] = (d); \
58 ((unsigned char *)(p))[0] = (d) >> 8; \
59 } while (0)
60
61 #define MPP_RL16(x) ((((const unsigned char *)(x))[1] << 8) | ((const unsigned char *)(x))[0])
62 #define MPP_WL16(p, d) \
63 do { \
64 ((unsigned char *)(p))[0] = (d); \
65 ((unsigned char *)(p))[1] = (d) >> 8; \
66 } while (0)
67
68 #define MPP_RB32(x) \
69 ((((const unsigned char *)(x))[0] << 24) | (((const unsigned char *)(x))[1] << 16) | \
70 (((const unsigned char *)(x))[2] << 8) | ((const unsigned char *)(x))[3])
71 #define MPP_WB32(p, d) \
72 do { \
73 ((unsigned char *)(p))[3] = (d); \
74 ((unsigned char *)(p))[2] = (d) >> 8; \
75 ((unsigned char *)(p))[1] = (d) >> 16; \
76 ((unsigned char *)(p))[0] = (d) >> 24; \
77 } while (0)
78
79 #define MPP_RL32(x) \
80 ((((const unsigned char *)(x))[3] << 24) | (((const unsigned char *)(x))[2] << 16) | \
81 (((const unsigned char *)(x))[1] << 8) | ((const unsigned char *)(x))[0])
82 #define MPP_WL32(p, d) \
83 do { \
84 ((unsigned char *)(p))[0] = (d); \
85 ((unsigned char *)(p))[1] = (d) >> 8; \
86 ((unsigned char *)(p))[2] = (d) >> 16; \
87 ((unsigned char *)(p))[3] = (d) >> 24; \
88 } while (0)
89
90 #define MPP_RB64(x) \
91 (((RK_U64)((const unsigned char *)(x))[0] << 56) | ((RK_U64)((const unsigned char *)(x))[1] << 48) | \
92 ((RK_U64)((const unsigned char *)(x))[2] << 40) | ((RK_U64)((const unsigned char *)(x))[3] << 32) | \
93 ((RK_U64)((const unsigned char *)(x))[4] << 24) | ((RK_U64)((const unsigned char *)(x))[5] << 16) | \
94 ((RK_U64)((const unsigned char *)(x))[6] << 8) | (RK_U64)((const unsigned char *)(x))[7])
95 #define MPP_WB64(p, d) \
96 do { \
97 ((unsigned char *)(p))[7] = (d); \
98 ((unsigned char *)(p))[6] = (d) >> 8; \
99 ((unsigned char *)(p))[5] = (d) >> 16; \
100 ((unsigned char *)(p))[4] = (d) >> 24; \
101 ((unsigned char *)(p))[3] = (d) >> 32; \
102 ((unsigned char *)(p))[2] = (d) >> 40; \
103 ((unsigned char *)(p))[1] = (d) >> 48; \
104 ((unsigned char *)(p))[0] = (d) >> 56; \
105 } while (0)
106
107 #define MPP_RL64(x) \
108 (((RK_U64)((const unsigned char *)(x))[7] << 56) | ((RK_U64)((const unsigned char *)(x))[6] << 48) | \
109 ((RK_U64)((const unsigned char *)(x))[5] << 40) | ((RK_U64)((const unsigned char *)(x))[4] << 32) | \
110 ((RK_U64)((const unsigned char *)(x))[3] << 24) | ((RK_U64)((const unsigned char *)(x))[2] << 16) | \
111 ((RK_U64)((const unsigned char *)(x))[1] << 8) | (RK_U64)((const unsigned char *)(x))[0])
112 #define MPP_WL64(p, d) \
113 do { \
114 ((unsigned char *)(p))[0] = (d); \
115 ((unsigned char *)(p))[1] = (d) >> 8; \
116 ((unsigned char *)(p))[2] = (d) >> 16; \
117 ((unsigned char *)(p))[3] = (d) >> 24; \
118 ((unsigned char *)(p))[4] = (d) >> 32; \
119 ((unsigned char *)(p))[5] = (d) >> 40; \
120 ((unsigned char *)(p))[6] = (d) >> 48; \
121 ((unsigned char *)(p))[7] = (d) >> 56; \
122 } while (0)
123
124 #define MPP_RB24(x) \
125 ((((const unsigned char *)(x))[0] << 16) | (((const unsigned char *)(x))[1] << 8) | ((const unsigned char *)(x))[2])
126 #define MPP_WB24(p, d) \
127 do { \
128 ((unsigned char *)(p))[2] = (d); \
129 ((unsigned char *)(p))[1] = (d) >> 8; \
130 ((unsigned char *)(p))[0] = (d) >> 16; \
131 } while (0)
132
133 #define MPP_RL24(x) \
134 ((((const unsigned char *)(x))[2] << 16) | (((const unsigned char *)(x))[1] << 8) | ((const unsigned char *)(x))[0])
135
136 #define MPP_WL24(p, d) \
137 do { \
138 ((unsigned char *)(p))[0] = (d); \
139 ((unsigned char *)(p))[1] = (d) >> 8; \
140 ((unsigned char *)(p))[2] = (d) >> 16; \
141 } while (0)
142
143 #include <stdio.h>
144 #if defined(_WIN32) && !defined(__MINGW32CE__)
145 #define fseeko _fseeki64
146
147 #include <direct.h>
148 #include <io.h>
149 #include <sys/stat.h>
150 #define chdir _chdir
151 #define mkdir _mkdir
152 #define access _access
153 #define off_t _off_t
154
155 #define R_OK 4 /* Test for read permission. */
156 #define W_OK 2 /* Test for write permission. */
157 #define X_OK 1 /* Test for execute permission. */
158 #define F_OK 0 /* Test for existence. */
159
160 #elif defined(__MINGW32CE__)
161 #define fseeko fseeko64
162 #else
163 #include <unistd.h>
164 #include <stddef.h>
165 #include <sys/stat.h>
166 #include <sys/types.h>
167 #define mkdir(x) mkdir(x, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
168 #endif
169
170 #define container_of(ptr, type, member) ((type *)((char *)(ptr)-offsetof(type, member)))
171
172 #define RETURN __Return
173 #define FAILED __Failed
174
175 #define ARG_T(t) t
176 #define ARG_N(a, b, c, d, N, ...) N
177 #define ARG_N_HELPER(...) ARG_T(ARG_N(__VA_ARGS__))
178 #define COUNT_ARG(...) ARG_N_HELPER(__VA_ARGS__, 4, 3, 2, 1, 0)
179
180 #define SZ_1K (1024)
181 #define SZ_2K (SZ_1K * 2)
182 #define SZ_4K (SZ_1K * 4)
183 #define SZ_8K (SZ_1K * 8)
184 #define SZ_16K (SZ_1K * 16)
185 #define SZ_32K (SZ_1K * 32)
186 #define SZ_64K (SZ_1K * 64)
187 #define SZ_128K (SZ_1K * 128)
188 #define SZ_256K (SZ_1K * 256)
189 #define SZ_512K (SZ_1K * 512)
190 #define SZ_1M (SZ_1K * SZ_1K)
191 #define SZ_2M (SZ_1M * 2)
192 #define SZ_4M (SZ_1M * 4)
193 #define SZ_8M (SZ_1M * 8)
194 #define SZ_16M (SZ_1M * 16)
195 #define SZ_32M (SZ_1M * 32)
196 #define SZ_64M (SZ_1M * 64)
197 #define SZ_80M (SZ_1M * 80)
198 #define SZ_128M (SZ_1M * 128)
199
200 #define SIZE_INT 4
201 #ifdef __cplusplus
202 extern "C" {
203 #endif
204
205 signed int mpp_log2(unsigned int v);
206 signed int mpp_log2_16bit(unsigned int v);
207
mpp_ceil_log2(signed int x)208 static __inline signed int mpp_ceil_log2(signed int x)
209 {
210 return mpp_log2((x - 1) << 1);
211 }
212
mpp_clip(signed int a,signed int amin,signed int amax)213 static __inline signed int mpp_clip(signed int a, signed int amin, signed int amax)
214 {
215 if (a < amin) {
216 return amin;
217 } else if (a > amax) {
218 return amax;
219 } else {
220 return a;
221 }
222 }
223
mpp_is_32bit(void)224 static __inline unsigned int mpp_is_32bit(void)
225 {
226 return ((sizeof(void *) == SIZE_INT) ? (1) : (0));
227 }
228
229 signed int axb_div_c(signed int a, signed int b, signed int c);
230
231 #ifdef __cplusplus
232 }
233 #endif
234
235 #endif /* __MPP_COMMON_H__ */
236