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