1 // Avisynth C Interface Version 0.20
2 // Copyright 2003 Kevin Atkinson
3 
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 // MA 02110-1301 USA, or visit
18 // http://www.gnu.org/copyleft/gpl.html .
19 //
20 // As a special exception, I give you permission to link to the
21 // Avisynth C interface with independent modules that communicate with
22 // the Avisynth C interface solely through the interfaces defined in
23 // avisynth_c.h, regardless of the license terms of these independent
24 // modules, and to copy and distribute the resulting combined work
25 // under terms of your choice, provided that every copy of the
26 // combined work is accompanied by a complete copy of the source code
27 // of the Avisynth C interface and Avisynth itself (with the version
28 // used to produce the combined work), being distributed under the
29 // terms of the GNU General Public License plus this exception.  An
30 // independent module is a module which is not derived from or based
31 // on Avisynth C Interface, such as 3rd-party filters, import and
32 // export plugins, or graphical user interfaces.
33 
34 // NOTE: this is a partial update of the Avisynth C interface to recognize
35 // new color spaces added in Avisynth 2.60. By no means is this document
36 // completely Avisynth 2.60 compliant.
37 // 170103: added new CPU constants (FMA4, AVX512xx)
38 // 171102: define SIZETMOD. do not use yet, experimental. Offsets are size_t instead of int. Affects x64.
39 // 171106: avs_get_row_size calls into avs_get_row_size_p, instead of direct field access
40 // 171106: avs_get_height calls into avs_get_row_size_p, instead of direct field access
41 // 180524: AVSC_EXPORT to dllexport in capi.h for avisynth_c_plugin_init
42 // 180524: avs_is_same_colorspace VideoInfo parameters to const
43 // 181230: Readability: functions regrouped to mix less AVSC_API and AVSC_INLINE, put together Avisynth+ specific stuff
44 // 181230: use #ifndef AVSC_NO_DECLSPEC for AVSC_INLINE functions which are calling API functions
45 // 181230: comments on avs_load_library (helper for loading API entries dynamically into a struct using AVSC_NO_DECLSPEC define)
46 // 181230: define alias AVS_FRAME_ALIGN as FRAME_ALIGN
47 // 181230: remove unused form of avs_get_rowsize and avs_get_height (kept earlier for reference)
48 // 190104: avs_load_library: smart fallback mechanism for Avisynth+ specific functions:
49 //         if they are not loadable, they will work in a classic Avisynth compatible mode
50 //         Example#1: e.g. avs_is_444 will call the existing avs_is_yv24 instead
51 //         Example#2: avs_bits_per_component will return 8 for all colorspaces (Classic Avisynth supports only 8 bits/pixel)
52 //         Thus the Avisynth+ specific API functions are safely callable even when connected to classic Avisynth DLL
53 
54 #ifndef __AVISYNTH_C__
55 #define __AVISYNTH_C__
56 
57 #include "avs/config.h"
58 #include "avs/capi.h"
59 #include "avs/types.h"
60 
61 #define AVS_FRAME_ALIGN FRAME_ALIGN
62 /////////////////////////////////////////////////////////////////////
63 //
64 // Constants
65 //
66 
67 #ifndef __AVISYNTH_6_H__
68 enum { AVISYNTH_INTERFACE_VERSION = 6 };
69 #endif
70 
71 enum {AVS_SAMPLE_INT8  = 1<<0,
72       AVS_SAMPLE_INT16 = 1<<1,
73       AVS_SAMPLE_INT24 = 1<<2,
74       AVS_SAMPLE_INT32 = 1<<3,
75       AVS_SAMPLE_FLOAT = 1<<4};
76 
77 enum {AVS_PLANAR_Y=1<<0,
78       AVS_PLANAR_U=1<<1,
79       AVS_PLANAR_V=1<<2,
80       AVS_PLANAR_ALIGNED=1<<3,
81       AVS_PLANAR_Y_ALIGNED=AVS_PLANAR_Y|AVS_PLANAR_ALIGNED,
82       AVS_PLANAR_U_ALIGNED=AVS_PLANAR_U|AVS_PLANAR_ALIGNED,
83       AVS_PLANAR_V_ALIGNED=AVS_PLANAR_V|AVS_PLANAR_ALIGNED,
84       AVS_PLANAR_A=1<<4,
85       AVS_PLANAR_R=1<<5,
86       AVS_PLANAR_G=1<<6,
87       AVS_PLANAR_B=1<<7,
88       AVS_PLANAR_A_ALIGNED=AVS_PLANAR_A|AVS_PLANAR_ALIGNED,
89       AVS_PLANAR_R_ALIGNED=AVS_PLANAR_R|AVS_PLANAR_ALIGNED,
90       AVS_PLANAR_G_ALIGNED=AVS_PLANAR_G|AVS_PLANAR_ALIGNED,
91       AVS_PLANAR_B_ALIGNED=AVS_PLANAR_B|AVS_PLANAR_ALIGNED};
92 
93   // Colorspace properties.
94 enum {
95     AVS_CS_YUVA = 1 << 27,
96     AVS_CS_BGR = 1 << 28,
97     AVS_CS_YUV = 1 << 29,
98     AVS_CS_INTERLEAVED = 1 << 30,
99     AVS_CS_PLANAR = 1 << 31,
100 
101     AVS_CS_SHIFT_SUB_WIDTH = 0,
102     AVS_CS_SHIFT_SUB_HEIGHT = 8,
103     AVS_CS_SHIFT_SAMPLE_BITS = 16,
104 
105     AVS_CS_SUB_WIDTH_MASK = 7 << AVS_CS_SHIFT_SUB_WIDTH,
106     AVS_CS_SUB_WIDTH_1 = 3 << AVS_CS_SHIFT_SUB_WIDTH, // YV24
107     AVS_CS_SUB_WIDTH_2 = 0 << AVS_CS_SHIFT_SUB_WIDTH, // YV12, I420, YV16
108     AVS_CS_SUB_WIDTH_4 = 1 << AVS_CS_SHIFT_SUB_WIDTH, // YUV9, YV411
109 
110     AVS_CS_VPLANEFIRST = 1 << 3, // YV12, YV16, YV24, YV411, YUV9
111     AVS_CS_UPLANEFIRST = 1 << 4, // I420
112 
113     AVS_CS_SUB_HEIGHT_MASK = 7 << AVS_CS_SHIFT_SUB_HEIGHT,
114     AVS_CS_SUB_HEIGHT_1 = 3 << AVS_CS_SHIFT_SUB_HEIGHT, // YV16, YV24, YV411
115     AVS_CS_SUB_HEIGHT_2 = 0 << AVS_CS_SHIFT_SUB_HEIGHT, // YV12, I420
116     AVS_CS_SUB_HEIGHT_4 = 1 << AVS_CS_SHIFT_SUB_HEIGHT, // YUV9
117 
118     AVS_CS_SAMPLE_BITS_MASK = 7 << AVS_CS_SHIFT_SAMPLE_BITS,
119     AVS_CS_SAMPLE_BITS_8 = 0 << AVS_CS_SHIFT_SAMPLE_BITS,
120     AVS_CS_SAMPLE_BITS_10 = 5 << AVS_CS_SHIFT_SAMPLE_BITS,
121     AVS_CS_SAMPLE_BITS_12 = 6 << AVS_CS_SHIFT_SAMPLE_BITS,
122     AVS_CS_SAMPLE_BITS_14 = 7 << AVS_CS_SHIFT_SAMPLE_BITS,
123     AVS_CS_SAMPLE_BITS_16 = 1 << AVS_CS_SHIFT_SAMPLE_BITS,
124     AVS_CS_SAMPLE_BITS_32 = 2 << AVS_CS_SHIFT_SAMPLE_BITS,
125 
126     AVS_CS_PLANAR_MASK = AVS_CS_PLANAR | AVS_CS_INTERLEAVED | AVS_CS_YUV | AVS_CS_BGR | AVS_CS_YUVA | AVS_CS_SAMPLE_BITS_MASK | AVS_CS_SUB_HEIGHT_MASK | AVS_CS_SUB_WIDTH_MASK,
127     AVS_CS_PLANAR_FILTER = ~(AVS_CS_VPLANEFIRST | AVS_CS_UPLANEFIRST),
128 
129     AVS_CS_RGB_TYPE  = 1 << 0,
130     AVS_CS_RGBA_TYPE = 1 << 1,
131 
132     AVS_CS_GENERIC_YUV420  = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_2 | AVS_CS_SUB_WIDTH_2,  // 4:2:0 planar
133     AVS_CS_GENERIC_YUV422  = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_1 | AVS_CS_SUB_WIDTH_2,  // 4:2:2 planar
134     AVS_CS_GENERIC_YUV444  = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_1 | AVS_CS_SUB_WIDTH_1,  // 4:4:4 planar
135     AVS_CS_GENERIC_Y       = AVS_CS_PLANAR | AVS_CS_INTERLEAVED | AVS_CS_YUV,                                             // Y only (4:0:0)
136     AVS_CS_GENERIC_RGBP    = AVS_CS_PLANAR | AVS_CS_BGR | AVS_CS_RGB_TYPE,                                                // planar RGB
137     AVS_CS_GENERIC_RGBAP   = AVS_CS_PLANAR | AVS_CS_BGR | AVS_CS_RGBA_TYPE,                                               // planar RGBA
138     AVS_CS_GENERIC_YUVA420 = AVS_CS_PLANAR | AVS_CS_YUVA | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_2 | AVS_CS_SUB_WIDTH_2, // 4:2:0:A planar
139     AVS_CS_GENERIC_YUVA422 = AVS_CS_PLANAR | AVS_CS_YUVA | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_1 | AVS_CS_SUB_WIDTH_2, // 4:2:2:A planar
140     AVS_CS_GENERIC_YUVA444 = AVS_CS_PLANAR | AVS_CS_YUVA | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_1 | AVS_CS_SUB_WIDTH_1 }; // 4:4:4:A planar
141 
142 
143   // Specific color formats
144 enum {
145   AVS_CS_UNKNOWN = 0,
146   AVS_CS_BGR24 = AVS_CS_RGB_TYPE  | AVS_CS_BGR | AVS_CS_INTERLEAVED,
147   AVS_CS_BGR32 = AVS_CS_RGBA_TYPE | AVS_CS_BGR | AVS_CS_INTERLEAVED,
148   AVS_CS_YUY2 = 1<<2 | AVS_CS_YUV | AVS_CS_INTERLEAVED,
149   //  AVS_CS_YV12  = 1<<3  Reserved
150   //  AVS_CS_I420  = 1<<4  Reserved
151   AVS_CS_RAW32 = 1<<5 | AVS_CS_INTERLEAVED,
152 
153   AVS_CS_YV24  = AVS_CS_GENERIC_YUV444 | AVS_CS_SAMPLE_BITS_8,  // YUV 4:4:4 planar
154   AVS_CS_YV16  = AVS_CS_GENERIC_YUV422 | AVS_CS_SAMPLE_BITS_8,  // YUV 4:2:2 planar
155   AVS_CS_YV12  = AVS_CS_GENERIC_YUV420 | AVS_CS_SAMPLE_BITS_8,  // YUV 4:2:0 planar
156   AVS_CS_I420  = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8 | AVS_CS_UPLANEFIRST | AVS_CS_SUB_HEIGHT_2 | AVS_CS_SUB_WIDTH_2,  // YUV 4:2:0 planar
157   AVS_CS_IYUV  = AVS_CS_I420,
158   AVS_CS_YV411 = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8 | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_1 | AVS_CS_SUB_WIDTH_4,  // YUV 4:1:1 planar
159   AVS_CS_YUV9  = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8 | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_4 | AVS_CS_SUB_WIDTH_4,  // YUV 4:1:0 planar
160   AVS_CS_Y8    = AVS_CS_GENERIC_Y | AVS_CS_SAMPLE_BITS_8,       // Y   4:0:0 planar
161 
162   //-------------------------
163   // AVS16: new planar constants go live! Experimental PF 160613
164   // 10-12-14-16 bit + planar RGB + BGR48/64 160725
165   AVS_CS_YUV444P10 = AVS_CS_GENERIC_YUV444 | AVS_CS_SAMPLE_BITS_10, // YUV 4:4:4 10bit samples
166   AVS_CS_YUV422P10 = AVS_CS_GENERIC_YUV422 | AVS_CS_SAMPLE_BITS_10, // YUV 4:2:2 10bit samples
167   AVS_CS_YUV420P10 = AVS_CS_GENERIC_YUV420 | AVS_CS_SAMPLE_BITS_10, // YUV 4:2:0 10bit samples
168   AVS_CS_Y10       = AVS_CS_GENERIC_Y | AVS_CS_SAMPLE_BITS_10,      // Y   4:0:0 10bit samples
169 
170   AVS_CS_YUV444P12 = AVS_CS_GENERIC_YUV444 | AVS_CS_SAMPLE_BITS_12, // YUV 4:4:4 12bit samples
171   AVS_CS_YUV422P12 = AVS_CS_GENERIC_YUV422 | AVS_CS_SAMPLE_BITS_12, // YUV 4:2:2 12bit samples
172   AVS_CS_YUV420P12 = AVS_CS_GENERIC_YUV420 | AVS_CS_SAMPLE_BITS_12, // YUV 4:2:0 12bit samples
173   AVS_CS_Y12       = AVS_CS_GENERIC_Y | AVS_CS_SAMPLE_BITS_12,      // Y   4:0:0 12bit samples
174 
175   AVS_CS_YUV444P14 = AVS_CS_GENERIC_YUV444 | AVS_CS_SAMPLE_BITS_14, // YUV 4:4:4 14bit samples
176   AVS_CS_YUV422P14 = AVS_CS_GENERIC_YUV422 | AVS_CS_SAMPLE_BITS_14, // YUV 4:2:2 14bit samples
177   AVS_CS_YUV420P14 = AVS_CS_GENERIC_YUV420 | AVS_CS_SAMPLE_BITS_14, // YUV 4:2:0 14bit samples
178   AVS_CS_Y14       = AVS_CS_GENERIC_Y | AVS_CS_SAMPLE_BITS_14,      // Y   4:0:0 14bit samples
179 
180   AVS_CS_YUV444P16 = AVS_CS_GENERIC_YUV444 | AVS_CS_SAMPLE_BITS_16, // YUV 4:4:4 16bit samples
181   AVS_CS_YUV422P16 = AVS_CS_GENERIC_YUV422 | AVS_CS_SAMPLE_BITS_16, // YUV 4:2:2 16bit samples
182   AVS_CS_YUV420P16 = AVS_CS_GENERIC_YUV420 | AVS_CS_SAMPLE_BITS_16, // YUV 4:2:0 16bit samples
183   AVS_CS_Y16       = AVS_CS_GENERIC_Y | AVS_CS_SAMPLE_BITS_16,      // Y   4:0:0 16bit samples
184 
185   // 32 bit samples (float)
186   AVS_CS_YUV444PS = AVS_CS_GENERIC_YUV444 | AVS_CS_SAMPLE_BITS_32,  // YUV 4:4:4 32bit samples
187   AVS_CS_YUV422PS = AVS_CS_GENERIC_YUV422 | AVS_CS_SAMPLE_BITS_32,  // YUV 4:2:2 32bit samples
188   AVS_CS_YUV420PS = AVS_CS_GENERIC_YUV420 | AVS_CS_SAMPLE_BITS_32,  // YUV 4:2:0 32bit samples
189   AVS_CS_Y32      = AVS_CS_GENERIC_Y | AVS_CS_SAMPLE_BITS_32,       // Y   4:0:0 32bit samples
190 
191   // RGB packed
192   AVS_CS_BGR48 = AVS_CS_RGB_TYPE | AVS_CS_BGR | AVS_CS_INTERLEAVED | AVS_CS_SAMPLE_BITS_16,    // BGR 3x16 bit
193   AVS_CS_BGR64 = AVS_CS_RGBA_TYPE | AVS_CS_BGR | AVS_CS_INTERLEAVED | AVS_CS_SAMPLE_BITS_16,    // BGR 4x16 bit
194   // no packed 32 bit (float) support for these legacy types
195 
196   // RGB planar
197   AVS_CS_RGBP   = AVS_CS_GENERIC_RGBP | AVS_CS_SAMPLE_BITS_8,  // Planar RGB 8 bit samples
198   AVS_CS_RGBP10 = AVS_CS_GENERIC_RGBP | AVS_CS_SAMPLE_BITS_10, // Planar RGB 10bit samples
199   AVS_CS_RGBP12 = AVS_CS_GENERIC_RGBP | AVS_CS_SAMPLE_BITS_12, // Planar RGB 12bit samples
200   AVS_CS_RGBP14 = AVS_CS_GENERIC_RGBP | AVS_CS_SAMPLE_BITS_14, // Planar RGB 14bit samples
201   AVS_CS_RGBP16 = AVS_CS_GENERIC_RGBP | AVS_CS_SAMPLE_BITS_16, // Planar RGB 16bit samples
202   AVS_CS_RGBPS  = AVS_CS_GENERIC_RGBP | AVS_CS_SAMPLE_BITS_32, // Planar RGB 32bit samples
203 
204   // RGBA planar
205   AVS_CS_RGBAP   = AVS_CS_GENERIC_RGBAP | AVS_CS_SAMPLE_BITS_8,  // Planar RGBA 8 bit samples
206   AVS_CS_RGBAP10 = AVS_CS_GENERIC_RGBAP | AVS_CS_SAMPLE_BITS_10, // Planar RGBA 10bit samples
207   AVS_CS_RGBAP12 = AVS_CS_GENERIC_RGBAP | AVS_CS_SAMPLE_BITS_12, // Planar RGBA 12bit samples
208   AVS_CS_RGBAP14 = AVS_CS_GENERIC_RGBAP | AVS_CS_SAMPLE_BITS_14, // Planar RGBA 14bit samples
209   AVS_CS_RGBAP16 = AVS_CS_GENERIC_RGBAP | AVS_CS_SAMPLE_BITS_16, // Planar RGBA 16bit samples
210   AVS_CS_RGBAPS  = AVS_CS_GENERIC_RGBAP | AVS_CS_SAMPLE_BITS_32, // Planar RGBA 32bit samples
211 
212   // Planar YUVA
213   AVS_CS_YUVA444    = AVS_CS_GENERIC_YUVA444 | AVS_CS_SAMPLE_BITS_8,  // YUVA 4:4:4 8bit samples
214   AVS_CS_YUVA422    = AVS_CS_GENERIC_YUVA422 | AVS_CS_SAMPLE_BITS_8,  // YUVA 4:2:2 8bit samples
215   AVS_CS_YUVA420    = AVS_CS_GENERIC_YUVA420 | AVS_CS_SAMPLE_BITS_8,  // YUVA 4:2:0 8bit samples
216 
217   AVS_CS_YUVA444P10 = AVS_CS_GENERIC_YUVA444 | AVS_CS_SAMPLE_BITS_10, // YUVA 4:4:4 10bit samples
218   AVS_CS_YUVA422P10 = AVS_CS_GENERIC_YUVA422 | AVS_CS_SAMPLE_BITS_10, // YUVA 4:2:2 10bit samples
219   AVS_CS_YUVA420P10 = AVS_CS_GENERIC_YUVA420 | AVS_CS_SAMPLE_BITS_10, // YUVA 4:2:0 10bit samples
220 
221   AVS_CS_YUVA444P12 = AVS_CS_GENERIC_YUVA444 | AVS_CS_SAMPLE_BITS_12, // YUVA 4:4:4 12bit samples
222   AVS_CS_YUVA422P12 = AVS_CS_GENERIC_YUVA422 | AVS_CS_SAMPLE_BITS_12, // YUVA 4:2:2 12bit samples
223   AVS_CS_YUVA420P12 = AVS_CS_GENERIC_YUVA420 | AVS_CS_SAMPLE_BITS_12, // YUVA 4:2:0 12bit samples
224 
225   AVS_CS_YUVA444P14 = AVS_CS_GENERIC_YUVA444 | AVS_CS_SAMPLE_BITS_14, // YUVA 4:4:4 14bit samples
226   AVS_CS_YUVA422P14 = AVS_CS_GENERIC_YUVA422 | AVS_CS_SAMPLE_BITS_14, // YUVA 4:2:2 14bit samples
227   AVS_CS_YUVA420P14 = AVS_CS_GENERIC_YUVA420 | AVS_CS_SAMPLE_BITS_14, // YUVA 4:2:0 14bit samples
228 
229   AVS_CS_YUVA444P16 = AVS_CS_GENERIC_YUVA444 | AVS_CS_SAMPLE_BITS_16, // YUVA 4:4:4 16bit samples
230   AVS_CS_YUVA422P16 = AVS_CS_GENERIC_YUVA422 | AVS_CS_SAMPLE_BITS_16, // YUVA 4:2:2 16bit samples
231   AVS_CS_YUVA420P16 = AVS_CS_GENERIC_YUVA420 | AVS_CS_SAMPLE_BITS_16, // YUVA 4:2:0 16bit samples
232 
233   AVS_CS_YUVA444PS  = AVS_CS_GENERIC_YUVA444 | AVS_CS_SAMPLE_BITS_32,  // YUVA 4:4:4 32bit samples
234   AVS_CS_YUVA422PS  = AVS_CS_GENERIC_YUVA422 | AVS_CS_SAMPLE_BITS_32,  // YUVA 4:2:2 32bit samples
235   AVS_CS_YUVA420PS  = AVS_CS_GENERIC_YUVA420 | AVS_CS_SAMPLE_BITS_32,  // YUVA 4:2:0 32bit samples
236 
237 };
238 
239 enum {
240   AVS_IT_BFF = 1<<0,
241   AVS_IT_TFF = 1<<1,
242   AVS_IT_FIELDBASED = 1<<2};
243 
244 enum {
245   AVS_FILTER_TYPE=1,
246   AVS_FILTER_INPUT_COLORSPACE=2,
247   AVS_FILTER_OUTPUT_TYPE=9,
248   AVS_FILTER_NAME=4,
249   AVS_FILTER_AUTHOR=5,
250   AVS_FILTER_VERSION=6,
251   AVS_FILTER_ARGS=7,
252   AVS_FILTER_ARGS_INFO=8,
253   AVS_FILTER_ARGS_DESCRIPTION=10,
254   AVS_FILTER_DESCRIPTION=11};
255 
256 enum {  //SUBTYPES
257   AVS_FILTER_TYPE_AUDIO=1,
258   AVS_FILTER_TYPE_VIDEO=2,
259   AVS_FILTER_OUTPUT_TYPE_SAME=3,
260   AVS_FILTER_OUTPUT_TYPE_DIFFERENT=4};
261 
262 enum {
263   // New 2.6 explicitly defined cache hints.
264   AVS_CACHE_NOTHING=10, // Do not cache video.
265   AVS_CACHE_WINDOW=11, // Hard protect up to X frames within a range of X from the current frame N.
266   AVS_CACHE_GENERIC=12, // LRU cache up to X frames.
267   AVS_CACHE_FORCE_GENERIC=13, // LRU cache up to X frames, override any previous CACHE_WINDOW.
268 
269   AVS_CACHE_GET_POLICY=30, // Get the current policy.
270   AVS_CACHE_GET_WINDOW=31, // Get the current window h_span.
271   AVS_CACHE_GET_RANGE=32, // Get the current generic frame range.
272 
273   AVS_CACHE_AUDIO=50, // Explicitly do cache audio, X byte cache.
274   AVS_CACHE_AUDIO_NOTHING=51, // Explicitly do not cache audio.
275   AVS_CACHE_AUDIO_NONE=52, // Audio cache off (auto mode), X byte initial cache.
276   AVS_CACHE_AUDIO_AUTO=53, // Audio cache on (auto mode), X byte initial cache.
277 
278   AVS_CACHE_GET_AUDIO_POLICY=70, // Get the current audio policy.
279   AVS_CACHE_GET_AUDIO_SIZE=71, // Get the current audio cache size.
280 
281   AVS_CACHE_PREFETCH_FRAME=100, // Queue request to prefetch frame N.
282   AVS_CACHE_PREFETCH_GO=101, // Action video prefetches.
283 
284   AVS_CACHE_PREFETCH_AUDIO_BEGIN=120, // Begin queue request transaction to prefetch audio (take critical section).
285   AVS_CACHE_PREFETCH_AUDIO_STARTLO=121, // Set low 32 bits of start.
286   AVS_CACHE_PREFETCH_AUDIO_STARTHI=122, // Set high 32 bits of start.
287   AVS_CACHE_PREFETCH_AUDIO_COUNT=123, // Set low 32 bits of length.
288   AVS_CACHE_PREFETCH_AUDIO_COMMIT=124, // Enqueue request transaction to prefetch audio (release critical section).
289   AVS_CACHE_PREFETCH_AUDIO_GO=125, // Action audio prefetches.
290 
291   AVS_CACHE_GETCHILD_CACHE_MODE=200, // Cache ask Child for desired video cache mode.
292   AVS_CACHE_GETCHILD_CACHE_SIZE=201, // Cache ask Child for desired video cache size.
293   AVS_CACHE_GETCHILD_AUDIO_MODE=202, // Cache ask Child for desired audio cache mode.
294   AVS_CACHE_GETCHILD_AUDIO_SIZE=203, // Cache ask Child for desired audio cache size.
295 
296   AVS_CACHE_GETCHILD_COST=220, // Cache ask Child for estimated processing cost.
297     AVS_CACHE_COST_ZERO=221, // Child response of zero cost (ptr arithmetic only).
298     AVS_CACHE_COST_UNIT=222, // Child response of unit cost (less than or equal 1 full frame blit).
299     AVS_CACHE_COST_LOW=223, // Child response of light cost. (Fast)
300     AVS_CACHE_COST_MED=224, // Child response of medium cost. (Real time)
301     AVS_CACHE_COST_HI=225, // Child response of heavy cost. (Slow)
302 
303   AVS_CACHE_GETCHILD_THREAD_MODE=240, // Cache ask Child for thread safety.
304     AVS_CACHE_THREAD_UNSAFE=241, // Only 1 thread allowed for all instances. 2.5 filters default!
305     AVS_CACHE_THREAD_CLASS=242, // Only 1 thread allowed for each instance. 2.6 filters default!
306     AVS_CACHE_THREAD_SAFE=243, //  Allow all threads in any instance.
307     AVS_CACHE_THREAD_OWN=244, // Safe but limit to 1 thread, internally threaded.
308 
309   AVS_CACHE_GETCHILD_ACCESS_COST=260, // Cache ask Child for preferred access pattern.
310     AVS_CACHE_ACCESS_RAND=261, // Filter is access order agnostic.
311     AVS_CACHE_ACCESS_SEQ0=262, // Filter prefers sequential access (low cost)
312     AVS_CACHE_ACCESS_SEQ1=263, // Filter needs sequential access (high cost)
313   };
314 
315 #ifdef BUILDING_AVSCORE
316 AVSValue create_c_video_filter(AVSValue args, void * user_data, IScriptEnvironment * e0);
317 
318 struct AVS_ScriptEnvironment {
319         IScriptEnvironment * env;
320         const char * error;
321         AVS_ScriptEnvironment(IScriptEnvironment * e = 0)
envAVS_ScriptEnvironment322                 : env(e), error(0) {}
323 };
324 #endif
325 
326 typedef struct AVS_Clip AVS_Clip;
327 typedef struct AVS_ScriptEnvironment AVS_ScriptEnvironment;
328 
329 /////////////////////////////////////////////////////////////////////
330 //
331 // AVS_VideoInfo
332 //
333 
334 // AVS_VideoInfo is laid out identically to VideoInfo
335 typedef struct AVS_VideoInfo {
336   int width, height;    // width=0 means no video
337   unsigned fps_numerator, fps_denominator;
338   int num_frames;
339 
340   int pixel_type;
341 
342   int audio_samples_per_second;   // 0 means no audio
343   int sample_type;
344   INT64 num_audio_samples;
345   int nchannels;
346 
347   // Image type properties
348 
349   int image_type;
350 } AVS_VideoInfo;
351 
352 // useful functions of the above
avs_has_video(const AVS_VideoInfo * p)353 AVSC_INLINE int avs_has_video(const AVS_VideoInfo * p)
354         { return (p->width!=0); }
355 
avs_has_audio(const AVS_VideoInfo * p)356 AVSC_INLINE int avs_has_audio(const AVS_VideoInfo * p)
357         { return (p->audio_samples_per_second!=0); }
358 
avs_is_rgb(const AVS_VideoInfo * p)359 AVSC_INLINE int avs_is_rgb(const AVS_VideoInfo * p)
360         { return !!(p->pixel_type&AVS_CS_BGR); }
361 
avs_is_rgb24(const AVS_VideoInfo * p)362 AVSC_INLINE int avs_is_rgb24(const AVS_VideoInfo * p)
363         { return ((p->pixel_type&AVS_CS_BGR24)==AVS_CS_BGR24) && ((p->pixel_type & AVS_CS_SAMPLE_BITS_MASK) == AVS_CS_SAMPLE_BITS_8); }
364 
avs_is_rgb32(const AVS_VideoInfo * p)365 AVSC_INLINE int avs_is_rgb32(const AVS_VideoInfo * p)
366        { return ((p->pixel_type&AVS_CS_BGR32)==AVS_CS_BGR32) && ((p->pixel_type & AVS_CS_SAMPLE_BITS_MASK) == AVS_CS_SAMPLE_BITS_8); }
367 
avs_is_yuv(const AVS_VideoInfo * p)368 AVSC_INLINE int avs_is_yuv(const AVS_VideoInfo * p)
369         { return !!(p->pixel_type&AVS_CS_YUV ); }
370 
avs_is_yuy2(const AVS_VideoInfo * p)371 AVSC_INLINE int avs_is_yuy2(const AVS_VideoInfo * p)
372         { return (p->pixel_type & AVS_CS_YUY2) == AVS_CS_YUY2; }
373 
374 AVSC_API(int, avs_is_yv24)(const AVS_VideoInfo * p); // avs+: for generic 444 check, use avs_is_yuv444
375 
376 AVSC_API(int, avs_is_yv16)(const AVS_VideoInfo * p); // avs+: for generic 422 check, use avs_is_yuv422
377 
378 AVSC_API(int, avs_is_yv12)(const AVS_VideoInfo * p) ; // avs+: for generic 420 check, use avs_is_yuv420
379 
380 AVSC_API(int, avs_is_yv411)(const AVS_VideoInfo * p);
381 
382 AVSC_API(int, avs_is_y8)(const AVS_VideoInfo * p); // avs+: for generic grayscale, use avs_is_y
383 
384 AVSC_API(int, avs_get_plane_width_subsampling)(const AVS_VideoInfo * p, int plane);
385 
386 AVSC_API(int, avs_get_plane_height_subsampling)(const AVS_VideoInfo * p, int plane);
387 
388 AVSC_API(int, avs_bits_per_pixel)(const AVS_VideoInfo * p);
389 
390 AVSC_API(int, avs_bytes_from_pixels)(const AVS_VideoInfo * p, int pixels);
391 
392 AVSC_API(int, avs_row_size)(const AVS_VideoInfo * p, int plane);
393 
394 AVSC_API(int, avs_bmp_size)(const AVS_VideoInfo * vi);
395 
396 AVSC_API(int, avs_is_color_space)(const AVS_VideoInfo * p, int c_space);
397 
398 // no API for these, inline helper functions
avs_is_property(const AVS_VideoInfo * p,int property)399 AVSC_INLINE int avs_is_property(const AVS_VideoInfo * p, int property)
400 {
401   return ((p->image_type & property) == property);
402 }
403 
avs_is_planar(const AVS_VideoInfo * p)404 AVSC_INLINE int avs_is_planar(const AVS_VideoInfo * p)
405 {
406   return !!(p->pixel_type & AVS_CS_PLANAR);
407 }
408 
avs_is_field_based(const AVS_VideoInfo * p)409 AVSC_INLINE int avs_is_field_based(const AVS_VideoInfo * p)
410 {
411   return !!(p->image_type & AVS_IT_FIELDBASED);
412 }
413 
avs_is_parity_known(const AVS_VideoInfo * p)414 AVSC_INLINE int avs_is_parity_known(const AVS_VideoInfo * p)
415 {
416   return ((p->image_type & AVS_IT_FIELDBASED) && (p->image_type & (AVS_IT_BFF | AVS_IT_TFF)));
417 }
418 
avs_is_bff(const AVS_VideoInfo * p)419 AVSC_INLINE int avs_is_bff(const AVS_VideoInfo * p)
420 {
421   return !!(p->image_type & AVS_IT_BFF);
422 }
423 
avs_is_tff(const AVS_VideoInfo * p)424 AVSC_INLINE int avs_is_tff(const AVS_VideoInfo * p)
425 {
426   return !!(p->image_type & AVS_IT_TFF);
427 }
428 
avs_samples_per_second(const AVS_VideoInfo * p)429 AVSC_INLINE int avs_samples_per_second(const AVS_VideoInfo * p)
430         { return p->audio_samples_per_second; }
431 
avs_bytes_per_channel_sample(const AVS_VideoInfo * p)432 AVSC_INLINE int avs_bytes_per_channel_sample(const AVS_VideoInfo * p)
433 {
434     switch (p->sample_type) {
435       case AVS_SAMPLE_INT8:  return sizeof(signed char);
436       case AVS_SAMPLE_INT16: return sizeof(signed short);
437       case AVS_SAMPLE_INT24: return 3;
438       case AVS_SAMPLE_INT32: return sizeof(signed int);
439       case AVS_SAMPLE_FLOAT: return sizeof(float);
440       default: return 0;
441     }
442 }
443 
avs_bytes_per_audio_sample(const AVS_VideoInfo * p)444 AVSC_INLINE int avs_bytes_per_audio_sample(const AVS_VideoInfo * p)
445         { return p->nchannels*avs_bytes_per_channel_sample(p);}
446 
avs_audio_samples_from_frames(const AVS_VideoInfo * p,INT64 frames)447 AVSC_INLINE INT64 avs_audio_samples_from_frames(const AVS_VideoInfo * p, INT64 frames)
448         { return ((INT64)(frames) * p->audio_samples_per_second * p->fps_denominator / p->fps_numerator); }
449 
avs_frames_from_audio_samples(const AVS_VideoInfo * p,INT64 samples)450 AVSC_INLINE int avs_frames_from_audio_samples(const AVS_VideoInfo * p, INT64 samples)
451         { return (int)(samples * (INT64)p->fps_numerator / (INT64)p->fps_denominator / (INT64)p->audio_samples_per_second); }
452 
avs_audio_samples_from_bytes(const AVS_VideoInfo * p,INT64 bytes)453 AVSC_INLINE INT64 avs_audio_samples_from_bytes(const AVS_VideoInfo * p, INT64 bytes)
454         { return bytes / avs_bytes_per_audio_sample(p); }
455 
avs_bytes_from_audio_samples(const AVS_VideoInfo * p,INT64 samples)456 AVSC_INLINE INT64 avs_bytes_from_audio_samples(const AVS_VideoInfo * p, INT64 samples)
457         { return samples * avs_bytes_per_audio_sample(p); }
458 
avs_audio_channels(const AVS_VideoInfo * p)459 AVSC_INLINE int avs_audio_channels(const AVS_VideoInfo * p)
460         { return p->nchannels; }
461 
avs_sample_type(const AVS_VideoInfo * p)462 AVSC_INLINE int avs_sample_type(const AVS_VideoInfo * p)
463         { return p->sample_type;}
464 
465 // useful mutator
avs_set_property(AVS_VideoInfo * p,int property)466 AVSC_INLINE void avs_set_property(AVS_VideoInfo * p, int property)
467         { p->image_type|=property; }
468 
avs_clear_property(AVS_VideoInfo * p,int property)469 AVSC_INLINE void avs_clear_property(AVS_VideoInfo * p, int property)
470         { p->image_type&=~property; }
471 
avs_set_field_based(AVS_VideoInfo * p,int isfieldbased)472 AVSC_INLINE void avs_set_field_based(AVS_VideoInfo * p, int isfieldbased)
473         { if (isfieldbased) p->image_type|=AVS_IT_FIELDBASED; else p->image_type&=~AVS_IT_FIELDBASED; }
474 
avs_set_fps(AVS_VideoInfo * p,unsigned numerator,unsigned denominator)475 AVSC_INLINE void avs_set_fps(AVS_VideoInfo * p, unsigned numerator, unsigned denominator)
476 {
477     unsigned x=numerator, y=denominator;
478     while (y) {   // find gcd
479       unsigned t = x%y; x = y; y = t;
480     }
481     p->fps_numerator = numerator/x;
482     p->fps_denominator = denominator/x;
483 }
484 
485 #ifndef AVSC_NO_DECLSPEC
486 // this inline function is calling an API function
avs_is_same_colorspace(const AVS_VideoInfo * x,const AVS_VideoInfo * y)487 AVSC_INLINE int avs_is_same_colorspace(const AVS_VideoInfo * x, const AVS_VideoInfo * y)
488 {
489         return (x->pixel_type == y->pixel_type)
490                 || (avs_is_yv12(x) && avs_is_yv12(y));
491 }
492 #endif
493 
494 // Avisynth+ extensions
495 AVSC_API(int, avs_is_rgb48)(const AVS_VideoInfo * p);
496 
497 AVSC_API(int, avs_is_rgb64)(const AVS_VideoInfo * p);
498 
499 AVSC_API(int, avs_is_yuv444p16)(const AVS_VideoInfo * p); // obsolete, use avs_is_yuv444
500 
501 AVSC_API(int, avs_is_yuv422p16)(const AVS_VideoInfo * p); // obsolete, use avs_is_yuv422
502 
503 AVSC_API(int, avs_is_yuv420p16)(const AVS_VideoInfo * p); // obsolete, use avs_is_yuv420
504 
505 AVSC_API(int, avs_is_y16)(const AVS_VideoInfo * p); // obsolete, use avs_is_y
506 
507 AVSC_API(int, avs_is_yuv444ps)(const AVS_VideoInfo * p); // obsolete, use avs_is_yuv444
508 
509 AVSC_API(int, avs_is_yuv422ps)(const AVS_VideoInfo * p); // obsolete, use avs_is_yuv422
510 
511 AVSC_API(int, avs_is_yuv420ps)(const AVS_VideoInfo * p); // obsolete, use avs_is_yuv420
512 
513 AVSC_API(int, avs_is_y32)(const AVS_VideoInfo * p); // obsolete, use avs_is_y
514 
515 AVSC_API(int, avs_is_444)(const AVS_VideoInfo * p);
516 
517 AVSC_API(int, avs_is_422)(const AVS_VideoInfo * p);
518 
519 AVSC_API(int, avs_is_420)(const AVS_VideoInfo * p);
520 
521 AVSC_API(int, avs_is_y)(const AVS_VideoInfo * p);
522 
523 AVSC_API(int, avs_is_yuva)(const AVS_VideoInfo * p);
524 
525 AVSC_API(int, avs_is_planar_rgb)(const AVS_VideoInfo * p);
526 
527 AVSC_API(int, avs_is_planar_rgba)(const AVS_VideoInfo * p);
528 
529 AVSC_API(int, avs_num_components)(const AVS_VideoInfo * p);
530 
531 AVSC_API(int, avs_component_size)(const AVS_VideoInfo * p);
532 
533 AVSC_API(int, avs_bits_per_component)(const AVS_VideoInfo * p);
534 // end of Avisynth+ specific
535 
536 /////////////////////////////////////////////////////////////////////
537 //
538 // AVS_VideoFrame
539 //
540 
541 // VideoFrameBuffer holds information about a memory block which is used
542 // for video data.  For efficiency, instances of this class are not deleted
543 // when the refcount reaches zero; instead they're stored in a linked list
544 // to be reused.  The instances are deleted when the corresponding AVS
545 // file is closed.
546 
547 // AVS_VideoFrameBuffer is laid out identically to VideoFrameBuffer
548 // DO NOT USE THIS STRUCTURE DIRECTLY
549 typedef struct AVS_VideoFrameBuffer {
550   BYTE * data;
551 #ifdef SIZETMOD
552   size_t data_size;
553 #else
554   int data_size;
555 #endif
556   // sequence_number is incremented every time the buffer is changed, so
557   // that stale views can tell they're no longer valid.
558   volatile long sequence_number;
559 
560   volatile long refcount;
561 } AVS_VideoFrameBuffer;
562 
563 // VideoFrame holds a "window" into a VideoFrameBuffer.
564 
565 // AVS_VideoFrame is laid out identically to IVideoFrame
566 // DO NOT USE THIS STRUCTURE DIRECTLY
567 typedef struct AVS_VideoFrame {
568   volatile long refcount;
569   AVS_VideoFrameBuffer * vfb;
570 #ifdef SIZETMOD
571   size_t offset;
572 #else
573   int offset;
574 #endif
575   int pitch, row_size, height;
576 #ifdef SIZETMOD
577   size_t offsetU, offsetV;
578 #else
579   int offsetU, offsetV;
580 #endif
581   int pitchUV;  // U&V offsets are from top of picture.
582   int row_sizeUV, heightUV; // for Planar RGB offsetU, offsetV is for the 2nd and 3rd Plane.
583                             // for Planar RGB pitchUV and row_sizeUV = 0, because when no VideoInfo (MakeWriteable)
584                             // the decision on existence of UV is checked by zero pitch
585   // AVS+ extension, avisynth.h: class does not break plugins if appended here
586 #ifdef SIZETMOD
587   size_t offsetA;
588 #else
589   int offsetA;
590 #endif
591   int pitchA, row_sizeA; // 4th alpha plane support, pitch and row_size is 0 is none
592 } AVS_VideoFrame;
593 
594 // Access functions for AVS_VideoFrame
595 AVSC_API(int, avs_get_pitch_p)(const AVS_VideoFrame * p, int plane);
596 
597 AVSC_API(int, avs_get_row_size_p)(const AVS_VideoFrame * p, int plane);
598 
599 AVSC_API(int, avs_get_height_p)(const AVS_VideoFrame * p, int plane);
600 
601 AVSC_API(const BYTE *, avs_get_read_ptr_p)(const AVS_VideoFrame * p, int plane);
602 
603 AVSC_API(int, avs_is_writable)(const AVS_VideoFrame * p);
604 
605 AVSC_API(BYTE *, avs_get_write_ptr_p)(const AVS_VideoFrame * p, int plane);
606 
607 AVSC_API(void, avs_release_video_frame)(AVS_VideoFrame *);
608 // makes a shallow copy of a video frame
609 AVSC_API(AVS_VideoFrame *, avs_copy_video_frame)(AVS_VideoFrame *);
610 
611 // no API for these, inline helper functions
612 #ifndef AVSC_NO_DECLSPEC
613 // this inline function is calling an API function
avs_get_pitch(const AVS_VideoFrame * p)614 AVSC_INLINE int avs_get_pitch(const AVS_VideoFrame * p) {
615   return avs_get_pitch_p(p, 0);
616 }
617 #endif
618 
619 #ifndef AVSC_NO_DECLSPEC
620 // this inline function is calling an API function
avs_get_row_size(const AVS_VideoFrame * p)621 AVSC_INLINE int avs_get_row_size(const AVS_VideoFrame * p) {
622         return avs_get_row_size_p(p, 0); }
623 #endif
624 
625 
626 #ifndef AVSC_NO_DECLSPEC
627 // this inline function is calling an API function
avs_get_height(const AVS_VideoFrame * p)628 AVSC_INLINE int avs_get_height(const AVS_VideoFrame * p) {
629   return avs_get_height_p(p, 0);
630 }
631 #endif
632 
633 #ifndef AVSC_NO_DECLSPEC
634 // this inline function is calling an API function
avs_get_read_ptr(const AVS_VideoFrame * p)635 AVSC_INLINE const BYTE* avs_get_read_ptr(const AVS_VideoFrame * p) {
636         return avs_get_read_ptr_p(p, 0);}
637 #endif
638 
639 #ifndef AVSC_NO_DECLSPEC
640 // this inline function is calling an API function
avs_get_write_ptr(const AVS_VideoFrame * p)641 AVSC_INLINE BYTE* avs_get_write_ptr(const AVS_VideoFrame * p) {
642         return avs_get_write_ptr_p(p, 0);}
643 #endif
644 
645 #ifndef AVSC_NO_DECLSPEC
646 // this inline function is calling an API function
avs_release_frame(AVS_VideoFrame * f)647 AVSC_INLINE void avs_release_frame(AVS_VideoFrame * f)
648   {avs_release_video_frame(f);}
649 #endif
650 
651 #ifndef AVSC_NO_DECLSPEC
652 // this inline function is calling an API function
avs_copy_frame(AVS_VideoFrame * f)653 AVSC_INLINE AVS_VideoFrame * avs_copy_frame(AVS_VideoFrame * f)
654   {return avs_copy_video_frame(f);}
655 #endif
656 
657 /////////////////////////////////////////////////////////////////////
658 //
659 // AVS_Value
660 //
661 
662 // Treat AVS_Value as a fat pointer.  That is use avs_copy_value
663 // and avs_release_value appropriately as you would if AVS_Value was
664 // a pointer.
665 
666 // To maintain source code compatibility with future versions of the
667 // avisynth_c API don't use the AVS_Value directly.  Use the helper
668 // functions below.
669 
670 // AVS_Value is laid out identically to AVSValue
671 typedef struct AVS_Value AVS_Value;
672 struct AVS_Value {
673   short type;  // 'a'rray, 'c'lip, 'b'ool, 'i'nt, 'f'loat, 's'tring, 'v'oid, or 'l'ong
674                // for some function e'rror
675   short array_size;
676   union {
677     void * clip; // do not use directly, use avs_take_clip
678     char boolean;
679     int integer;
680     float floating_pt;
681     const char * string;
682     const AVS_Value * array;
683   } d;
684 };
685 
686 // AVS_Value should be initialized with avs_void.
687 // Should also set to avs_void after the value is released
688 // with avs_copy_value.  Consider it the equivalent of setting
689 // a pointer to NULL
690 static const AVS_Value avs_void = {'v'};
691 
692 AVSC_API(void, avs_copy_value)(AVS_Value * dest, AVS_Value src);
693 AVSC_API(void, avs_release_value)(AVS_Value);
694 AVSC_API(AVS_Clip *, avs_take_clip)(AVS_Value, AVS_ScriptEnvironment *);
695 AVSC_API(void, avs_set_to_clip)(AVS_Value *, AVS_Clip *);
696 
697 
698 // no API for these, inline helper functions
avs_defined(AVS_Value v)699 AVSC_INLINE int avs_defined(AVS_Value v) { return v.type != 'v'; }
avs_is_clip(AVS_Value v)700 AVSC_INLINE int avs_is_clip(AVS_Value v) { return v.type == 'c'; }
avs_is_bool(AVS_Value v)701 AVSC_INLINE int avs_is_bool(AVS_Value v) { return v.type == 'b'; }
avs_is_int(AVS_Value v)702 AVSC_INLINE int avs_is_int(AVS_Value v) { return v.type == 'i'; }
avs_is_float(AVS_Value v)703 AVSC_INLINE int avs_is_float(AVS_Value v) { return v.type == 'f' || v.type == 'i'; }
avs_is_string(AVS_Value v)704 AVSC_INLINE int avs_is_string(AVS_Value v) { return v.type == 's'; }
avs_is_array(AVS_Value v)705 AVSC_INLINE int avs_is_array(AVS_Value v) { return v.type == 'a'; }
avs_is_error(AVS_Value v)706 AVSC_INLINE int avs_is_error(AVS_Value v) { return v.type == 'e'; }
707 
avs_as_bool(AVS_Value v)708 AVSC_INLINE int avs_as_bool(AVS_Value v)
709         { return v.d.boolean; }
avs_as_int(AVS_Value v)710 AVSC_INLINE int avs_as_int(AVS_Value v)
711         { return v.d.integer; }
avs_as_string(AVS_Value v)712 AVSC_INLINE const char * avs_as_string(AVS_Value v)
713         { return avs_is_error(v) || avs_is_string(v) ? v.d.string : 0; }
avs_as_float(AVS_Value v)714 AVSC_INLINE double avs_as_float(AVS_Value v)
715         { return avs_is_int(v) ? v.d.integer : v.d.floating_pt; }
avs_as_error(AVS_Value v)716 AVSC_INLINE const char * avs_as_error(AVS_Value v)
717         { return avs_is_error(v) ? v.d.string : 0; }
avs_as_array(AVS_Value v)718 AVSC_INLINE const AVS_Value * avs_as_array(AVS_Value v)
719         { return v.d.array; }
avs_array_size(AVS_Value v)720 AVSC_INLINE int avs_array_size(AVS_Value v)
721         { return avs_is_array(v) ? v.array_size : 1; }
avs_array_elt(AVS_Value v,int index)722 AVSC_INLINE AVS_Value avs_array_elt(AVS_Value v, int index)
723         { return avs_is_array(v) ? v.d.array[index] : v; }
724 
725 // only use these functions on an AVS_Value that does not already have
726 // an active value.  Remember, treat AVS_Value as a fat pointer.
avs_new_value_bool(int v0)727 AVSC_INLINE AVS_Value avs_new_value_bool(int v0)
728         { AVS_Value v; v.type = 'b'; v.d.boolean = v0 == 0 ? 0 : 1; return v; }
avs_new_value_int(int v0)729 AVSC_INLINE AVS_Value avs_new_value_int(int v0)
730         { AVS_Value v; v.type = 'i'; v.d.integer = v0; return v; }
avs_new_value_string(const char * v0)731 AVSC_INLINE AVS_Value avs_new_value_string(const char * v0)
732         { AVS_Value v; v.type = 's'; v.d.string = v0; return v; }
avs_new_value_float(float v0)733 AVSC_INLINE AVS_Value avs_new_value_float(float v0)
734         { AVS_Value v; v.type = 'f'; v.d.floating_pt = v0; return v;}
avs_new_value_error(const char * v0)735 AVSC_INLINE AVS_Value avs_new_value_error(const char * v0)
736         { AVS_Value v; v.type = 'e'; v.d.string = v0; return v; }
737 #ifndef AVSC_NO_DECLSPEC
738 // this inline function is calling an API function
avs_new_value_clip(AVS_Clip * v0)739 AVSC_INLINE AVS_Value avs_new_value_clip(AVS_Clip * v0)
740         { AVS_Value v; avs_set_to_clip(&v, v0); return v; }
741 #endif
avs_new_value_array(AVS_Value * v0,int size)742 AVSC_INLINE AVS_Value avs_new_value_array(AVS_Value * v0, int size)
743         { AVS_Value v; v.type = 'a'; v.d.array = v0; v.array_size = (short)size; return v; }
744 // end of inline helper functions
745 
746 /////////////////////////////////////////////////////////////////////
747 //
748 // AVS_Clip
749 //
750 
751 AVSC_API(void, avs_release_clip)(AVS_Clip *);
752 AVSC_API(AVS_Clip *, avs_copy_clip)(AVS_Clip *);
753 
754 AVSC_API(const char *, avs_clip_get_error)(AVS_Clip *); // return 0 if no error
755 
756 AVSC_API(const AVS_VideoInfo *, avs_get_video_info)(AVS_Clip *);
757 
758 AVSC_API(int, avs_get_version)(AVS_Clip *);
759 
760 AVSC_API(AVS_VideoFrame *, avs_get_frame)(AVS_Clip *, int n);
761 // The returned video frame must be released with avs_release_video_frame
762 
763 AVSC_API(int, avs_get_parity)(AVS_Clip *, int n);
764 // return field parity if field_based, else parity of first field in frame
765 
766 AVSC_API(int, avs_get_audio)(AVS_Clip *, void * buf,
767                              INT64 start, INT64 count);
768 // start and count are in samples
769 
770 AVSC_API(int, avs_set_cache_hints)(AVS_Clip *,
771                                    int cachehints, int frame_range);
772 
773 // This is the callback type used by avs_add_function
774 typedef AVS_Value (AVSC_CC * AVS_ApplyFunc)
775                         (AVS_ScriptEnvironment *, AVS_Value args, void * user_data);
776 
777 typedef struct AVS_FilterInfo AVS_FilterInfo;
778 struct AVS_FilterInfo
779 {
780   // these members should not be modified outside of the AVS_ApplyFunc callback
781   AVS_Clip * child;
782   AVS_VideoInfo vi;
783   AVS_ScriptEnvironment * env;
784   AVS_VideoFrame * (AVSC_CC * get_frame)(AVS_FilterInfo *, int n);
785   int (AVSC_CC * get_parity)(AVS_FilterInfo *, int n);
786   int (AVSC_CC * get_audio)(AVS_FilterInfo *, void * buf,
787                                   INT64 start, INT64 count);
788   int (AVSC_CC * set_cache_hints)(AVS_FilterInfo *, int cachehints,
789                                         int frame_range);
790   void (AVSC_CC * free_filter)(AVS_FilterInfo *);
791 
792   // Should be set when ever there is an error to report.
793   // It is cleared before any of the above methods are called
794   const char * error;
795   // this is to store whatever and may be modified at will
796   void * user_data;
797 };
798 
799 // Create a new filter
800 // fi is set to point to the AVS_FilterInfo so that you can
801 //   modify it once it is initialized.
802 // store_child should generally be set to true.  If it is not
803 //    set than ALL methods (the function pointers) must be defined
804 // If it is set than you do not need to worry about freeing the child
805 //    clip.
806 AVSC_API(AVS_Clip *, avs_new_c_filter)(AVS_ScriptEnvironment * e,
807                                        AVS_FilterInfo * * fi,
808                                        AVS_Value child, int store_child);
809 
810 /////////////////////////////////////////////////////////////////////
811 //
812 // AVS_ScriptEnvironment
813 //
814 
815 // For GetCPUFlags.  These are backwards-compatible with those in VirtualDub.
816 enum {
817                                 /* slowest CPU to support extension */
818   AVS_CPU_FORCE        = 0x01,   // N/A
819   AVS_CPU_FPU          = 0x02,   // 386/486DX
820   AVS_CPU_MMX          = 0x04,   // P55C, K6, PII
821   AVS_CPU_INTEGER_SSE  = 0x08,   // PIII, Athlon
822   AVS_CPU_SSE          = 0x10,   // PIII, Athlon XP/MP
823   AVS_CPU_SSE2         = 0x20,   // PIV, Hammer
824   AVS_CPU_3DNOW        = 0x40,   // K6-2
825   AVS_CPU_3DNOW_EXT    = 0x80,   // Athlon
826   AVS_CPU_X86_64       = 0xA0,   // Hammer (note: equiv. to 3DNow + SSE2,
827                                  // which only Hammer will have anyway)
828   AVS_CPUF_SSE3       = 0x100,   //  PIV+, K8 Venice
829   AVS_CPUF_SSSE3      = 0x200,   //  Core 2
830   AVS_CPUF_SSE4       = 0x400,   //  Penryn, Wolfdale, Yorkfield
831   AVS_CPUF_SSE4_1     = 0x400,
832   AVS_CPUF_AVX        = 0x800,   //  Sandy Bridge, Bulldozer
833   AVS_CPUF_SSE4_2    = 0x1000,   //  Nehalem
834   // AVS+
835   AVS_CPUF_AVX2      = 0x2000,   //  Haswell
836   AVS_CPUF_FMA3      = 0x4000,
837   AVS_CPUF_F16C      = 0x8000,
838   AVS_CPUF_MOVBE     = 0x10000,   // Big Endian Move
839   AVS_CPUF_POPCNT    = 0x20000,
840   AVS_CPUF_AES       = 0x40000,
841   AVS_CPUF_FMA4      = 0x80000,
842 
843   AVS_CPUF_AVX512F    = 0x100000,  // AVX-512 Foundation.
844   AVS_CPUF_AVX512DQ   = 0x200000,  // AVX-512 DQ (Double/Quad granular) Instructions
845   AVS_CPUF_AVX512PF   = 0x400000,  // AVX-512 Prefetch
846   AVS_CPUF_AVX512ER   = 0x800000,  // AVX-512 Exponential and Reciprocal
847   AVS_CPUF_AVX512CD   = 0x1000000, // AVX-512 Conflict Detection
848   AVS_CPUF_AVX512BW   = 0x2000000, // AVX-512 BW (Byte/Word granular) Instructions
849   AVS_CPUF_AVX512VL   = 0x4000000, // AVX-512 VL (128/256 Vector Length) Extensions
850   AVS_CPUF_AVX512IFMA = 0x8000000, // AVX-512 IFMA integer 52 bit
851   AVS_CPUF_AVX512VBMI = 0x10000000 // AVX-512 VBMI
852 };
853 
854 
855 AVSC_API(const char *, avs_get_error)(AVS_ScriptEnvironment *); // return 0 if no error
856 
857 AVSC_API(int, avs_get_cpu_flags)(AVS_ScriptEnvironment *);
858 AVSC_API(int, avs_check_version)(AVS_ScriptEnvironment *, int version);
859 
860 AVSC_API(char *, avs_save_string)(AVS_ScriptEnvironment *, const char* s, int length);
861 AVSC_API(char *, avs_sprintf)(AVS_ScriptEnvironment *, const char * fmt, ...);
862 
863 AVSC_API(char *, avs_vsprintf)(AVS_ScriptEnvironment *, const char * fmt, void* val);
864  // note: val is really a va_list; I hope everyone typedefs va_list to a pointer
865 
866 AVSC_API(int, avs_add_function)(AVS_ScriptEnvironment *,
867                                 const char * name, const char * params,
868                                 AVS_ApplyFunc apply, void * user_data);
869 
870 AVSC_API(int, avs_function_exists)(AVS_ScriptEnvironment *, const char * name);
871 
872 AVSC_API(AVS_Value, avs_invoke)(AVS_ScriptEnvironment *, const char * name,
873                                AVS_Value args, const char** arg_names);
874 // The returned value must be be released with avs_release_value
875 
876 AVSC_API(AVS_Value, avs_get_var)(AVS_ScriptEnvironment *, const char* name);
877 // The returned value must be be released with avs_release_value
878 
879 AVSC_API(int, avs_set_var)(AVS_ScriptEnvironment *, const char* name, AVS_Value val);
880 
881 AVSC_API(int, avs_set_global_var)(AVS_ScriptEnvironment *, const char* name, const AVS_Value val);
882 
883 //void avs_push_context(AVS_ScriptEnvironment *, int level=0);
884 //void avs_pop_context(AVS_ScriptEnvironment *);
885 
886 AVSC_API(AVS_VideoFrame *, avs_new_video_frame_a)(AVS_ScriptEnvironment *,
887                                           const AVS_VideoInfo * vi, int align);
888 // align should be at least 16 for classic Avisynth
889 // Avisynth+: any value, Avs+ ensures a minimum alignment if too small align is provided
890 
891 // no API for these, inline helper functions
892 #ifndef AVSC_NO_DECLSPEC
893 // this inline function is calling an API function
avs_new_video_frame(AVS_ScriptEnvironment * env,const AVS_VideoInfo * vi)894 AVSC_INLINE AVS_VideoFrame * avs_new_video_frame(AVS_ScriptEnvironment * env,
895                                      const AVS_VideoInfo * vi)
896   {return avs_new_video_frame_a(env,vi,AVS_FRAME_ALIGN);}
897 
898 // an older compatibility alias
899 // this inline function is calling an API function
avs_new_frame(AVS_ScriptEnvironment * env,const AVS_VideoInfo * vi)900 AVSC_INLINE AVS_VideoFrame * avs_new_frame(AVS_ScriptEnvironment * env,
901                                const AVS_VideoInfo * vi)
902   {return avs_new_video_frame_a(env,vi,AVS_FRAME_ALIGN);}
903 #endif
904 // end of inline helper functions
905 
906 AVSC_API(int, avs_make_writable)(AVS_ScriptEnvironment *, AVS_VideoFrame * * pvf);
907 
908 AVSC_API(void, avs_bit_blt)(AVS_ScriptEnvironment *, BYTE* dstp, int dst_pitch, const BYTE* srcp, int src_pitch, int row_size, int height);
909 
910 typedef void (AVSC_CC *AVS_ShutdownFunc)(void* user_data, AVS_ScriptEnvironment * env);
911 AVSC_API(void, avs_at_exit)(AVS_ScriptEnvironment *, AVS_ShutdownFunc function, void * user_data);
912 
913 AVSC_API(AVS_VideoFrame *, avs_subframe)(AVS_ScriptEnvironment *, AVS_VideoFrame * src, int rel_offset, int new_pitch, int new_row_size, int new_height);
914 // The returned video frame must be be released
915 
916 AVSC_API(int, avs_set_memory_max)(AVS_ScriptEnvironment *, int mem);
917 
918 AVSC_API(int, avs_set_working_dir)(AVS_ScriptEnvironment *, const char * newdir);
919 
920 // avisynth.dll exports this; it's a way to use it as a library, without
921 // writing an AVS script or without going through AVIFile.
922 AVSC_API(AVS_ScriptEnvironment *, avs_create_script_environment)(int version);
923 
924 // this symbol is the entry point for the plugin and must
925 // be defined
926 AVSC_EXPORT
927 const char * AVSC_CC avisynth_c_plugin_init(AVS_ScriptEnvironment* env);
928 
929 
930 AVSC_API(void, avs_delete_script_environment)(AVS_ScriptEnvironment *);
931 
932 
933 AVSC_API(AVS_VideoFrame *, avs_subframe_planar)(AVS_ScriptEnvironment *, AVS_VideoFrame * src, int rel_offset, int new_pitch, int new_row_size, int new_height, int rel_offsetU, int rel_offsetV, int new_pitchUV);
934 // The returned video frame must be be released
935 
936 #ifdef AVSC_NO_DECLSPEC
937 // This part uses LoadLibrary and related functions to dynamically load Avisynth instead of declspec(dllimport)
938 // When AVSC_NO_DECLSPEC is defined, you can use avs_load_library to populate API functions into a struct
939 // AVSC_INLINE functions which call onto an API functions should be treated specially (todo)
940 
941 /*
942   The following functions needs to have been declared, probably from windows.h
943 
944   void* malloc(size_t)
945   void free(void*);
946 
947   HMODULE LoadLibrary(const char*);
948   void* GetProcAddress(HMODULE, const char*);
949   FreeLibrary(HMODULE);
950 */
951 
952 
953 typedef struct AVS_Library AVS_Library;
954 
955 #define AVSC_DECLARE_FUNC(name) name##_func name
956 
957 // AVSC_DECLARE_FUNC helps keeping naming convention: type is xxxxx_func, function name is xxxxx
958 // e.g. "AVSC_DECLARE_FUNC(avs_add_function);"
959 // is a shortcut for "avs_add_function_func avs_add_function;"
960 
961 // Note: AVSC_INLINE functions which call into API,
962 // are guarded by #ifndef AVSC_NO_DECLSPEC
963 // They should call the appropriate library-> API entry
964 
965 struct AVS_Library {
966   HMODULE handle;
967 
968   AVSC_DECLARE_FUNC(avs_add_function);
969   AVSC_DECLARE_FUNC(avs_at_exit);
970   AVSC_DECLARE_FUNC(avs_bit_blt);
971   AVSC_DECLARE_FUNC(avs_check_version);
972   AVSC_DECLARE_FUNC(avs_clip_get_error);
973   AVSC_DECLARE_FUNC(avs_copy_clip);
974   AVSC_DECLARE_FUNC(avs_copy_value);
975   AVSC_DECLARE_FUNC(avs_copy_video_frame);
976   AVSC_DECLARE_FUNC(avs_create_script_environment);
977   AVSC_DECLARE_FUNC(avs_delete_script_environment);
978   AVSC_DECLARE_FUNC(avs_function_exists);
979   AVSC_DECLARE_FUNC(avs_get_audio);
980   AVSC_DECLARE_FUNC(avs_get_cpu_flags);
981   AVSC_DECLARE_FUNC(avs_get_frame);
982   AVSC_DECLARE_FUNC(avs_get_parity);
983   AVSC_DECLARE_FUNC(avs_get_var);
984   AVSC_DECLARE_FUNC(avs_get_version);
985   AVSC_DECLARE_FUNC(avs_get_video_info);
986   AVSC_DECLARE_FUNC(avs_invoke);
987   AVSC_DECLARE_FUNC(avs_make_writable);
988   AVSC_DECLARE_FUNC(avs_new_c_filter);
989   AVSC_DECLARE_FUNC(avs_new_video_frame_a);
990   AVSC_DECLARE_FUNC(avs_release_clip);
991   AVSC_DECLARE_FUNC(avs_release_value);
992   AVSC_DECLARE_FUNC(avs_release_video_frame);
993   AVSC_DECLARE_FUNC(avs_save_string);
994   AVSC_DECLARE_FUNC(avs_set_cache_hints);
995   AVSC_DECLARE_FUNC(avs_set_global_var);
996   AVSC_DECLARE_FUNC(avs_set_memory_max);
997   AVSC_DECLARE_FUNC(avs_set_to_clip);
998   AVSC_DECLARE_FUNC(avs_set_var);
999   AVSC_DECLARE_FUNC(avs_set_working_dir);
1000   AVSC_DECLARE_FUNC(avs_sprintf);
1001   AVSC_DECLARE_FUNC(avs_subframe);
1002   AVSC_DECLARE_FUNC(avs_subframe_planar);
1003   AVSC_DECLARE_FUNC(avs_take_clip);
1004   AVSC_DECLARE_FUNC(avs_vsprintf);
1005 
1006   AVSC_DECLARE_FUNC(avs_get_error);
1007   AVSC_DECLARE_FUNC(avs_is_yv24);
1008   AVSC_DECLARE_FUNC(avs_is_yv16);
1009   AVSC_DECLARE_FUNC(avs_is_yv12);
1010   AVSC_DECLARE_FUNC(avs_is_yv411);
1011   AVSC_DECLARE_FUNC(avs_is_y8);
1012   AVSC_DECLARE_FUNC(avs_is_color_space);
1013 
1014   AVSC_DECLARE_FUNC(avs_get_plane_width_subsampling);
1015   AVSC_DECLARE_FUNC(avs_get_plane_height_subsampling);
1016   AVSC_DECLARE_FUNC(avs_bits_per_pixel);
1017   AVSC_DECLARE_FUNC(avs_bytes_from_pixels);
1018   AVSC_DECLARE_FUNC(avs_row_size);
1019   AVSC_DECLARE_FUNC(avs_bmp_size);
1020   AVSC_DECLARE_FUNC(avs_get_pitch_p);
1021   AVSC_DECLARE_FUNC(avs_get_row_size_p);
1022   AVSC_DECLARE_FUNC(avs_get_height_p);
1023   AVSC_DECLARE_FUNC(avs_get_read_ptr_p);
1024   AVSC_DECLARE_FUNC(avs_is_writable);
1025   AVSC_DECLARE_FUNC(avs_get_write_ptr_p);
1026 
1027   // Avisynth+ specific
1028   // Note: these functions are simulated/use fallback to existing functions
1029   AVSC_DECLARE_FUNC(avs_is_rgb48);
1030   AVSC_DECLARE_FUNC(avs_is_rgb64);
1031   AVSC_DECLARE_FUNC(avs_is_yuv444p16);
1032   AVSC_DECLARE_FUNC(avs_is_yuv422p16);
1033   AVSC_DECLARE_FUNC(avs_is_yuv420p16);
1034   AVSC_DECLARE_FUNC(avs_is_y16);
1035   AVSC_DECLARE_FUNC(avs_is_yuv444ps);
1036   AVSC_DECLARE_FUNC(avs_is_yuv422ps);
1037   AVSC_DECLARE_FUNC(avs_is_yuv420ps);
1038   AVSC_DECLARE_FUNC(avs_is_y32);
1039   AVSC_DECLARE_FUNC(avs_is_444);
1040   AVSC_DECLARE_FUNC(avs_is_422);
1041   AVSC_DECLARE_FUNC(avs_is_420);
1042   AVSC_DECLARE_FUNC(avs_is_y);
1043   AVSC_DECLARE_FUNC(avs_is_yuva);
1044   AVSC_DECLARE_FUNC(avs_is_planar_rgb);
1045   AVSC_DECLARE_FUNC(avs_is_planar_rgba);
1046   AVSC_DECLARE_FUNC(avs_num_components);
1047   AVSC_DECLARE_FUNC(avs_component_size);
1048   AVSC_DECLARE_FUNC(avs_bits_per_component);
1049   // end of Avisynth+ specific
1050 
1051 };
1052 
1053 #undef AVSC_DECLARE_FUNC
1054 
1055 // Helper functions for fallback simulation
1056 // Avisynth+ extensions do not exist in classic Avisynth so they are simulated
avs_is_xx_fallback_return_false(const AVS_VideoInfo * p)1057 AVSC_INLINE int avs_is_xx_fallback_return_false(const AVS_VideoInfo * p)
1058 {
1059   return 0;
1060 }
1061 
1062 // Avisynth+ extensions do not exist in classic Avisynth so they are simulated
avs_num_components_fallback(const AVS_VideoInfo * p)1063 AVSC_INLINE int avs_num_components_fallback(const AVS_VideoInfo * p)
1064 {
1065   switch (p->pixel_type) {
1066   case AVS_CS_UNKNOWN:
1067     return 0;
1068   case AVS_CS_RAW32:
1069   case AVS_CS_Y8:
1070     return 1;
1071   case AVS_CS_BGR32:
1072     return 4; // not planar but return the count
1073   default:
1074     return 3;
1075   }
1076 }
1077 
1078 // Avisynth+ extensions do not exist in classic Avisynth so they are simulated
avs_component_size_fallback(const AVS_VideoInfo * p)1079 AVSC_INLINE int avs_component_size_fallback(const AVS_VideoInfo * p)
1080 {
1081   return 1;
1082 }
1083 
1084 // Avisynth+ extensions do not exist in classic Avisynth so they are simulated
avs_bits_per_component_fallback(const AVS_VideoInfo * p)1085 AVSC_INLINE int avs_bits_per_component_fallback(const AVS_VideoInfo * p)
1086 {
1087   return 8;
1088 }
1089 // End of helper functions for fallback simulation
1090 
1091 // avs_load_library() allocates an array for API procedure entries
1092 // reads and fills the entries with live procedure addresses.
1093 // AVSC_INLINE helpers which are calling into API procedures are not treated here (todo)
1094 
avs_load_library()1095 AVSC_INLINE AVS_Library * avs_load_library() {
1096   AVS_Library *library = (AVS_Library *)malloc(sizeof(AVS_Library));
1097   if (library == NULL)
1098     return NULL;
1099   library->handle = LoadLibraryA("avisynth");
1100   if (library->handle == NULL)
1101     goto fail;
1102 
1103 #define __AVSC_STRINGIFY(x) #x
1104 #define AVSC_STRINGIFY(x) __AVSC_STRINGIFY(x)
1105 #define AVSC_LOAD_FUNC(name) {\
1106   library->name = (name##_func) GetProcAddress(library->handle, AVSC_STRINGIFY(name));\
1107   if (library->name == NULL)\
1108     goto fail;\
1109 }
1110 
1111 #if 0
1112 // FFmpeg-specific: we don't use the FALLBACK stuff, and it causes build errors,
1113 // so ifdef it out on our side.
1114 
1115 // When an API function is not loadable, let's try a replacement
1116 // Missing Avisynth+ functions will be substituted with classic Avisynth compatible methods
1117 /*
1118 Avisynth+                 When method is missing (classic Avisynth)
1119 avs_is_rgb48              constant false
1120 avs_is_rgb64              constant false
1121 avs_is_yuv444p16          constant false
1122 avs_is_yuv422p16          constant false
1123 avs_is_yuv420p16          constant false
1124 avs_is_y16                constant false
1125 avs_is_yuv444ps           constant false
1126 avs_is_yuv422ps           constant false
1127 avs_is_yuv420ps           constant false
1128 avs_is_y32                constant false
1129 avs_is_444                avs_is_yv24
1130 avs_is_422                avs_is_yv16
1131 avs_is_420                avs_is_yv12
1132 avs_is_y                  avs_is_y8
1133 avs_is_yuva               constant false
1134 avs_is_planar_rgb         constant false
1135 avs_is_planar_rgba        constant false
1136 avs_num_components        special: avs_num_components_fake Y8:1 RGB32:4 else 3
1137 avs_component_size        constant 1 (1 bytes/component)
1138 avs_bits_per_component    constant 8 (8 bits/component)
1139 */
1140 
1141   // try to load an alternative function
1142 #define AVSC_LOAD_FUNC_FALLBACK(name,name2) {\
1143   library->name = (name##_func) GetProcAddress(library->handle, AVSC_STRINGIFY(name));\
1144   if (library->name == NULL)\
1145     library->name = (name##_func) GetProcAddress(library->handle, AVSC_STRINGIFY(name2));\
1146   if (library->name == NULL)\
1147     goto fail;\
1148 }
1149 
1150   // try to assign a replacement function
1151 #define AVSC_LOAD_FUNC_FALLBACK_SIMULATED(name,name2) {\
1152   library->name = (name##_func) GetProcAddress(library->handle, AVSC_STRINGIFY(name));\
1153   if (library->name == NULL)\
1154     library->name = name2;\
1155   if (library->name == NULL)\
1156     goto fail;\
1157 }
1158 #endif
1159 
1160   AVSC_LOAD_FUNC(avs_add_function);
1161   AVSC_LOAD_FUNC(avs_at_exit);
1162   AVSC_LOAD_FUNC(avs_bit_blt);
1163   AVSC_LOAD_FUNC(avs_check_version);
1164   AVSC_LOAD_FUNC(avs_clip_get_error);
1165   AVSC_LOAD_FUNC(avs_copy_clip);
1166   AVSC_LOAD_FUNC(avs_copy_value);
1167   AVSC_LOAD_FUNC(avs_copy_video_frame);
1168   AVSC_LOAD_FUNC(avs_create_script_environment);
1169   AVSC_LOAD_FUNC(avs_delete_script_environment);
1170   AVSC_LOAD_FUNC(avs_function_exists);
1171   AVSC_LOAD_FUNC(avs_get_audio);
1172   AVSC_LOAD_FUNC(avs_get_cpu_flags);
1173   AVSC_LOAD_FUNC(avs_get_frame);
1174   AVSC_LOAD_FUNC(avs_get_parity);
1175   AVSC_LOAD_FUNC(avs_get_var);
1176   AVSC_LOAD_FUNC(avs_get_version);
1177   AVSC_LOAD_FUNC(avs_get_video_info);
1178   AVSC_LOAD_FUNC(avs_invoke);
1179   AVSC_LOAD_FUNC(avs_make_writable);
1180   AVSC_LOAD_FUNC(avs_new_c_filter);
1181   AVSC_LOAD_FUNC(avs_new_video_frame_a);
1182   AVSC_LOAD_FUNC(avs_release_clip);
1183   AVSC_LOAD_FUNC(avs_release_value);
1184   AVSC_LOAD_FUNC(avs_release_video_frame);
1185   AVSC_LOAD_FUNC(avs_save_string);
1186   AVSC_LOAD_FUNC(avs_set_cache_hints);
1187   AVSC_LOAD_FUNC(avs_set_global_var);
1188   AVSC_LOAD_FUNC(avs_set_memory_max);
1189   AVSC_LOAD_FUNC(avs_set_to_clip);
1190   AVSC_LOAD_FUNC(avs_set_var);
1191   AVSC_LOAD_FUNC(avs_set_working_dir);
1192   AVSC_LOAD_FUNC(avs_sprintf);
1193   AVSC_LOAD_FUNC(avs_subframe);
1194   AVSC_LOAD_FUNC(avs_subframe_planar);
1195   AVSC_LOAD_FUNC(avs_take_clip);
1196   AVSC_LOAD_FUNC(avs_vsprintf);
1197 
1198   AVSC_LOAD_FUNC(avs_get_error);
1199   AVSC_LOAD_FUNC(avs_is_yv24);
1200   AVSC_LOAD_FUNC(avs_is_yv16);
1201   AVSC_LOAD_FUNC(avs_is_yv12);
1202   AVSC_LOAD_FUNC(avs_is_yv411);
1203   AVSC_LOAD_FUNC(avs_is_y8);
1204   AVSC_LOAD_FUNC(avs_is_color_space);
1205 
1206   AVSC_LOAD_FUNC(avs_get_plane_width_subsampling);
1207   AVSC_LOAD_FUNC(avs_get_plane_height_subsampling);
1208   AVSC_LOAD_FUNC(avs_bits_per_pixel);
1209   AVSC_LOAD_FUNC(avs_bytes_from_pixels);
1210   AVSC_LOAD_FUNC(avs_row_size);
1211   AVSC_LOAD_FUNC(avs_bmp_size);
1212   AVSC_LOAD_FUNC(avs_get_pitch_p);
1213   AVSC_LOAD_FUNC(avs_get_row_size_p);
1214   AVSC_LOAD_FUNC(avs_get_height_p);
1215   AVSC_LOAD_FUNC(avs_get_read_ptr_p);
1216   AVSC_LOAD_FUNC(avs_is_writable);
1217   AVSC_LOAD_FUNC(avs_get_write_ptr_p);
1218 
1219 #if 0
1220   // Avisynth+ specific but made them callable for classic Avisynth hosts
1221   AVSC_LOAD_FUNC_FALLBACK_SIMULATED(avs_is_rgb48, avs_is_xx_fallback_return_false);
1222   AVSC_LOAD_FUNC_FALLBACK_SIMULATED(avs_is_rgb64, avs_is_xx_fallback_return_false);
1223   AVSC_LOAD_FUNC_FALLBACK_SIMULATED(avs_is_yuv444p16, avs_is_xx_fallback_return_false);
1224   AVSC_LOAD_FUNC_FALLBACK_SIMULATED(avs_is_yuv422p16, avs_is_xx_fallback_return_false);
1225   AVSC_LOAD_FUNC_FALLBACK_SIMULATED(avs_is_yuv420p16, avs_is_xx_fallback_return_false);
1226   AVSC_LOAD_FUNC_FALLBACK_SIMULATED(avs_is_y16, avs_is_xx_fallback_return_false);
1227   AVSC_LOAD_FUNC_FALLBACK_SIMULATED(avs_is_yuv444ps, avs_is_xx_fallback_return_false);
1228   AVSC_LOAD_FUNC_FALLBACK_SIMULATED(avs_is_yuv422ps, avs_is_xx_fallback_return_false);
1229   AVSC_LOAD_FUNC_FALLBACK_SIMULATED(avs_is_yuv420ps, avs_is_xx_fallback_return_false);
1230   AVSC_LOAD_FUNC_FALLBACK_SIMULATED(avs_is_y32, avs_is_xx_fallback_return_false);
1231   AVSC_LOAD_FUNC_FALLBACK(avs_is_444, avs_is_yv24);
1232   AVSC_LOAD_FUNC_FALLBACK(avs_is_422, avs_is_yv16);
1233   AVSC_LOAD_FUNC_FALLBACK(avs_is_420, avs_is_yv12);
1234   AVSC_LOAD_FUNC_FALLBACK(avs_is_y, avs_is_y8);
1235   AVSC_LOAD_FUNC_FALLBACK_SIMULATED(avs_is_yuva, avs_is_xx_fallback_return_false);
1236   AVSC_LOAD_FUNC_FALLBACK_SIMULATED(avs_is_planar_rgb, avs_is_xx_fallback_return_false);
1237   AVSC_LOAD_FUNC_FALLBACK_SIMULATED(avs_is_planar_rgba, avs_is_xx_fallback_return_false);
1238   AVSC_LOAD_FUNC_FALLBACK_SIMULATED(avs_num_components, avs_num_components_fallback);
1239   AVSC_LOAD_FUNC_FALLBACK_SIMULATED(avs_component_size, avs_component_size_fallback);
1240   AVSC_LOAD_FUNC_FALLBACK_SIMULATED(avs_bits_per_component, avs_bits_per_component_fallback);
1241 #endif
1242 
1243 #undef __AVSC_STRINGIFY
1244 #undef AVSC_STRINGIFY
1245 #undef AVSC_LOAD_FUNC
1246 #undef AVSC_LOAD_FUNC_FALLBACK
1247 #undef AVSC_LOAD_FUNC_FALLBACK_SIMULATED
1248 
1249   return library;
1250 
1251 fail:
1252   free(library);
1253   return NULL;
1254 }
1255 
avs_free_library(AVS_Library * library)1256 AVSC_INLINE void avs_free_library(AVS_Library *library) {
1257   if (library == NULL)
1258     return;
1259   FreeLibrary(library->handle);
1260   free(library);
1261 }
1262 #endif
1263 
1264 #endif
1265