Debian Codegraph @

src/vapoursynth/VSHelper4.h

raw symbols SCIP
1 /*****************************************************************************
2 * Copyright (c) 2012-2020 Fredrik Mellbin
3 * --- Legal stuff ---
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://sam.zoy.org/wtfpl/COPYING for more details.
9 *****************************************************************************/
10
11 #ifndef VSHELPER4_H
12 #define VSHELPER4_H
13
14 #include <limits.h>
15 #include <stdint.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <assert.h>
19 #include <math.h>
20 #include <float.h>
21 #ifdef _WIN32
22 #include <malloc.h>
23 #endif
24 #include "VapourSynth4.h"
25
26 #define VSH_STD_PLUGIN_ID "com.vapoursynth.std"
27 #define VSH_RESIZE_PLUGIN_ID "com.vapoursynth.resize"
28 #define VSH_TEXT_PLUGIN_ID "com.vapoursynth.text"
29
30 #ifdef __cplusplus
31 namespace vsh {
32 #define VSH4_MANGLE_FUNCTION_NAME(name) name
33 #define VSH4_BOOLEAN_TYPE bool
34 #else
35 #define VSH4_MANGLE_FUNCTION_NAME(name) vsh_##name
36 #define VSH4_BOOLEAN_TYPE int
37 #endif
38
39 /* Visual Studio doesn't recognize inline in c mode */
40 #if defined(_MSC_VER) && !defined(__cplusplus)
41 #define inline _inline
42 #endif
43
44 /* A kinda portable definition of the C99 restrict keyword (or its inofficial C++ equivalent) */
45 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* Available in C99 */
46 #define VS_RESTRICT restrict
47 #elif defined(__cplusplus) || defined(_MSC_VER) /* Almost all relevant C++ compilers support it so just assume it works */
48 #define VS_RESTRICT __restrict
49 #else /* Not supported */
50 #define VS_RESTRICT
51 #endif
52
53 #ifdef _WIN32
54 #define VSH_ALIGNED_MALLOC(pptr, size, alignment) do { *(pptr) = _aligned_malloc((size), (alignment)); } while (0)
55 #define VSH_ALIGNED_FREE(ptr) do { _aligned_free((ptr)); } while (0)
56 #else
57 #define VSH_ALIGNED_MALLOC(pptr, size, alignment) do { if(posix_memalign((void**)(pptr), (alignment), (size))) *((void**)pptr) = NULL; } while (0)
58 #define VSH_ALIGNED_FREE(ptr) do { free((ptr)); } while (0)
59 #endif
60
61 #define VSMAX(a,b) ((a) > (b) ? (a) : (b))
62 #define VSMIN(a,b) ((a) > (b) ? (b) : (a))
63
64 #ifdef __cplusplus
65 /* A nicer templated malloc for all the C++ users out there */
66 #if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
67 template<typename T = void>
68 #else
69 template<typename T>
70 #endif
71 static inline T *vsh_aligned_malloc(size_t size, size_t alignment) {
72 #ifdef _WIN32
73 return (T *)_aligned_malloc(size, alignment);
74 #else
75 void *tmp = NULL;
76 if (posix_memalign(&tmp, alignment, size))
77 tmp = 0;
78 return (T *)tmp;
79 #endif
80 }
81
82 static inline void vsh_aligned_free(void *ptr) {
83 VSH_ALIGNED_FREE(ptr);
84 }
85 #endif /* __cplusplus */
86
87 /* convenience function for checking if the format never changes between frames */
88 static inline VSH4_BOOLEAN_TYPE VSH4_MANGLE_FUNCTION_NAME(isConstantVideoFormat)(const VSVideoInfo *vi) {
89 return vi->height > 0 && vi->width > 0 && vi->format.colorFamily != cfUndefined;
90 }
91
92 /* convenience function to check for if two clips have the same format (unknown/changeable will be considered the same too) */
93 static inline VSH4_BOOLEAN_TYPE VSH4_MANGLE_FUNCTION_NAME(isSameVideoFormat)(const VSVideoFormat *v1, const VSVideoFormat *v2) {
94 return v1->colorFamily == v2->colorFamily && v1->sampleType == v2->sampleType && v1->bitsPerSample == v2->bitsPerSample && v1->subSamplingW == v2->subSamplingW && v1->subSamplingH == v2->subSamplingH;
95 }
96
97 /* convenience function to check for if two clips have the same format (but not framerate) while also including width and height (unknown/changeable will be considered the same too) */
98 static inline VSH4_BOOLEAN_TYPE VSH4_MANGLE_FUNCTION_NAME(isSameVideoInfo)(const VSVideoInfo *v1, const VSVideoInfo *v2) {
99 return v1->height == v2->height && v1->width == v2->width && VSH4_MANGLE_FUNCTION_NAME(isSameVideoFormat)(&v1->format, &v2->format);
100 }
101
102 /* convenience function to check for if two clips have the same format while also including samplerate (unknown/changeable will be considered the same too) */
103 static inline VSH4_BOOLEAN_TYPE VSH4_MANGLE_FUNCTION_NAME(isSameAudioFormat)(const VSAudioFormat *a1, const VSAudioFormat *a2) {
104 return a1->bitsPerSample == a2->bitsPerSample && a1->sampleType == a2->sampleType && a1->channelLayout == a2->channelLayout;
105 }
106
107 /* convenience function to check for if two clips have the same format while also including samplerate (unknown/changeable will be considered the same too) */
108 static inline VSH4_BOOLEAN_TYPE VSH4_MANGLE_FUNCTION_NAME(isSameAudioInfo)(const VSAudioInfo *a1, const VSAudioInfo *a2) {
109 return a1->sampleRate == a2->sampleRate && VSH4_MANGLE_FUNCTION_NAME(isSameAudioFormat)(&a1->format, &a2->format);
110 }
111
112 /* multiplies and divides a rational number, such as a frame duration, in place and reduces the result */
113 static inline void VSH4_MANGLE_FUNCTION_NAME(muldivRational)(int64_t *num, int64_t *den, int64_t mul, int64_t div) {
114 /* do nothing if the rational number is invalid */
115 if (!*den)
116 return;
117
118 /* nobody wants to accidentally divide by zero */
119 assert(div);
120
121 int64_t a, b;
122 *num *= mul;
123 *den *= div;
124 a = *num;
125 b = *den;
126 while (b != 0) {
127 int64_t t = a;
128 a = b;
129 b = t % b;
130 }
131 if (a < 0)
132 a = -a;
133 *num /= a;
134 *den /= a;
135 }
136
137 /* reduces a rational number */
138 static inline void VSH4_MANGLE_FUNCTION_NAME(reduceRational)(int64_t *num, int64_t *den) {
139 VSH4_MANGLE_FUNCTION_NAME(muldivRational)(num, den, 1, 1);
140 }
141
142 /* add two rational numbers and reduces the result */
143 static inline void VSH4_MANGLE_FUNCTION_NAME(addRational)(int64_t *num, int64_t *den, int64_t addnum, int64_t addden) {
144 /* do nothing if the rational number is invalid */
145 if (!*den)
146 return;
147
148 /* nobody wants to accidentally add an invalid rational number */
149 assert(addden);
150
151 if (*den == addden) {
152 *num += addnum;
153 } else {
154 int64_t temp = addden;
155 addnum *= *den;
156 addden *= *den;
157 *num *= temp;
158 *den *= temp;
159
160 *num += addnum;
161
162 VSH4_MANGLE_FUNCTION_NAME(reduceRational)(num, den);
163 }
164 }
165
166 /* converts an int64 to int with saturation, useful to silence warnings when reading int properties among other things */
167 static inline int VSH4_MANGLE_FUNCTION_NAME(int64ToIntS)(int64_t i) {
168 if (i > INT_MAX)
169 return INT_MAX;
170 else if (i < INT_MIN)
171 return INT_MIN;
172 else return (int)i;
173 }
174
175 /* converts a double to float with saturation, useful to silence warnings when reading float properties among other things */
176 static inline float VSH4_MANGLE_FUNCTION_NAME(doubleToFloatS)(double d) {
177 if (!isfinite(d))
178 return (float)d;
179 else if (d > FLT_MAX)
180 return FLT_MAX;
181 else if (d < -FLT_MAX)
182 return -FLT_MAX;
183 else
184 return (float)d;
185 }
186
187 static inline void VSH4_MANGLE_FUNCTION_NAME(bitblt)(void *dstp, ptrdiff_t dst_stride, const void *srcp, ptrdiff_t src_stride, size_t row_size, size_t height) {
188 if (height) {
189 if (src_stride == dst_stride && src_stride == (ptrdiff_t)row_size) {
190 memcpy(dstp, srcp, row_size * height);
191 } else {
192 const uint8_t *srcp8 = (const uint8_t *)srcp;
193 uint8_t *dstp8 = (uint8_t *)dstp;
194 size_t i;
195 for (i = 0; i < height; i++) {
196 memcpy(dstp8, srcp8, row_size);
197 srcp8 += src_stride;
198 dstp8 += dst_stride;
199 }
200 }
201 }
202 }
203
204 /* check if the frame dimensions are valid for a given format */
205 /* returns non-zero for valid width and height */
206 static inline VSH4_BOOLEAN_TYPE VSH4_MANGLE_FUNCTION_NAME(areValidDimensions)(const VSVideoFormat *fi, int width, int height) {
207 return !(width % (1 << fi->subSamplingW) || height % (1 << fi->subSamplingH));
208 }
209
210 /* Visual Studio doesn't recognize inline in c mode */
211 #if defined(_MSC_VER) && !defined(__cplusplus)
212 #undef inline
213 #endif
214
215 #ifdef __cplusplus
216 }
217 #endif
218
219 #endif