]> git.sesse.net Git - x264/blob - common/osdep.h
MBAFF: Enable adaptive MBAFF with VSAD decision
[x264] / common / osdep.h
1 /*****************************************************************************
2  * osdep.h: platform-specific code
3  *****************************************************************************
4  * Copyright (C) 2007-2011 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  * This program is also available under a commercial proprietary license.
24  * For more information, contact us at licensing@x264.com.
25  *****************************************************************************/
26
27 #ifndef X264_OSDEP_H
28 #define X264_OSDEP_H
29
30 #define _LARGEFILE_SOURCE 1
31 #define _FILE_OFFSET_BITS 64
32 #include <stdio.h>
33 #include <sys/stat.h>
34
35 #include "config.h"
36
37 #if HAVE_STDINT_H
38 #include <stdint.h>
39 #else
40 #include <inttypes.h>
41 #endif
42
43 #if !HAVE_LOG2F
44 #define log2f(x) (logf(x)/0.693147180559945f)
45 #define log2(x) (log(x)/0.693147180559945)
46 #endif
47
48 #ifdef _WIN32
49 #include <io.h>    // _setmode()
50 #include <fcntl.h> // _O_BINARY
51 #endif
52
53 #ifdef __ICL
54 #define inline __inline
55 #define strcasecmp _stricmp
56 #define strncasecmp _strnicmp
57 #define snprintf _snprintf
58 #define strtok_r strtok_s
59 #define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
60 #endif
61
62 #ifdef __INTEL_COMPILER
63 #include <mathimf.h>
64 #else
65 #include <math.h>
66 #endif
67
68 #if (defined(__GNUC__) || defined(__INTEL_COMPILER)) && (ARCH_X86 || ARCH_X86_64)
69 #define HAVE_X86_INLINE_ASM 1
70 #endif
71
72 #if !defined(isfinite) && (SYS_OPENBSD || SYS_SunOS)
73 #define isfinite finite
74 #endif
75 #ifdef _WIN32
76 #define rename(src,dst) (unlink(dst), rename(src,dst)) // POSIX says that rename() removes the destination, but win32 doesn't.
77 #ifndef strtok_r
78 #define strtok_r(str,delim,save) strtok(str,delim)
79 #endif
80 #endif
81
82 #ifdef __ICL
83 #define DECLARE_ALIGNED( var, n ) __declspec(align(n)) var
84 #else
85 #define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
86 #endif
87 #define ALIGNED_16( var ) DECLARE_ALIGNED( var, 16 )
88 #define ALIGNED_8( var )  DECLARE_ALIGNED( var, 8 )
89 #define ALIGNED_4( var )  DECLARE_ALIGNED( var, 4 )
90
91 // ARM compiliers don't reliably align stack variables
92 // - EABI requires only 8 byte stack alignment to be maintained
93 // - gcc can't align stack variables to more even if the stack were to be correctly aligned outside the function
94 // - armcc can't either, but is nice enough to actually tell you so
95 // - Apple gcc only maintains 4 byte alignment
96 // - llvm can align the stack, but only in svn and (unrelated) it exposes bugs in all released GNU binutils...
97 #if ARCH_ARM && SYS_MACOSX
98 #define ALIGNED_ARRAY_8( type, name, sub1, ... )\
99     uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + 7]; \
100     type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+7) & ~7)
101 #else
102 #define ALIGNED_ARRAY_8( type, name, sub1, ... )\
103     ALIGNED_8( type name sub1 __VA_ARGS__ )
104 #endif
105
106 #if ARCH_ARM
107 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
108     uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + 15];\
109     type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+15) & ~15)
110 #else
111 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
112     ALIGNED_16( type name sub1 __VA_ARGS__ )
113 #endif
114
115 #define UNINIT(x) x=x
116
117 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
118 #define UNUSED __attribute__((unused))
119 #define ALWAYS_INLINE __attribute__((always_inline)) inline
120 #define NOINLINE __attribute__((noinline))
121 #define MAY_ALIAS __attribute__((may_alias))
122 #define x264_constant_p(x) __builtin_constant_p(x)
123 #define x264_nonconstant_p(x) (!__builtin_constant_p(x))
124 #else
125 #ifdef __ICL
126 #define ALWAYS_INLINE __forceinline
127 #define NOINLINE __declspec(noinline)
128 #else
129 #define ALWAYS_INLINE inline
130 #define NOINLINE
131 #endif
132 #define UNUSED
133 #define MAY_ALIAS
134 #define x264_constant_p(x) 0
135 #define x264_nonconstant_p(x) 0
136 #endif
137
138 /* threads */
139 #if HAVE_BEOSTHREAD
140 #include <kernel/OS.h>
141 #define x264_pthread_t               thread_id
142 static inline int x264_pthread_create( x264_pthread_t *t, void *a, void *(*f)(void *), void *d )
143 {
144      *t = spawn_thread( f, "", 10, d );
145      if( *t < B_NO_ERROR )
146          return -1;
147      resume_thread( *t );
148      return 0;
149 }
150 #define x264_pthread_join(t,s)       { long tmp; \
151                                        wait_for_thread(t,(s)?(long*)(*(s)):&tmp); }
152
153 #elif HAVE_POSIXTHREAD
154 #include <pthread.h>
155 #define x264_pthread_t               pthread_t
156 #define x264_pthread_create          pthread_create
157 #define x264_pthread_join            pthread_join
158 #define x264_pthread_mutex_t         pthread_mutex_t
159 #define x264_pthread_mutex_init      pthread_mutex_init
160 #define x264_pthread_mutex_destroy   pthread_mutex_destroy
161 #define x264_pthread_mutex_lock      pthread_mutex_lock
162 #define x264_pthread_mutex_unlock    pthread_mutex_unlock
163 #define x264_pthread_cond_t          pthread_cond_t
164 #define x264_pthread_cond_init       pthread_cond_init
165 #define x264_pthread_cond_destroy    pthread_cond_destroy
166 #define x264_pthread_cond_broadcast  pthread_cond_broadcast
167 #define x264_pthread_cond_wait       pthread_cond_wait
168 #define x264_pthread_attr_t          pthread_attr_t
169 #define x264_pthread_attr_init       pthread_attr_init
170 #define x264_pthread_attr_destroy    pthread_attr_destroy
171 #define x264_pthread_num_processors_np pthread_num_processors_np
172 #define X264_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
173
174 #elif HAVE_WIN32THREAD
175 #include "win32thread.h"
176
177 #else
178 #define x264_pthread_t               int
179 #define x264_pthread_create(t,u,f,d) 0
180 #define x264_pthread_join(t,s)
181 #endif //HAVE_*THREAD
182
183 #if !HAVE_POSIXTHREAD && !HAVE_WIN32THREAD
184 #define x264_pthread_mutex_t         int
185 #define x264_pthread_mutex_init(m,f) 0
186 #define x264_pthread_mutex_destroy(m)
187 #define x264_pthread_mutex_lock(m)
188 #define x264_pthread_mutex_unlock(m)
189 #define x264_pthread_cond_t          int
190 #define x264_pthread_cond_init(c,f)  0
191 #define x264_pthread_cond_destroy(c)
192 #define x264_pthread_cond_broadcast(c)
193 #define x264_pthread_cond_wait(c,m)
194 #define x264_pthread_attr_t          int
195 #define x264_pthread_attr_init(a)    0
196 #define x264_pthread_attr_destroy(a)
197 #define X264_PTHREAD_MUTEX_INITIALIZER 0
198 #endif
199
200 #if HAVE_WIN32THREAD || PTW32_STATIC_LIB
201 int x264_threading_init( void );
202 #else
203 #define x264_threading_init() 0
204 #endif
205
206 #define WORD_SIZE sizeof(void*)
207
208 #define asm __asm__
209
210 #if WORDS_BIGENDIAN
211 #define endian_fix(x) (x)
212 #define endian_fix64(x) (x)
213 #define endian_fix32(x) (x)
214 #define endian_fix16(x) (x)
215 #else
216 #if HAVE_X86_INLINE_ASM && HAVE_MMX
217 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
218 {
219     asm("bswap %0":"+r"(x));
220     return x;
221 }
222 #elif defined(__GNUC__) && HAVE_ARMV6
223 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
224 {
225     asm("rev %0, %0":"+r"(x));
226     return x;
227 }
228 #else
229 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
230 {
231     return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
232 }
233 #endif
234 #if HAVE_X86_INLINE_ASM && ARCH_X86_64
235 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
236 {
237     asm("bswap %0":"+r"(x));
238     return x;
239 }
240 #else
241 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
242 {
243     return endian_fix32(x>>32) + ((uint64_t)endian_fix32(x)<<32);
244 }
245 #endif
246 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
247 {
248     return WORD_SIZE == 8 ? endian_fix64(x) : endian_fix32(x);
249 }
250 static ALWAYS_INLINE uint16_t endian_fix16( uint16_t x )
251 {
252     return (x<<8)|(x>>8);
253 }
254 #endif
255
256 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 3)
257 #define x264_clz(x) __builtin_clz(x)
258 #define x264_ctz(x) __builtin_ctz(x)
259 #else
260 static int ALWAYS_INLINE x264_clz( uint32_t x )
261 {
262     static uint8_t lut[16] = {4,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
263     int y, z = (((x >> 16) - 1) >> 27) & 16;
264     x >>= z^16;
265     z += y = ((x - 0x100) >> 28) & 8;
266     x >>= y^8;
267     z += y = ((x - 0x10) >> 29) & 4;
268     x >>= y^4;
269     return z + lut[x];
270 }
271
272 static int ALWAYS_INLINE x264_ctz( uint32_t x )
273 {
274     static uint8_t lut[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
275     int y, z = (((x & 0xffff) - 1) >> 27) & 16;
276     x >>= z;
277     z += y = (((x & 0xff) - 1) >> 28) & 8;
278     x >>= y;
279     z += y = (((x & 0xf) - 1) >> 29) & 4;
280     x >>= y;
281     return z + lut[x&0xf];
282 }
283 #endif
284
285 #if HAVE_X86_INLINE_ASM && HAVE_MMX
286 /* Don't use __builtin_prefetch; even as recent as 4.3.4, GCC seems incapable of
287  * using complex address modes properly unless we use inline asm. */
288 static ALWAYS_INLINE void x264_prefetch( void *p )
289 {
290     asm volatile( "prefetcht0 %0"::"m"(*(uint8_t*)p) );
291 }
292 /* We require that prefetch not fault on invalid reads, so we only enable it on
293  * known architectures. */
294 #elif defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 1) &&\
295       (ARCH_X86 || ARCH_X86_64 || ARCH_ARM || ARCH_PPC)
296 #define x264_prefetch(x) __builtin_prefetch(x)
297 #else
298 #define x264_prefetch(x)
299 #endif
300
301 #if HAVE_POSIXTHREAD
302 #if SYS_WINDOWS
303 #define x264_lower_thread_priority(p)\
304 {\
305     x264_pthread_t handle = pthread_self();\
306     struct sched_param sp;\
307     int policy = SCHED_OTHER;\
308     pthread_getschedparam( handle, &policy, &sp );\
309     sp.sched_priority -= p;\
310     pthread_setschedparam( handle, policy, &sp );\
311 }
312 #else
313 #include <unistd.h>
314 #define x264_lower_thread_priority(p) { UNUSED int nice_ret = nice(p); }
315 #endif /* SYS_WINDOWS */
316 #elif HAVE_WIN32THREAD
317 #define x264_lower_thread_priority(p) SetThreadPriority( GetCurrentThread(), X264_MAX( -2, -p ) )
318 #else
319 #define x264_lower_thread_priority(p)
320 #endif
321
322 static inline uint8_t x264_is_regular_file( FILE *filehandle )
323 {
324     struct stat file_stat;
325     if( fstat( fileno( filehandle ), &file_stat ) )
326         return -1;
327     return S_ISREG( file_stat.st_mode );
328 }
329
330 static inline uint8_t x264_is_regular_file_path( const char *filename )
331 {
332     struct stat file_stat;
333     if( stat( filename, &file_stat ) )
334         return -1;
335     return S_ISREG( file_stat.st_mode );
336 }
337
338 #endif /* X264_OSDEP_H */