]> git.sesse.net Git - x264/blob - common/osdep.h
915ec05efeed6d767589f1491f1159ad4acd0d56
[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 #ifdef _WIN32
38 #include <io.h>    // _setmode()
39 #include <fcntl.h> // _O_BINARY
40 #endif
41
42 #ifdef _MSC_VER
43 #define inline __inline
44 #define strcasecmp stricmp
45 #define strncasecmp strnicmp
46 #define snprintf _snprintf
47 #define fseek _fseeki64
48 #define ftell _ftelli64
49 #define isfinite _finite
50 #define strtok_r strtok_s
51 #define _CRT_SECURE_NO_DEPRECATE
52 #define X264_VERSION "" // no configure script for msvc
53 #endif
54
55 #if (defined(SYS_OPENBSD) && !defined(isfinite)) || defined(SYS_SunOS)
56 #define isfinite finite
57 #endif
58 #if defined(_MSC_VER) || defined(SYS_SunOS) || defined(SYS_MACOSX)
59 #define sqrtf sqrt
60 #endif
61 #ifdef _WIN32
62 #define rename(src,dst) (unlink(dst), rename(src,dst)) // POSIX says that rename() removes the destination, but win32 doesn't.
63 #ifndef strtok_r
64 #define strtok_r(str,delim,save) strtok(str,delim)
65 #endif
66 #endif
67
68 #ifdef _MSC_VER
69 #define DECLARE_ALIGNED( var, n ) __declspec(align(n)) var
70 #else
71 #define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
72 #endif
73 #define DECLARE_ALIGNED_16( var ) DECLARE_ALIGNED( var, 16 )
74 #define DECLARE_ALIGNED_8( var )  DECLARE_ALIGNED( var, 8 )
75 #define DECLARE_ALIGNED_4( var )  DECLARE_ALIGNED( var, 4 )
76
77 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
78 #define UNUSED __attribute__((unused))
79 #define ALWAYS_INLINE __attribute__((always_inline)) inline
80 #define NOINLINE __attribute__((noinline))
81 #define x264_constant_p(x) __builtin_constant_p(x)
82 #else
83 #define UNUSED
84 #define ALWAYS_INLINE inline
85 #define NOINLINE
86 #define x264_constant_p(x) 0
87 #endif
88
89 /* threads */
90 #if defined(SYS_BEOS)
91 #include <kernel/OS.h>
92 #define x264_pthread_t               thread_id
93 #define x264_pthread_create(t,u,f,d) { *(t)=spawn_thread(f,"",10,d); \
94                                        resume_thread(*(t)); }
95 #define x264_pthread_join(t,s)       { long tmp; \
96                                        wait_for_thread(t,(s)?(long*)(s):&tmp); }
97 #ifndef usleep
98 #define usleep(t)                    snooze(t)
99 #endif
100 #define HAVE_PTHREAD 1
101
102 #elif defined(HAVE_PTHREAD)
103 #include <pthread.h>
104 #define USE_REAL_PTHREAD
105
106 #else
107 #define x264_pthread_t               int
108 #define x264_pthread_create(t,u,f,d)
109 #define x264_pthread_join(t,s)
110 #endif //SYS_*
111
112 #ifdef USE_REAL_PTHREAD
113 #define x264_pthread_t               pthread_t
114 #define x264_pthread_create          pthread_create
115 #define x264_pthread_join            pthread_join
116 #define x264_pthread_mutex_t         pthread_mutex_t
117 #define x264_pthread_mutex_init      pthread_mutex_init
118 #define x264_pthread_mutex_destroy   pthread_mutex_destroy
119 #define x264_pthread_mutex_lock      pthread_mutex_lock
120 #define x264_pthread_mutex_unlock    pthread_mutex_unlock
121 #define x264_pthread_cond_t          pthread_cond_t
122 #define x264_pthread_cond_init       pthread_cond_init
123 #define x264_pthread_cond_destroy    pthread_cond_destroy
124 #define x264_pthread_cond_broadcast  pthread_cond_broadcast
125 #define x264_pthread_cond_wait       pthread_cond_wait
126 #else
127 #define x264_pthread_mutex_t         int
128 #define x264_pthread_mutex_init(m,f)
129 #define x264_pthread_mutex_destroy(m)
130 #define x264_pthread_mutex_lock(m)
131 #define x264_pthread_mutex_unlock(m)
132 #define x264_pthread_cond_t          int
133 #define x264_pthread_cond_init(c,f)
134 #define x264_pthread_cond_destroy(c)
135 #define x264_pthread_cond_broadcast(c)
136 #define x264_pthread_cond_wait(c,m)
137 #endif
138
139 #define WORD_SIZE sizeof(void*)
140
141 #if !defined(_WIN64) && !defined(__LP64__)
142 #if defined(_MSC_VER) || defined(__INTEL_COMPILER)
143 #define BROKEN_STACK_ALIGNMENT /* define it if stack is not mod16 */
144 #endif
145 #endif
146
147 #ifdef WORDS_BIGENDIAN
148 #define endian_fix(x) (x)
149 #define endian_fix32(x) (x)
150 #elif defined(__GNUC__) && defined(HAVE_MMX)
151 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
152 {
153     asm("bswap %0":"+r"(x));
154     return x;
155 }
156 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
157 {
158     asm("bswap %0":"+r"(x));
159     return x;
160 }
161 #else
162 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
163 {
164     return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
165 }
166 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
167 {
168     if( WORD_SIZE == 8 )
169         return endian_fix32(x>>32) + ((uint64_t)endian_fix32(x)<<32);
170     else
171         return endian_fix32(x);
172 }
173 #endif
174
175 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 3)
176 #define x264_clz(x) __builtin_clz(x)
177 #else
178 static int ALWAYS_INLINE x264_clz( uint32_t x )
179 {
180     static uint8_t lut[16] = {4,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
181     int y, z = ((x - 0x10000) >> 27) & 16;
182     x >>= z^16;
183     z += y = ((x - 0x100) >> 28) & 8;
184     x >>= y^8;
185     z += y = ((x - 0x10) >> 29) & 4;
186     x >>= y^4;
187     return z + lut[x];
188 }
189 #endif
190
191 #endif /* X264_OSDEP_H */