1 // Copyright (c) 2012 Fredrik Mellbin
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:
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
31 #include "VapourSynth.h"
33 // VS2010 doesn't recognize inline in c mode
34 #if defined(_MSC_VER) && !defined(__cplusplus)
35 #define inline _inline
39 #define VS_ALIGNED_MALLOC(pptr, size, alignment) *(pptr) = _aligned_malloc((size), (alignment))
40 #define VS_ALIGNED_FREE(ptr) _aligned_free((ptr))
42 #define VS_ALIGNED_MALLOC(pptr, size, alignment) posix_memalign((void**)(pptr), (alignment), (size))
43 #define VS_ALIGNED_FREE(ptr) free((ptr))
47 // A nicer templated malloc for all the C++ users out there
49 static inline T
* vs_aligned_malloc(size_t size
, size_t alignment
) {
51 return (T
*)_aligned_malloc(size
, alignment
);
54 if (posix_memalign(&tmp
, alignment
, size
))
60 static inline void vs_aligned_free(void *ptr
) {
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
) {
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
) {
103 static inline void vs_bitblt(void *dstp
, int dst_stride
, const void *srcp
, int src_stride
, int row_size
, int height
) {
105 if (src_stride
== dst_stride
&& src_stride
== row_size
) {
106 memcpy(dstp
, srcp
, row_size
* height
);
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
);
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
));