Handle case of get_bits(0)
[libav.git] / libavutil / internal.h
CommitLineData
04d7f601
DB
1/*
2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
05020c89
RD
19/**
20 * @file internal.h
21 * common internal api header.
22 */
23
24#ifndef INTERNAL_H
25#define INTERNAL_H
26
27#if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC)
28# define PIC
29#endif
30
31# ifndef ENODATA
32# define ENODATA 61
33# endif
34
aab77159
RD
35#include "bswap.h"
36
05020c89
RD
37#include <stddef.h>
38#ifndef offsetof
39# define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
40#endif
41
05020c89
RD
42#ifdef __MINGW32__
43# ifdef _DEBUG
44# define DEBUG
45# endif
46
47# define snprintf _snprintf
48# define vsnprintf _vsnprintf
49
50# ifdef CONFIG_WINCE
51# define perror(a)
52# endif
53
54/* __MINGW32__ end */
55#elif defined (CONFIG_OS2)
56/* OS/2 EMX */
57
58#include <float.h>
59
60#endif /* !__MINGW32__ && CONFIG_OS2 */
61
62# ifdef USE_FASTMEMCPY
f4bd289a 63# include "libvo/fastmemcpy.h"
05020c89
RD
64# endif
65
66// Use rip-relative addressing if compiling PIC code on x86-64.
67# if defined(__MINGW32__) || defined(__CYGWIN__) || \
68 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
69# if defined(ARCH_X86_64) && defined(PIC)
70# define MANGLE(a) "_" #a"(%%rip)"
71# else
72# define MANGLE(a) "_" #a
73# endif
74# else
75# if defined(ARCH_X86_64) && defined(PIC)
76# define MANGLE(a) #a"(%%rip)"
77# elif defined(CONFIG_DARWIN)
78# define MANGLE(a) "_" #a
79# else
80# define MANGLE(a) #a
81# endif
82# endif
83
84/* debug stuff */
85
86# if !defined(DEBUG) && !defined(NDEBUG)
87# define NDEBUG
88# endif
89# include <assert.h>
90
91/* dprintf macros */
92# ifdef DEBUG
93# define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
94# else
95# define dprintf(fmt,...)
96# endif
97
98# ifdef CONFIG_WINCE
99# define abort()
100# endif
101
102# define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
103
104extern const uint32_t inverse[256];
105
106#if defined(ARCH_X86) || defined(ARCH_X86_64)
107# define FASTDIV(a,b) \
108 ({\
109 int ret,dmy;\
110 asm volatile(\
111 "mull %3"\
112 :"=d"(ret),"=a"(dmy)\
113 :"1"(a),"g"(inverse[b])\
114 );\
115 ret;\
116 })
117#elif defined(CONFIG_FASTDIV)
118# define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
119#else
120# define FASTDIV(a,b) ((a)/(b))
121#endif
122
123/* math */
124extern FF_IMPORT_ATTR const uint8_t ff_sqrt_tab[128];
125
126static inline int ff_sqrt(int a)
127{
128 int ret=0;
129 int s;
130 int ret_sq=0;
131
132 if(a<128) return ff_sqrt_tab[a];
133
134 for(s=15; s>=0; s--){
135 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
136 if(b<=a){
137 ret_sq=b;
138 ret+= 1<<s;
139 }
140 }
141 return ret;
142}
143
144#if defined(ARCH_X86) || defined(ARCH_X86_64)
145#define MASK_ABS(mask, level)\
146 asm volatile(\
147 "cdq \n\t"\
148 "xorl %1, %0 \n\t"\
149 "subl %1, %0 \n\t"\
150 : "+a" (level), "=&d" (mask)\
151 );
152#else
153#define MASK_ABS(mask, level)\
154 mask= level>>31;\
155 level= (level^mask)-mask;
156#endif
157
158#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
159#define COPY3_IF_LT(x,y,a,b,c,d)\
160asm volatile (\
161 "cmpl %0, %3 \n\t"\
162 "cmovl %3, %0 \n\t"\
163 "cmovl %4, %1 \n\t"\
164 "cmovl %5, %2 \n\t"\
165 : "+r" (x), "+r" (a), "+r" (c)\
166 : "r" (y), "r" (b), "r" (d)\
167);
168#else
169#define COPY3_IF_LT(x,y,a,b,c,d)\
170if((y)<(x)){\
171 (x)=(y);\
172 (a)=(b);\
173 (c)=(d);\
174}
175#endif
176
177/* avoid usage of various functions */
178#define malloc please_use_av_malloc
179#define free please_use_av_free
180#define realloc please_use_av_realloc
181#define time time_is_forbidden_due_to_security_issues
182#define rand rand_is_forbidden_due_to_state_trashing
183#define srand srand_is_forbidden_due_to_state_trashing
184#define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
185#define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
186#if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
187#define printf please_use_av_log
188#define fprintf please_use_av_log
189#endif
190
191#define CHECKED_ALLOCZ(p, size)\
192{\
193 p= av_mallocz(size);\
194 if(p==NULL && (size)!=0){\
195 perror("malloc");\
196 goto fail;\
197 }\
198}
199
200#ifndef HAVE_LRINTF
201/* XXX: add ISOC specific test to avoid specific BSD testing. */
202/* better than nothing implementation. */
203/* btw, rintf() is existing on fbsd too -- alex */
204static always_inline long int lrintf(float x)
205{
206#ifdef __MINGW32__
207# ifdef ARCH_X86
208 int32_t i;
209 asm volatile(
210 "fistpl %0\n\t"
211 : "=m" (i) : "t" (x) : "st"
212 );
213 return i;
214# else
215 /* XXX: incorrect, but make it compile */
216 return (int)(x + (x < 0 ? -0.5 : 0.5));
217# endif /* ARCH_X86 */
218#else
219 return (int)(rint(x));
220#endif /* __MINGW32__ */
221}
222#endif /* HAVE_LRINTF */
223
224#endif /* INTERNAL_H */