]> git.sesse.net Git - x264/blob - common/osdep.h
998880398d8d960fdae14cd3825167d2dd09f71d
[x264] / common / osdep.h
1 /*****************************************************************************
2  * osdep.h: h264 encoder
3  *****************************************************************************
4  * Copyright (C) 2007-2008 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #ifndef X264_OSDEP_H
25 #define X264_OSDEP_H
26
27 #define _LARGEFILE_SOURCE 1
28 #define _FILE_OFFSET_BITS 64
29 #include <stdio.h>
30
31 #ifdef HAVE_STDINT_H
32 #include <stdint.h>
33 #else
34 #include <inttypes.h>
35 #endif
36
37 #ifndef HAVE_LOG2F
38 #define log2f(x) (logf((x))/0.693147180559945f)
39 #endif
40
41 #ifdef _WIN32
42 #include <io.h>    // _setmode()
43 #include <fcntl.h> // _O_BINARY
44 #endif
45
46 #if (defined(SYS_OPENBSD) && !defined(isfinite)) || defined(SYS_SunOS)
47 #define isfinite finite
48 #endif
49 #ifdef _WIN32
50 #define rename(src,dst) (unlink(dst), rename(src,dst)) // POSIX says that rename() removes the destination, but win32 doesn't.
51 #ifndef strtok_r
52 #define strtok_r(str,delim,save) strtok(str,delim)
53 #endif
54 #endif
55
56 #define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
57 #define ALIGNED_16( var ) DECLARE_ALIGNED( var, 16 )
58 #define ALIGNED_8( var )  DECLARE_ALIGNED( var, 8 )
59 #define ALIGNED_4( var )  DECLARE_ALIGNED( var, 4 )
60
61 // ARM compiliers don't reliably align stack variables
62 // - EABI requires only 8 byte stack alignment to be maintained
63 // - gcc can't align stack variables to more even if the stack were to be correctly aligned outside the function
64 // - armcc can't either, but is nice enough to actually tell you so
65 // - Apple gcc only maintains 4 byte alignment
66 // - llvm can align the stack, but only in svn and (unrelated) it exposes bugs in all released GNU binutils...
67 #if defined(ARCH_ARM) && defined(SYS_MACOSX)
68 #define ALIGNED_ARRAY_8( type, name, sub1, ... )\
69     uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + 7]; \
70     type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+7) & ~7)
71 #else
72 #define ALIGNED_ARRAY_8( type, name, sub1, ... )\
73     ALIGNED_8( type name sub1 __VA_ARGS__ )
74 #endif
75
76 #ifdef ARCH_ARM
77 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
78     uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + 15];\
79     type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+15) & ~15)
80 #else
81 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
82     ALIGNED_16( type name sub1 __VA_ARGS__ )
83 #endif
84
85 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
86 #define UNUSED __attribute__((unused))
87 #define ALWAYS_INLINE __attribute__((always_inline)) inline
88 #define NOINLINE __attribute__((noinline))
89 #define MAY_ALIAS __attribute__((may_alias))
90 #define x264_constant_p(x) __builtin_constant_p(x)
91 #else
92 #define UNUSED
93 #define ALWAYS_INLINE inline
94 #define NOINLINE
95 #define MAY_ALIAS
96 #define x264_constant_p(x) 0
97 #endif
98
99 /* threads */
100 #if defined(SYS_BEOS)
101 #include <kernel/OS.h>
102 #define x264_pthread_t               thread_id
103 static inline int x264_pthread_create( x264_pthread_t *t, void *a, void *(*f)(void *), void *d )
104 {
105      *t = spawn_thread( f, "", 10, d );
106      if( *t < B_NO_ERROR )
107          return -1;
108      resume_thread( *t );
109      return 0;
110 }
111 #define x264_pthread_join(t,s)       { long tmp; \
112                                        wait_for_thread(t,(s)?(long*)(*(s)):&tmp); }
113 #ifndef usleep
114 #define usleep(t)                    snooze(t)
115 #endif
116 #define HAVE_PTHREAD 1
117
118 #elif defined(HAVE_PTHREAD)
119 #include <pthread.h>
120 #define USE_REAL_PTHREAD
121
122 #else
123 #define x264_pthread_t               int
124 #define x264_pthread_create(t,u,f,d) 0
125 #define x264_pthread_join(t,s)
126 #endif //SYS_*
127
128 #ifdef USE_REAL_PTHREAD
129 #define x264_pthread_t               pthread_t
130 #define x264_pthread_create          pthread_create
131 #define x264_pthread_join            pthread_join
132 #define x264_pthread_mutex_t         pthread_mutex_t
133 #define x264_pthread_mutex_init      pthread_mutex_init
134 #define x264_pthread_mutex_destroy   pthread_mutex_destroy
135 #define x264_pthread_mutex_lock      pthread_mutex_lock
136 #define x264_pthread_mutex_unlock    pthread_mutex_unlock
137 #define x264_pthread_cond_t          pthread_cond_t
138 #define x264_pthread_cond_init       pthread_cond_init
139 #define x264_pthread_cond_destroy    pthread_cond_destroy
140 #define x264_pthread_cond_broadcast  pthread_cond_broadcast
141 #define x264_pthread_cond_wait       pthread_cond_wait
142 #define x264_pthread_attr_t          pthread_attr_t
143 #define x264_pthread_attr_init       pthread_attr_init
144 #define x264_pthread_attr_destroy    pthread_attr_destroy
145 #define X264_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
146 #else
147 #define x264_pthread_mutex_t         int
148 #define x264_pthread_mutex_init(m,f) 0
149 #define x264_pthread_mutex_destroy(m)
150 #define x264_pthread_mutex_lock(m)
151 #define x264_pthread_mutex_unlock(m)
152 #define x264_pthread_cond_t          int
153 #define x264_pthread_cond_init(c,f)  0
154 #define x264_pthread_cond_destroy(c)
155 #define x264_pthread_cond_broadcast(c)
156 #define x264_pthread_cond_wait(c,m)
157 #define x264_pthread_attr_t          int
158 #define x264_pthread_attr_init(a)    0
159 #define x264_pthread_attr_destroy(a)
160 #define X264_PTHREAD_MUTEX_INITIALIZER 0
161 #endif
162
163 #define WORD_SIZE sizeof(void*)
164
165 #if !defined(_WIN64) && !defined(__LP64__)
166 #if defined(__INTEL_COMPILER)
167 #define BROKEN_STACK_ALIGNMENT /* define it if stack is not mod16 */
168 #endif
169 #endif
170
171 #ifdef WORDS_BIGENDIAN
172 #define endian_fix(x) (x)
173 #define endian_fix64(x) (x)
174 #define endian_fix32(x) (x)
175 #define endian_fix16(x) (x)
176 #else
177 #if defined(__GNUC__) && defined(HAVE_MMX)
178 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
179 {
180     asm("bswap %0":"+r"(x));
181     return x;
182 }
183 #elif defined(__GNUC__) && defined(HAVE_ARMV6)
184 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
185 {
186     asm("rev %0, %0":"+r"(x));
187     return x;
188 }
189 #else
190 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
191 {
192     return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
193 }
194 #endif
195 #if defined(__GNUC__) && defined(ARCH_X86_64)
196 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
197 {
198     asm("bswap %0":"+r"(x));
199     return x;
200 }
201 #else
202 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
203 {
204     return endian_fix32(x>>32) + ((uint64_t)endian_fix32(x)<<32);
205 }
206 #endif
207 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
208 {
209     return WORD_SIZE == 8 ? endian_fix64(x) : endian_fix32(x);
210 }
211 static ALWAYS_INLINE uint16_t endian_fix16( uint16_t x )
212 {
213     return (x<<8)|(x>>8);
214 }
215 #endif
216
217 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 3)
218 #define x264_clz(x) __builtin_clz(x)
219 #else
220 static int ALWAYS_INLINE x264_clz( uint32_t x )
221 {
222     static uint8_t lut[16] = {4,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
223     int y, z = (((x >> 16) - 1) >> 27) & 16;
224     x >>= z^16;
225     z += y = ((x - 0x100) >> 28) & 8;
226     x >>= y^8;
227     z += y = ((x - 0x10) >> 29) & 4;
228     x >>= y^4;
229     return z + lut[x];
230 }
231 #endif
232
233 #ifdef USE_REAL_PTHREAD
234 #ifdef SYS_MINGW
235 #define x264_lower_thread_priority(p)\
236 {\
237     x264_pthread_t handle = pthread_self();\
238     struct sched_param sp;\
239     int policy = SCHED_OTHER;\
240     pthread_getschedparam( handle, &policy, &sp );\
241     sp.sched_priority -= p;\
242     pthread_setschedparam( handle, policy, &sp );\
243 }
244 #else
245 #include <unistd.h>
246 #define x264_lower_thread_priority(p) { UNUSED int nice_ret = nice(p); }
247 #endif /* USE_REAL_PTHREAD */
248 #else
249 #define x264_lower_thread_priority(p)
250 #endif
251
252 static inline uint8_t x264_is_regular_file( FILE *filehandle )
253 {
254     struct stat file_stat;
255     if( fstat( fileno( filehandle ), &file_stat ) )
256         return 0;
257     return S_ISREG( file_stat.st_mode );
258 }
259
260 #endif /* X264_OSDEP_H */