]> git.sesse.net Git - x264/blob - common/osdep.h
Fix issues relating to input/output files being pipes/FIFOs
[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 #if (defined(SYS_OPENBSD) && !defined(isfinite)) || defined(SYS_SunOS)
43 #define isfinite finite
44 #endif
45 #ifdef _WIN32
46 #define rename(src,dst) (unlink(dst), rename(src,dst)) // POSIX says that rename() removes the destination, but win32 doesn't.
47 #ifndef strtok_r
48 #define strtok_r(str,delim,save) strtok(str,delim)
49 #endif
50 #endif
51
52 #define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
53 #define ALIGNED_16( var ) DECLARE_ALIGNED( var, 16 )
54 #define ALIGNED_8( var )  DECLARE_ALIGNED( var, 8 )
55 #define ALIGNED_4( var )  DECLARE_ALIGNED( var, 4 )
56
57 // current arm compilers only maintain 8-byte stack alignment
58 // and cannot align stack variables to more than 8-bytes
59 #ifdef ARCH_ARM
60 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
61     ALIGNED_8( uint8_t name##_8 [sizeof(type sub1 __VA_ARGS__) + 8] );\
62     type (*name) __VA_ARGS__ = (void*)(name##_8 + ((intptr_t)name##_8 & 8))
63 #else
64 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
65     ALIGNED_16( type name sub1 __VA_ARGS__ )
66 #endif
67
68 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
69 #define UNUSED __attribute__((unused))
70 #define ALWAYS_INLINE __attribute__((always_inline)) inline
71 #define NOINLINE __attribute__((noinline))
72 #define x264_constant_p(x) __builtin_constant_p(x)
73 #else
74 #define UNUSED
75 #define ALWAYS_INLINE inline
76 #define NOINLINE
77 #define x264_constant_p(x) 0
78 #endif
79
80 /* threads */
81 #if defined(SYS_BEOS)
82 #include <kernel/OS.h>
83 #define x264_pthread_t               thread_id
84 static inline int x264_pthread_create( x264_pthread_t *t, void *a, void *(*f)(void *), void *d )
85 {
86      *t = spawn_thread( f, "", 10, d );
87      if( *t < B_NO_ERROR )
88          return -1;
89      resume_thread( *t );
90      return 0;
91 }
92 #define x264_pthread_join(t,s)       { long tmp; \
93                                        wait_for_thread(t,(s)?(long*)(*(s)):&tmp); }
94 #ifndef usleep
95 #define usleep(t)                    snooze(t)
96 #endif
97 #define HAVE_PTHREAD 1
98
99 #elif defined(HAVE_PTHREAD)
100 #include <pthread.h>
101 #define USE_REAL_PTHREAD
102
103 #else
104 #define x264_pthread_t               int
105 #define x264_pthread_create(t,u,f,d) 0
106 #define x264_pthread_join(t,s)
107 #endif //SYS_*
108
109 #ifdef USE_REAL_PTHREAD
110 #define x264_pthread_t               pthread_t
111 #define x264_pthread_create          pthread_create
112 #define x264_pthread_join            pthread_join
113 #define x264_pthread_mutex_t         pthread_mutex_t
114 #define x264_pthread_mutex_init      pthread_mutex_init
115 #define x264_pthread_mutex_destroy   pthread_mutex_destroy
116 #define x264_pthread_mutex_lock      pthread_mutex_lock
117 #define x264_pthread_mutex_unlock    pthread_mutex_unlock
118 #define x264_pthread_cond_t          pthread_cond_t
119 #define x264_pthread_cond_init       pthread_cond_init
120 #define x264_pthread_cond_destroy    pthread_cond_destroy
121 #define x264_pthread_cond_broadcast  pthread_cond_broadcast
122 #define x264_pthread_cond_wait       pthread_cond_wait
123 #define x264_pthread_attr_t          pthread_attr_t
124 #define x264_pthread_attr_init       pthread_attr_init
125 #define x264_pthread_attr_destroy    pthread_attr_destroy
126 #define X264_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
127 #else
128 #define x264_pthread_mutex_t         int
129 #define x264_pthread_mutex_init(m,f) 0
130 #define x264_pthread_mutex_destroy(m)
131 #define x264_pthread_mutex_lock(m)
132 #define x264_pthread_mutex_unlock(m)
133 #define x264_pthread_cond_t          int
134 #define x264_pthread_cond_init(c,f)  0
135 #define x264_pthread_cond_destroy(c)
136 #define x264_pthread_cond_broadcast(c)
137 #define x264_pthread_cond_wait(c,m)
138 #define x264_pthread_attr_t          int
139 #define x264_pthread_attr_init(a)    0
140 #define x264_pthread_attr_destroy(a)
141 #define X264_PTHREAD_MUTEX_INITIALIZER 0
142 #endif
143
144 #define WORD_SIZE sizeof(void*)
145
146 #if !defined(_WIN64) && !defined(__LP64__)
147 #if defined(__INTEL_COMPILER)
148 #define BROKEN_STACK_ALIGNMENT /* define it if stack is not mod16 */
149 #endif
150 #endif
151
152 #ifdef WORDS_BIGENDIAN
153 #define endian_fix(x) (x)
154 #define endian_fix32(x) (x)
155 #define endian_fix16(x) (x)
156 #else
157 #if defined(__GNUC__) && defined(HAVE_MMX)
158 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
159 {
160     asm("bswap %0":"+r"(x));
161     return x;
162 }
163 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
164 {
165     asm("bswap %0":"+r"(x));
166     return x;
167 }
168 #elif defined(__GNUC__) && defined(HAVE_ARMV6)
169 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
170 {
171     asm("rev %0, %0":"+r"(x));
172     return x;
173 }
174 #define endian_fix32 endian_fix
175 #else
176 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
177 {
178     return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
179 }
180 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
181 {
182     if( WORD_SIZE == 8 )
183         return endian_fix32(x>>32) + ((uint64_t)endian_fix32(x)<<32);
184     else
185         return endian_fix32(x);
186 }
187 #endif
188 static ALWAYS_INLINE uint16_t endian_fix16( uint16_t x )
189 {
190     return (x<<8)|(x>>8);
191 }
192 #endif
193
194 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 3)
195 #define x264_clz(x) __builtin_clz(x)
196 #else
197 static int ALWAYS_INLINE x264_clz( uint32_t x )
198 {
199     static uint8_t lut[16] = {4,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
200     int y, z = ((x - 0x10000) >> 27) & 16;
201     x >>= z^16;
202     z += y = ((x - 0x100) >> 28) & 8;
203     x >>= y^8;
204     z += y = ((x - 0x10) >> 29) & 4;
205     x >>= y^4;
206     return z + lut[x];
207 }
208 #endif
209
210 #ifdef USE_REAL_PTHREAD
211 #ifdef SYS_MINGW
212 #define x264_lower_thread_priority(p)\
213 {\
214     x264_pthread_t handle = pthread_self();\
215     struct sched_param sp;\
216     int policy = SCHED_OTHER;\
217     pthread_getschedparam( handle, &policy, &sp );\
218     sp.sched_priority -= p;\
219     pthread_setschedparam( handle, policy, &sp );\
220 }
221 #else
222 #include <unistd.h>
223 #define x264_lower_thread_priority(p) { UNUSED int nice_ret = nice(p); }
224 #endif /* USE_REAL_PTHREAD */
225 #else
226 #define x264_lower_thread_priority(p)
227 #endif
228
229 static inline uint8_t x264_is_regular_file( FILE *filehandle )
230 {
231     struct stat file_stat;
232     if( fstat( fileno( filehandle ), &file_stat ) )
233         return 0;
234     return S_ISREG( file_stat.st_mode );
235 }
236
237 #endif /* X264_OSDEP_H */