• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
131 #define container_of(ptr, type, member) \
132     ((type *)((char *)(ptr) - offsetof(type, member)))
133 
134 #define __RETURN                __Return
135 #define __FAILED                __failed
136 
137 #define ARG_T(t)                t
138 #define ARG_N(a,b,c,d,N,...)    N
139 #define ARG_N_HELPER(...)       ARG_T(ARG_N(__VA_ARGS__))
140 #define COUNT_ARG(...)          ARG_N_HELPER(__VA_ARGS__,4,3,2,1,0)
141 
142 #define SZ_1K                   (1024)
143 #define SZ_2K                   (SZ_1K*2)
144 #define SZ_4K                   (SZ_1K*4)
145 #define SZ_8K                   (SZ_1K*8)
146 #define SZ_16K                  (SZ_1K*16)
147 #define SZ_32K                  (SZ_1K*32)
148 #define SZ_64K                  (SZ_1K*64)
149 #define SZ_128K                 (SZ_1K*128)
150 #define SZ_256K                 (SZ_1K*256)
151 #define SZ_512K                 (SZ_1K*512)
152 #define SZ_1M                   (SZ_1K*SZ_1K)
153 #define SZ_2M                   (SZ_1M*2)
154 #define SZ_4M                   (SZ_1M*4)
155 #define SZ_8M                   (SZ_1M*8)
156 #define SZ_16M                  (SZ_1M*16)
157 #define SZ_32M                  (SZ_1M*32)
158 #define SZ_64M                  (SZ_1M*64)
159 #define SZ_80M                  (SZ_1M*80)
160 #define SZ_128M                 (SZ_1M*128)
161 
162 #ifdef __cplusplus
163 extern "C" {
164 #endif
165 
166 RK_S32 mpp_log2(RK_U32 v);
167 RK_S32 mpp_log2_16bit(RK_U32 v);
168 
mpp_ceil_log2(RK_S32 x)169 static __inline RK_S32 mpp_ceil_log2(RK_S32 x)
170 {
171     return mpp_log2((x - 1) << 1);
172 }
173 
mpp_clip(RK_S32 a,RK_S32 amin,RK_S32 amax)174 static __inline RK_S32 mpp_clip(RK_S32 a, RK_S32 amin, RK_S32 amax)
175 {
176     if      (a < amin) return amin;
177     else if (a > amax) return amax;
178     else               return a;
179 }
180 
mpp_is_32bit()181 static __inline RK_U32 mpp_is_32bit()
182 {
183     return ((sizeof(void *) == 4) ? (1) : (0));
184 }
185 
186 RK_S32 axb_div_c(RK_S32 a, RK_S32 b, RK_S32 c);
187 RK_U32 mpp_align_16(RK_U32 val);
188 RK_U32 mpp_align_64(RK_U32 val);
189 
190 #ifdef __cplusplus
191 }
192 #endif
193 
194 #endif /*__MPP_COMMON_H__*/
195 
196