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