1 /*
2 * Copyright (c) 2012-2013 Fredrik Mellbin
3 *
4 * This file is part of VapourSynth.
5 *
6 * VapourSynth is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * VapourSynth is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with VapourSynth; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 #ifndef FILTERSHARED_H
22 #define FILTERSHARED_H
23 
24 #include "VapourSynth.h"
25 #include "VSHelper.h"
26 #include <string.h>
27 
28 #define RETERROR(x) do { vsapi->setError(out, (x)); return; } while (0)
29 
vs_memset8(void * ptr,int value,size_t num)30 static inline void vs_memset8(void *ptr, int value, size_t num) {
31     memset(ptr, value, num);
32 }
33 
vs_memset16(void * ptr,int value,size_t num)34 static inline void vs_memset16(void *ptr, int value, size_t num) {
35     uint16_t *tptr = (uint16_t *)ptr;
36     while (num-- > 0)
37         *tptr++ = (uint16_t)value;
38 }
39 
vs_memset32(void * ptr,int value,size_t num)40 static inline void vs_memset32(void *ptr, int value, size_t num) {
41     int32_t *tptr = (int32_t *)ptr;
42     while (num-- > 0)
43         *tptr++ = (int32_t)value;
44 }
45 
vs_memset_float(void * ptr,float value,size_t num)46 static inline void vs_memset_float(void *ptr, float value, size_t num) {
47     float *tptr = (float *)ptr;
48     while (num-- > 0)
49         *tptr++ = value;
50 }
51 
52 // to detect compat formats
isCompatFormat(const VSVideoInfo * vi)53 static inline int isCompatFormat(const VSVideoInfo *vi) {
54     return vi->format && vi->format->colorFamily == cmCompat;
55 }
56 
57 // to get the width/height of a plane easily when not having a frame around
planeWidth(const VSVideoInfo * vi,int plane)58 static inline int planeWidth(const VSVideoInfo *vi, int plane) {
59     return vi->width >> (plane ? vi->format->subSamplingW : 0);
60 }
61 
planeHeight(const VSVideoInfo * vi,int plane)62 static inline int planeHeight(const VSVideoInfo *vi, int plane) {
63     return vi->height >> (plane ? vi->format->subSamplingH : 0);
64 }
65 
66 // get the triplet representing black for any colorspace (works for union with float too since it's always 0)
setBlack(uint32_t color[3],const VSFormat * format)67 static inline void setBlack(uint32_t color[3], const VSFormat *format) {
68     for (int i = 0; i < 3; i++)
69         color[i] = 0;
70     if (format->sampleType == stInteger && (format->colorFamily == cmYUV || format->colorFamily == cmYCoCg))
71         color[1] = color[2] = (1 << (format->bitsPerSample - 1));
72     else if (format->id == pfCompatYUY2)
73         color[1] = color[2] = 128;
74 }
75 
76 typedef struct {
77     VSNodeRef *node;
78     const VSVideoInfo *vi;
79 } SingleClipData;
80 
singleClipInit(VSMap * in,VSMap * out,void ** instanceData,VSNode * node,VSCore * core,const VSAPI * vsapi)81 static void VS_CC singleClipInit(VSMap *in, VSMap *out, void **instanceData, VSNode *node, VSCore *core, const VSAPI *vsapi) {
82     SingleClipData *d = (SingleClipData *) * instanceData;
83     vsapi->setVideoInfo(vsapi->getVideoInfo(d->node), 1, node);
84 }
85 
singleClipFree(void * instanceData,VSCore * core,const VSAPI * vsapi)86 static void VS_CC singleClipFree(void *instanceData, VSCore *core, const VSAPI *vsapi) {
87     SingleClipData *d = (SingleClipData *)instanceData;
88     vsapi->freeNode(d->node);
89     free(instanceData);
90 }
91 
floatToInt64S(float f)92 static inline int64_t floatToInt64S(float f) {
93     if (f > INT64_MAX)
94         return INT64_MAX;
95     else if (f < INT64_MIN)
96         return INT64_MIN;
97     else
98         return (int64_t)llround(f);
99 }
100 
floatToIntS(float f)101 static inline int floatToIntS(float f) {
102     if (f > INT_MAX)
103         return INT_MAX;
104     else if (f < INT_MIN)
105         return INT_MIN;
106     else
107         return (int)lround(f);
108 }
109 
110 #endif // FILTERSHARED_H
111