Fix things on case-insensitive file systems.
[vapoursynth-svn.git] / include / VSHelper.h
blob223c53b00598cb4d7225c9577b9f254129d1236a
1 // Copyright (c) 2012 Fredrik Mellbin
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
21 #ifndef VSHELPER_H
22 #define VSHELPER_H
24 #include <limits.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #ifdef _WIN32
29 #include <malloc.h>
30 #endif
31 #include "VapourSynth.h"
33 // VS2010 doesn't recognize inline in c mode
34 #if defined(_MSC_VER) && !defined(__cplusplus)
35 #define inline _inline
36 #endif
38 #ifdef _WIN32
39 #define VS_ALIGNED_MALLOC(pptr, size, alignment) *(pptr) = _aligned_malloc((size), (alignment))
40 #define VS_ALIGNED_FREE(ptr) _aligned_free((ptr))
41 #else
42 #define VS_ALIGNED_MALLOC(pptr, size, alignment) posix_memalign((void**)(pptr), (alignment), (size))
43 #define VS_ALIGNED_FREE(ptr) free((ptr))
44 #endif
46 #ifdef __cplusplus
47 // A nicer templated malloc for all the C++ users out there
48 template<class T>
49 static inline T* vs_aligned_malloc(size_t size, size_t alignment) {
50 #ifdef _WIN32
51 return (T*)_aligned_malloc(size, alignment);
52 #else
53 void *tmp;
54 if (posix_memalign(&tmp, alignment, size))
55 tmp = 0;
56 return (T*)tmp;
57 #endif
60 static inline void vs_aligned_free(void *ptr) {
61 VS_ALIGNED_FREE(ptr);
63 #endif //__cplusplus
65 // convenience function for checking if the format never changes between frames
66 static inline int isConstantFormat(const VSVideoInfo *vi) {
67 return vi->height > 0 && vi->width > 0 && vi->format;
70 // convenience function to check for if two clips have the same format (unknown/changeable will be considered the same too)
71 static inline int isSameFormat(const VSVideoInfo *v1, const VSVideoInfo *v2) {
72 return v1->height == v2->height && v1->width == v2->width && v1->format == v2->format;
75 // multiplies and divides a rational number, such as a frame duration, in place and reduces the result
76 static inline int muldivRational(int64_t *num, int64_t *den, int64_t mul, int64_t div) {
77 int64_t a, b;
78 *num *= mul;
79 *den *= div;
80 a = *num;
81 b = *den;
82 while (b != 0) {
83 int64_t t = a;
84 a = b;
85 b = t % b;
87 if (a < 0)
88 a = -a;
89 *num /= a;
90 *den /= a;
91 return 0;
94 // converts an int64 to int with saturation, useful to silence warnings when reading int properties among other things
95 static inline int int64ToIntS(int64_t i) {
96 if (i > INT_MAX)
97 return INT_MAX;
98 else if (i < INT_MIN)
99 return INT_MIN;
100 else return (int)i;
103 static inline void vs_bitblt(void *dstp, int dst_stride, const void *srcp, int src_stride, int row_size, int height) {
104 if (height > 0) {
105 if (src_stride == dst_stride && src_stride == row_size) {
106 memcpy(dstp, srcp, row_size * height);
107 } else {
108 int i;
109 uint8_t *srcp8 = (uint8_t *)srcp;
110 uint8_t *dstp8 = (uint8_t *)dstp;
111 for (i = 0; i < height; i++) {
112 memcpy(dstp8, srcp8, row_size);
113 srcp8 += src_stride;
114 dstp8 += dst_stride;
120 // check if the frame dimensions are valid for a given format
121 // returns non-zero for valid width and height
122 static inline int areValidDimensions(const VSFormat *fi, int width, int height) {
123 return !(width % (1 << fi->subSamplingW) || height % (1 << fi->subSamplingH));
126 #endif