]> git.sesse.net Git - vlc/blob - modules/codec/wmafixed/wmafixed.h
Use var_InheritString for --decklink-video-connection.
[vlc] / modules / codec / wmafixed / wmafixed.h
1 /****************************************************************************
2  *             __________               __   ___.
3  *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
4  *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
5  *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
6  *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
7  *                     \/            \/     \/    \/            \/
8  *
9  * Copyright (C) 2007 Michael Giacomelli
10  *
11  * All files in this archive are subject to the GNU General Public License.
12  * See the file COPYING in the source tree root for full license agreement.
13  *
14  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
15  * KIND, either express or implied.
16  *
17  ****************************************************************************/
18
19 /*  fixed precision code.  We use a combination of Sign 15.16 and Sign.31
20     precision here.
21
22     The WMA decoder does not always follow this convention, and occasionally
23     renormalizes values to other formats in order to maximize precision.
24     However, only the two precisions above are provided in this file.
25
26 */
27
28 #include <inttypes.h>
29
30 #define PRECISION       16
31 #define PRECISION64     16
32
33 #define fixtof64(x)       (float)((float)(x) / (float)(1 << PRECISION64))        //does not work on int64_t!
34 #define ftofix32(x)       ((int32_t)((x) * (float)(1 << PRECISION) + ((x) < 0 ? -0.5 : 0.5)))
35 #define itofix64(x)       (IntTo64(x))
36 #define itofix32(x)       ((x) << PRECISION)
37 #define fixtoi32(x)       ((x) >> PRECISION)
38 #define fixtoi64(x)       (IntFrom64(x))
39
40 /*fixed functions*/
41
42 int64_t IntTo64(int x);
43 int IntFrom64(int64_t x);
44 int32_t Fixed32From64(int64_t x);
45 int64_t Fixed32To64(int32_t x);
46 int64_t fixmul64byfixed(int64_t x, int32_t y);
47 int32_t fixdiv32(int32_t x, int32_t y);
48 int64_t fixdiv64(int64_t x, int64_t y);
49 int32_t fixsqrt32(int32_t x);
50 long fsincos(unsigned long phase, int32_t *cos);
51
52 #ifdef __arm__
53
54 /*Sign-15.16 format */
55
56 #define fixmul32(x, y)  \
57     ({ int32_t __hi;  \
58        uint32_t __lo;  \
59        int32_t __result;  \
60        asm ("smull   %0, %1, %3, %4\n\t"  \
61             "movs    %0, %0, lsr %5\n\t"  \
62             "adc    %2, %0, %1, lsl %6"  \
63             : "=&r" (__lo), "=&r" (__hi), "=r" (__result)  \
64             : "%r" (x), "r" (y),  \
65               "M" (PRECISION), "M" (32 - PRECISION)  \
66             : "cc");  \
67        __result;  \
68     })
69
70 #define fixmul32b(x, y)  \
71     ({ int32_t __hi;  \
72        uint32_t __lo;  \
73        int32_t __result;  \
74        asm ("smull   %0, %1, %3, %4\n\t"  \
75             "movs    %2, %1, lsl #1"  \
76             : "=&r" (__lo), "=&r" (__hi), "=r" (__result)  \
77             : "%r" (x), "r" (y)  \
78             : "cc");  \
79        __result;  \
80     })
81
82 #elif defined(CPU_COLDFIRE)
83
84 static inline int32_t fixmul32(int32_t x, int32_t y)
85 {
86 #if PRECISION != 16
87 #warning Coldfire fixmul32() only works for PRECISION == 16
88 #endif
89     int32_t t1;
90     asm (
91         "mac.l   %[x], %[y], %%acc0  \n" /* multiply */
92         "mulu.l  %[y], %[x]      \n"     /* get lower half, avoid emac stall */
93         "movclr.l %%acc0, %[t1]  \n"     /* get higher half */
94         "lsr.l   #1, %[t1]       \n"
95         "move.w  %[t1], %[x]     \n"
96         "swap    %[x]            \n"
97         : [t1] "=&d" (t1), [x] "+d" (x)
98         : [y] "d"  (y)
99     );
100     return x;
101 }
102
103 static inline int32_t fixmul32b(int32_t x, int32_t y)
104 {
105     asm (
106         "mac.l   %[x], %[y], %%acc0  \n" /* multiply */
107         "movclr.l %%acc0, %[x]  \n"     /* get higher half */
108         : [x] "+d" (x)
109         : [y] "d"  (y)
110     );
111     return x;
112 }
113
114 #else
115
116 static inline int32_t fixmul32(int32_t x, int32_t y)
117 {
118     int64_t temp;
119     temp = x;
120     temp *= y;
121
122     temp >>= PRECISION;
123
124     return (int32_t)temp;
125 }
126
127 static inline int32_t fixmul32b(int32_t x, int32_t y)
128 {
129     int64_t temp;
130
131     temp = x;
132     temp *= y;
133
134     temp >>= 31;        //16+31-16 = 31 bits
135
136     return (int32_t)temp;
137 }
138
139 #endif
140
141 #ifdef __arm__
142 static inline
143 void CMUL(int32_t *x, int32_t *y,
144           int32_t  a, int32_t  b,
145           int32_t  t, int32_t  v)
146 {
147     /* This version loses one bit of precision. Could be solved at the cost
148      * of 2 extra cycles if it becomes an issue. */
149     int x1, y1, l;
150     asm(
151         "smull    %[l], %[y1], %[b], %[t] \n"
152         "smlal    %[l], %[y1], %[a], %[v] \n"
153         "rsb      %[b], %[b], #0          \n"
154         "smull    %[l], %[x1], %[a], %[t] \n"
155         "smlal    %[l], %[x1], %[b], %[v] \n"
156         : [l] "=&r" (l), [x1]"=&r" (x1), [y1]"=&r" (y1), [b] "+r" (b)
157         : [a] "r" (a),   [t] "r" (t),    [v] "r" (v)
158         : "cc"
159     );
160     *x = x1 << 1;
161     *y = y1 << 1;
162 }
163 #elif defined CPU_COLDFIRE
164 static inline
165 void CMUL(int32_t *x, int32_t *y,
166           int32_t  a, int32_t  b,
167           int32_t  t, int32_t  v)
168 {
169   asm volatile ("mac.l %[a], %[t], %%acc0;"
170                 "msac.l %[b], %[v], %%acc0;"
171                 "mac.l %[b], %[t], %%acc1;"
172                 "mac.l %[a], %[v], %%acc1;"
173                 "movclr.l %%acc0, %[a];"
174                 "move.l %[a], (%[x]);"
175                 "movclr.l %%acc1, %[a];"
176                 "move.l %[a], (%[y]);"
177                 : [a] "+&r" (a)
178                 : [x] "a" (x), [y] "a" (y),
179                   [b] "r" (b), [t] "r" (t), [v] "r" (v)
180                 : "cc", "memory");
181 }
182 #else
183 static inline
184 void CMUL(int32_t *pre,
185           int32_t *pim,
186           int32_t are,
187           int32_t aim,
188           int32_t bre,
189           int32_t bim)
190 {
191     //int64_t x,y;
192     int32_t _aref = are;
193     int32_t _aimf = aim;
194     int32_t _bref = bre;
195     int32_t _bimf = bim;
196     int32_t _r1 = fixmul32b(_bref, _aref);
197     int32_t _r2 = fixmul32b(_bimf, _aimf);
198     int32_t _r3 = fixmul32b(_bref, _aimf);
199     int32_t _r4 = fixmul32b(_bimf, _aref);
200     *pre = _r1 - _r2;
201     *pim = _r3 + _r4;
202
203 }
204 #endif