]> git.sesse.net Git - ffmpeg/blob - libavcodec/mathops.h
Reduce the scope of some variables
[ffmpeg] / libavcodec / mathops.h
1 /*
2  * simple math operations
3  * Copyright (c) 2001, 2002 Fabrice Bellard
4  * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at> et al
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef AVCODEC_MATHOPS_H
23 #define AVCODEC_MATHOPS_H
24
25 #include <stdint.h>
26
27 #include "libavutil/common.h"
28 #include "config.h"
29
30 #define MAX_NEG_CROP 1024
31
32 extern const uint32_t ff_inverse[257];
33 extern const uint8_t  ff_reverse[256];
34 extern const uint8_t ff_sqrt_tab[256];
35 extern const uint8_t ff_crop_tab[256 + 2 * MAX_NEG_CROP];
36 extern const uint8_t ff_zigzag_direct[64];
37 extern const uint8_t ff_zigzag_scan[16];
38
39 #if   ARCH_ARM
40 #   include "arm/mathops.h"
41 #elif ARCH_AVR32
42 #   include "avr32/mathops.h"
43 #elif ARCH_BFIN
44 #   include "bfin/mathops.h"
45 #elif ARCH_MIPS
46 #   include "mips/mathops.h"
47 #elif ARCH_PPC
48 #   include "ppc/mathops.h"
49 #elif ARCH_X86
50 #   include "x86/mathops.h"
51 #endif
52
53 /* generic implementation */
54
55 #ifndef MUL64
56 #   define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
57 #endif
58
59 #ifndef MULL
60 #   define MULL(a,b,s) (MUL64(a, b) >> (s))
61 #endif
62
63 #ifndef MULH
64 static av_always_inline int MULH(int a, int b){
65     return MUL64(a, b) >> 32;
66 }
67 #endif
68
69 #ifndef UMULH
70 static av_always_inline unsigned UMULH(unsigned a, unsigned b){
71     return ((uint64_t)(a) * (uint64_t)(b))>>32;
72 }
73 #endif
74
75 #ifndef MAC64
76 #   define MAC64(d, a, b) ((d) += MUL64(a, b))
77 #endif
78
79 #ifndef MLS64
80 #   define MLS64(d, a, b) ((d) -= MUL64(a, b))
81 #endif
82
83 /* signed 16x16 -> 32 multiply add accumulate */
84 #ifndef MAC16
85 #   define MAC16(rt, ra, rb) rt += (ra) * (rb)
86 #endif
87
88 /* signed 16x16 -> 32 multiply */
89 #ifndef MUL16
90 #   define MUL16(ra, rb) ((ra) * (rb))
91 #endif
92
93 #ifndef MLS16
94 #   define MLS16(rt, ra, rb) ((rt) -= (ra) * (rb))
95 #endif
96
97 /* median of 3 */
98 #ifndef mid_pred
99 #define mid_pred mid_pred
100 static inline av_const int mid_pred(int a, int b, int c)
101 {
102 #if 0
103     int t= (a-b)&((a-b)>>31);
104     a-=t;
105     b+=t;
106     b-= (b-c)&((b-c)>>31);
107     b+= (a-b)&((a-b)>>31);
108
109     return b;
110 #else
111     if(a>b){
112         if(c>b){
113             if(c>a) b=a;
114             else    b=c;
115         }
116     }else{
117         if(b>c){
118             if(c>a) b=c;
119             else    b=a;
120         }
121     }
122     return b;
123 #endif
124 }
125 #endif
126
127 #ifndef sign_extend
128 static inline av_const int sign_extend(int val, unsigned bits)
129 {
130     unsigned shift = 8 * sizeof(int) - bits;
131     union { unsigned u; int s; } v = { (unsigned) val << shift };
132     return v.s >> shift;
133 }
134 #endif
135
136 #ifndef zero_extend
137 static inline av_const unsigned zero_extend(unsigned val, unsigned bits)
138 {
139     return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);
140 }
141 #endif
142
143 #ifndef COPY3_IF_LT
144 #define COPY3_IF_LT(x, y, a, b, c, d)\
145 if ((y) < (x)) {\
146     (x) = (y);\
147     (a) = (b);\
148     (c) = (d);\
149 }
150 #endif
151
152 #ifndef MASK_ABS
153 #define MASK_ABS(mask, level) do {              \
154         mask  = level >> 31;                    \
155         level = (level ^ mask) - mask;          \
156     } while (0)
157 #endif
158
159 #ifndef NEG_SSR32
160 #   define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
161 #endif
162
163 #ifndef NEG_USR32
164 #   define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
165 #endif
166
167 #if HAVE_BIGENDIAN
168 # ifndef PACK_2U8
169 #   define PACK_2U8(a,b)     (((a) <<  8) | (b))
170 # endif
171 # ifndef PACK_4U8
172 #   define PACK_4U8(a,b,c,d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
173 # endif
174 # ifndef PACK_2U16
175 #   define PACK_2U16(a,b)    (((a) << 16) | (b))
176 # endif
177 #else
178 # ifndef PACK_2U8
179 #   define PACK_2U8(a,b)     (((b) <<  8) | (a))
180 # endif
181 # ifndef PACK_4U2
182 #   define PACK_4U8(a,b,c,d) (((d) << 24) | ((c) << 16) | ((b) << 8) | (a))
183 # endif
184 # ifndef PACK_2U16
185 #   define PACK_2U16(a,b)    (((b) << 16) | (a))
186 # endif
187 #endif
188
189 #ifndef PACK_2S8
190 #   define PACK_2S8(a,b)     PACK_2U8((a)&255, (b)&255)
191 #endif
192 #ifndef PACK_4S8
193 #   define PACK_4S8(a,b,c,d) PACK_4U8((a)&255, (b)&255, (c)&255, (d)&255)
194 #endif
195 #ifndef PACK_2S16
196 #   define PACK_2S16(a,b)    PACK_2U16((a)&0xffff, (b)&0xffff)
197 #endif
198
199 #ifndef FASTDIV
200 #   define FASTDIV(a,b) ((uint32_t)((((uint64_t)a) * ff_inverse[b]) >> 32))
201 #endif /* FASTDIV */
202
203 static inline av_const unsigned int ff_sqrt(unsigned int a)
204 {
205     unsigned int b;
206
207     if (a < 255) return (ff_sqrt_tab[a + 1] - 1) >> 4;
208     else if (a < (1 << 12)) b = ff_sqrt_tab[a >> 4] >> 2;
209 #if !CONFIG_SMALL
210     else if (a < (1 << 14)) b = ff_sqrt_tab[a >> 6] >> 1;
211     else if (a < (1 << 16)) b = ff_sqrt_tab[a >> 8]   ;
212 #endif
213     else {
214         int s = av_log2_16bit(a >> 16) >> 1;
215         unsigned int c = a >> (s + 2);
216         b = ff_sqrt_tab[c >> (s + 8)];
217         b = FASTDIV(c,b) + (b << s);
218     }
219
220     return b - (a < b * b);
221 }
222
223 static inline int8_t ff_u8_to_s8(uint8_t a)
224 {
225     union {
226         uint8_t u8;
227         int8_t  s8;
228     } b;
229     b.u8 = a;
230     return b.s8;
231 }
232
233 #endif /* AVCODEC_MATHOPS_H */