| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 FFmpeg. | ||
| 7 | * | ||
| 8 | * FFmpeg 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 | * FFmpeg 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 FFmpeg; 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/attributes_internal.h" | ||
| 28 | #include "libavutil/common.h" | ||
| 29 | #include "config.h" | ||
| 30 | |||
| 31 | #define MAX_NEG_CROP 1024 | ||
| 32 | |||
| 33 | extern const uint32_t ff_inverse[257]; | ||
| 34 | extern const uint8_t ff_log2_run[41]; | ||
| 35 | EXTERN const uint32_t ff_square_tab[512]; | ||
| 36 | extern const uint8_t ff_sqrt_tab[256]; | ||
| 37 | EXTERN const uint8_t ff_crop_tab[256 + 2 * MAX_NEG_CROP]; | ||
| 38 | extern const uint8_t ff_zigzag_direct[64]; | ||
| 39 | extern const uint8_t ff_zigzag_scan[16+1]; | ||
| 40 | |||
| 41 | #if ARCH_ARM | ||
| 42 | # include "arm/mathops.h" | ||
| 43 | #elif ARCH_MIPS | ||
| 44 | # include "mips/mathops.h" | ||
| 45 | #elif ARCH_PPC | ||
| 46 | # include "ppc/mathops.h" | ||
| 47 | #elif ARCH_X86 | ||
| 48 | # include "x86/mathops.h" | ||
| 49 | #endif | ||
| 50 | |||
| 51 | /* generic implementation */ | ||
| 52 | |||
| 53 | #ifndef MUL64 | ||
| 54 | # define MUL64(a,b) ((int64_t)(a) * (int64_t)(b)) | ||
| 55 | #endif | ||
| 56 | |||
| 57 | #ifndef MULL | ||
| 58 | # define MULL(a,b,s) (MUL64(a, b) >> (s)) | ||
| 59 | #endif | ||
| 60 | |||
| 61 | #ifndef MULH | ||
| 62 | 47933229 | static av_always_inline int MULH(int a, int b){ | |
| 63 | 47933229 | return MUL64(a, b) >> 32; | |
| 64 | } | ||
| 65 | #endif | ||
| 66 | |||
| 67 | #ifndef UMULH | ||
| 68 | 499 | static av_always_inline unsigned UMULH(unsigned a, unsigned b){ | |
| 69 | 499 | return ((uint64_t)(a) * (uint64_t)(b))>>32; | |
| 70 | } | ||
| 71 | #endif | ||
| 72 | |||
| 73 | #ifndef MAC64 | ||
| 74 | # define MAC64(d, a, b) ((d) += MUL64(a, b)) | ||
| 75 | #endif | ||
| 76 | |||
| 77 | #ifndef MLS64 | ||
| 78 | # define MLS64(d, a, b) ((d) -= MUL64(a, b)) | ||
| 79 | #endif | ||
| 80 | |||
| 81 | /* signed 16x16 -> 32 multiply add accumulate */ | ||
| 82 | #ifndef MAC16 | ||
| 83 | # define MAC16(rt, ra, rb) rt += (ra) * (rb) | ||
| 84 | #endif | ||
| 85 | |||
| 86 | /* signed 16x16 -> 32 multiply */ | ||
| 87 | #ifndef MUL16 | ||
| 88 | # define MUL16(ra, rb) ((ra) * (rb)) | ||
| 89 | #endif | ||
| 90 | |||
| 91 | #ifndef MLS16 | ||
| 92 | # define MLS16(rt, ra, rb) ((rt) -= (ra) * (rb)) | ||
| 93 | #endif | ||
| 94 | |||
| 95 | /* median of 3 */ | ||
| 96 | #ifndef mid_pred | ||
| 97 | #define mid_pred mid_pred | ||
| 98 | static inline av_const int mid_pred(int a, int b, int c) | ||
| 99 | { | ||
| 100 | if(a>b){ | ||
| 101 | if(c>b){ | ||
| 102 | if(c>a) b=a; | ||
| 103 | else b=c; | ||
| 104 | } | ||
| 105 | }else{ | ||
| 106 | if(b>c){ | ||
| 107 | if(c>a) b=c; | ||
| 108 | else b=a; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | return b; | ||
| 112 | } | ||
| 113 | #endif | ||
| 114 | |||
| 115 | #ifndef median4 | ||
| 116 | #define median4 median4 | ||
| 117 | 74348 | static inline av_const int median4(int a, int b, int c, int d) | |
| 118 | { | ||
| 119 |
2/2✓ Branch 0 taken 10403 times.
✓ Branch 1 taken 63945 times.
|
74348 | if (a < b) { |
| 120 |
2/2✓ Branch 0 taken 2971 times.
✓ Branch 1 taken 7432 times.
|
10403 | if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2; |
| 121 | 7432 | else return (FFMIN(b, c) + FFMAX(a, d)) / 2; | |
| 122 | } else { | ||
| 123 |
2/2✓ Branch 0 taken 5325 times.
✓ Branch 1 taken 58620 times.
|
63945 | if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2; |
| 124 | 58620 | else return (FFMIN(a, c) + FFMAX(b, d)) / 2; | |
| 125 | } | ||
| 126 | } | ||
| 127 | #endif | ||
| 128 | |||
| 129 | #define FF_SIGNBIT(x) ((x) >> CHAR_BIT * sizeof(x) - 1) | ||
| 130 | |||
| 131 | #ifndef sign_extend | ||
| 132 | 416982618 | static inline av_const int sign_extend(int val, unsigned bits) | |
| 133 | { | ||
| 134 | 416982618 | unsigned shift = 8 * sizeof(int) - bits; | |
| 135 | 416982618 | union { unsigned u; int s; } v = { (unsigned) val << shift }; | |
| 136 | 416982618 | return v.s >> shift; | |
| 137 | } | ||
| 138 | #endif | ||
| 139 | |||
| 140 | #ifndef sign_extend64 | ||
| 141 | 5648 | static inline av_const int64_t sign_extend64(int64_t val, unsigned bits) | |
| 142 | { | ||
| 143 | 5648 | unsigned shift = 8 * sizeof(int64_t) - bits; | |
| 144 | 5648 | union { uint64_t u; int64_t s; } v = { (uint64_t) val << shift }; | |
| 145 | 5648 | return v.s >> shift; | |
| 146 | } | ||
| 147 | #endif | ||
| 148 | |||
| 149 | #ifndef zero_extend | ||
| 150 | 80389289 | static inline av_const unsigned zero_extend(unsigned val, unsigned bits) | |
| 151 | { | ||
| 152 | 80389289 | return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits); | |
| 153 | } | ||
| 154 | #endif | ||
| 155 | |||
| 156 | #ifndef COPY3_IF_LT | ||
| 157 | #define COPY3_IF_LT(x, y, a, b, c, d)\ | ||
| 158 | if ((y) < (x)) {\ | ||
| 159 | (x) = (y);\ | ||
| 160 | (a) = (b);\ | ||
| 161 | (c) = (d);\ | ||
| 162 | } | ||
| 163 | #endif | ||
| 164 | |||
| 165 | #ifndef MASK_ABS | ||
| 166 | #define MASK_ABS(mask, level) do { \ | ||
| 167 | mask = level >> 31; \ | ||
| 168 | level = (level ^ mask) - mask; \ | ||
| 169 | } while (0) | ||
| 170 | #endif | ||
| 171 | |||
| 172 | #ifndef NEG_SSR32 | ||
| 173 | # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s))) | ||
| 174 | #endif | ||
| 175 | |||
| 176 | #ifndef NEG_USR32 | ||
| 177 | # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s))) | ||
| 178 | #endif | ||
| 179 | |||
| 180 | #if HAVE_BIGENDIAN | ||
| 181 | # ifndef PACK_2U8 | ||
| 182 | # define PACK_2U8(a,b) (((a) << 8) | (b)) | ||
| 183 | # endif | ||
| 184 | # ifndef PACK_4U8 | ||
| 185 | # define PACK_4U8(a,b,c,d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d)) | ||
| 186 | # endif | ||
| 187 | # ifndef PACK_2U16 | ||
| 188 | # define PACK_2U16(a,b) (((a) << 16) | (b)) | ||
| 189 | # endif | ||
| 190 | #else | ||
| 191 | # ifndef PACK_2U8 | ||
| 192 | # define PACK_2U8(a,b) (((b) << 8) | (a)) | ||
| 193 | # endif | ||
| 194 | # ifndef PACK_4U2 | ||
| 195 | # define PACK_4U8(a,b,c,d) (((d) << 24) | ((c) << 16) | ((b) << 8) | (a)) | ||
| 196 | # endif | ||
| 197 | # ifndef PACK_2U16 | ||
| 198 | # define PACK_2U16(a,b) (((b) << 16) | (a)) | ||
| 199 | # endif | ||
| 200 | #endif | ||
| 201 | |||
| 202 | #ifndef PACK_2S8 | ||
| 203 | # define PACK_2S8(a,b) PACK_2U8((a)&255, (b)&255) | ||
| 204 | #endif | ||
| 205 | #ifndef PACK_4S8 | ||
| 206 | # define PACK_4S8(a,b,c,d) PACK_4U8((a)&255, (b)&255, (c)&255, (d)&255) | ||
| 207 | #endif | ||
| 208 | #ifndef PACK_2S16 | ||
| 209 | # define PACK_2S16(a,b) PACK_2U16((a)&0xffff, (b)&0xffff) | ||
| 210 | #endif | ||
| 211 | |||
| 212 | #ifndef FASTDIV | ||
| 213 | # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a) * ff_inverse[b]) >> 32)) | ||
| 214 | #endif /* FASTDIV */ | ||
| 215 | |||
| 216 | #ifndef ff_sqrt | ||
| 217 | #define ff_sqrt ff_sqrt | ||
| 218 | 7626644 | static inline av_const unsigned int ff_sqrt(unsigned int a) | |
| 219 | { | ||
| 220 | unsigned int b; | ||
| 221 | |||
| 222 |
2/2✓ Branch 0 taken 1000087 times.
✓ Branch 1 taken 6626557 times.
|
7626644 | if (a < 255) return (ff_sqrt_tab[a + 1] - 1) >> 4; |
| 223 |
2/2✓ Branch 0 taken 883322 times.
✓ Branch 1 taken 5743235 times.
|
6626557 | else if (a < (1 << 12)) b = ff_sqrt_tab[a >> 4] >> 2; |
| 224 | #if !CONFIG_SMALL | ||
| 225 |
2/2✓ Branch 0 taken 1071923 times.
✓ Branch 1 taken 4671312 times.
|
5743235 | else if (a < (1 << 14)) b = ff_sqrt_tab[a >> 6] >> 1; |
| 226 |
2/2✓ Branch 0 taken 1079880 times.
✓ Branch 1 taken 3591432 times.
|
4671312 | else if (a < (1 << 16)) b = ff_sqrt_tab[a >> 8] ; |
| 227 | #endif | ||
| 228 | else { | ||
| 229 | 3591432 | int s = av_log2_16bit(a >> 16) >> 1; | |
| 230 | 3591432 | unsigned int c = a >> (s + 2); | |
| 231 | 3591432 | b = ff_sqrt_tab[c >> (s + 8)]; | |
| 232 | 3591432 | b = FASTDIV(c,b) + (b << s); | |
| 233 | } | ||
| 234 | |||
| 235 | 6626557 | return b - (a < b * b); | |
| 236 | } | ||
| 237 | #endif | ||
| 238 | |||
| 239 | 30177 | static inline av_const float ff_sqrf(float a) | |
| 240 | { | ||
| 241 | 30177 | return a*a; | |
| 242 | } | ||
| 243 | |||
| 244 | 393216 | static inline int8_t ff_u8_to_s8(uint8_t a) | |
| 245 | { | ||
| 246 | union { | ||
| 247 | uint8_t u8; | ||
| 248 | int8_t s8; | ||
| 249 | } b; | ||
| 250 | 393216 | b.u8 = a; | |
| 251 | 393216 | return b.s8; | |
| 252 | } | ||
| 253 | |||
| 254 | #endif /* AVCODEC_MATHOPS_H */ | ||
| 255 |