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., 675 Mass Ave, Cambridge, MA 02139, USA, or visit
17 // http://www.gnu.org/copyleft/gpl.html .
18 //
19 // As a special exception, I give you permission to link to the
20 // Avisynth C interface with independent modules that communicate with
21 // the Avisynth C interface solely through the interfaces defined in
22 // avisynth_c.h, regardless of the license terms of these independent
23 // modules, and to copy and distribute the resulting combined work
24 // under terms of your choice, provided that every copy of the
25 // combined work is accompanied by a complete copy of the source code
26 // of the Avisynth C interface and Avisynth itself (with the version
27 // used to produce the combined work), being distributed under the
28 // terms of the GNU General Public License plus this exception.  An
29 // independent module is a module which is not derived from or based
30 // on Avisynth C Interface, such as 3rd-party filters, import and
31 // export plugins, or graphical user interfaces.
32 
33 // NOTE: this is a partial update of the Avisynth C interface to recognize
34 // new color spaces added in Avisynth 2.60. By no means is this document
35 // completely Avisynth 2.60 compliant.
36 
37 #ifndef __AVISYNTH_C__
38 #define __AVISYNTH_C__
39 
40 #ifdef __cplusplus
41 #  define EXTERN_C extern "C"
42 #else
43 #  define EXTERN_C
44 #endif
45 
46 #define AVSC_USE_STDCALL 1
47 
48 #ifndef AVSC_USE_STDCALL
49 #  define AVSC_CC __cdecl
50 #else
51 #  define AVSC_CC __stdcall
52 #endif
53 
54 #define AVSC_INLINE static __inline
55 
56 #ifdef AVISYNTH_C_EXPORTS
57 #  define AVSC_EXPORT __declspec(dllexport)
58 #  define AVSC_API(ret, name) EXTERN_C AVSC_EXPORT ret AVSC_CC name
59 #else
60 #  define AVSC_EXPORT __declspec(dllimport)
61 #  ifndef AVSC_NO_DECLSPEC
62 #    define AVSC_API(ret, name) EXTERN_C AVSC_EXPORT ret AVSC_CC name
63 #  else
64 #    define AVSC_API(ret, name) typedef ret (AVSC_CC *name##_func)
65 #  endif
66 #endif
67 
68 typedef unsigned char BYTE;
69 #ifdef __GNUC__
70 typedef long long int INT64;
71 #else
72 typedef __int64 INT64;
73 #endif
74 
75 
76 /////////////////////////////////////////////////////////////////////
77 //
78 // Constants
79 //
80 
81 #ifndef __AVISYNTH_6_H__
82 enum { AVISYNTH_INTERFACE_VERSION = 6 };
83 #endif
84 
85 enum {AVS_SAMPLE_INT8  = 1<<0,
86       AVS_SAMPLE_INT16 = 1<<1,
87       AVS_SAMPLE_INT24 = 1<<2,
88       AVS_SAMPLE_INT32 = 1<<3,
89       AVS_SAMPLE_FLOAT = 1<<4};
90 
91 enum {AVS_PLANAR_Y=1<<0,
92       AVS_PLANAR_U=1<<1,
93       AVS_PLANAR_V=1<<2,
94       AVS_PLANAR_ALIGNED=1<<3,
95       AVS_PLANAR_Y_ALIGNED=AVS_PLANAR_Y|AVS_PLANAR_ALIGNED,
96       AVS_PLANAR_U_ALIGNED=AVS_PLANAR_U|AVS_PLANAR_ALIGNED,
97       AVS_PLANAR_V_ALIGNED=AVS_PLANAR_V|AVS_PLANAR_ALIGNED,
98       AVS_PLANAR_A=1<<4,
99       AVS_PLANAR_R=1<<5,
100       AVS_PLANAR_G=1<<6,
101       AVS_PLANAR_B=1<<7,
102       AVS_PLANAR_A_ALIGNED=AVS_PLANAR_A|AVS_PLANAR_ALIGNED,
103       AVS_PLANAR_R_ALIGNED=AVS_PLANAR_R|AVS_PLANAR_ALIGNED,
104       AVS_PLANAR_G_ALIGNED=AVS_PLANAR_G|AVS_PLANAR_ALIGNED,
105       AVS_PLANAR_B_ALIGNED=AVS_PLANAR_B|AVS_PLANAR_ALIGNED};
106 
107   // Colorspace properties.
108 enum {AVS_CS_BGR = 1<<28,
109       AVS_CS_YUV = 1<<29,
110       AVS_CS_INTERLEAVED = 1<<30,
111       AVS_CS_PLANAR = 1<<31,
112 
113       AVS_CS_SHIFT_SUB_WIDTH   = 0,
114       AVS_CS_SHIFT_SUB_HEIGHT  = 8,
115       AVS_CS_SHIFT_SAMPLE_BITS = 16,
116 
117       AVS_CS_SUB_WIDTH_MASK    = 7 << AVS_CS_SHIFT_SUB_WIDTH,
118       AVS_CS_SUB_WIDTH_1       = 3 << AVS_CS_SHIFT_SUB_WIDTH, // YV24
119       AVS_CS_SUB_WIDTH_2       = 0 << AVS_CS_SHIFT_SUB_WIDTH, // YV12, I420, YV16
120       AVS_CS_SUB_WIDTH_4       = 1 << AVS_CS_SHIFT_SUB_WIDTH, // YUV9, YV411
121 
122       AVS_CS_VPLANEFIRST       = 1 << 3, // YV12, YV16, YV24, YV411, YUV9
123       AVS_CS_UPLANEFIRST       = 1 << 4, // I420
124 
125       AVS_CS_SUB_HEIGHT_MASK   = 7 << AVS_CS_SHIFT_SUB_HEIGHT,
126       AVS_CS_SUB_HEIGHT_1      = 3 << AVS_CS_SHIFT_SUB_HEIGHT, // YV16, YV24, YV411
127       AVS_CS_SUB_HEIGHT_2      = 0 << AVS_CS_SHIFT_SUB_HEIGHT, // YV12, I420
128       AVS_CS_SUB_HEIGHT_4      = 1 << AVS_CS_SHIFT_SUB_HEIGHT, // YUV9
129 
130       AVS_CS_SAMPLE_BITS_MASK  = 7 << AVS_CS_SHIFT_SAMPLE_BITS,
131       AVS_CS_SAMPLE_BITS_8     = 0 << AVS_CS_SHIFT_SAMPLE_BITS,
132       AVS_CS_SAMPLE_BITS_16    = 1 << AVS_CS_SHIFT_SAMPLE_BITS,
133       AVS_CS_SAMPLE_BITS_32    = 2 << AVS_CS_SHIFT_SAMPLE_BITS,
134 
135       AVS_CS_PLANAR_MASK       = AVS_CS_PLANAR | AVS_CS_INTERLEAVED | AVS_CS_YUV | AVS_CS_BGR | AVS_CS_SAMPLE_BITS_MASK | AVS_CS_SUB_HEIGHT_MASK | AVS_CS_SUB_WIDTH_MASK,
136       AVS_CS_PLANAR_FILTER     = ~( AVS_CS_VPLANEFIRST | AVS_CS_UPLANEFIRST )};
137 
138   // Specific colorformats
139 enum {
140   AVS_CS_UNKNOWN = 0,
141   AVS_CS_BGR24 = 1<<0 | AVS_CS_BGR | AVS_CS_INTERLEAVED,
142   AVS_CS_BGR32 = 1<<1 | AVS_CS_BGR | AVS_CS_INTERLEAVED,
143   AVS_CS_YUY2 = 1<<2 | AVS_CS_YUV | AVS_CS_INTERLEAVED,
144   //  AVS_CS_YV12  = 1<<3  Reserved
145   //  AVS_CS_I420  = 1<<4  Reserved
146   AVS_CS_RAW32 = 1<<5 | AVS_CS_INTERLEAVED,
147 
148   AVS_CS_YV24  = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8 | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_1 | AVS_CS_SUB_WIDTH_1,  // YVU 4:4:4 planar
149   AVS_CS_YV16  = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8 | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_1 | AVS_CS_SUB_WIDTH_2,  // YVU 4:2:2 planar
150   AVS_CS_YV12  = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8 | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_2 | AVS_CS_SUB_WIDTH_2,  // YVU 4:2:0 planar
151   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
152   AVS_CS_IYUV  = AVS_CS_I420,
153   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,  // YVU 4:1:1 planar
154   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,  // YVU 4:1:0 planar
155   AVS_CS_Y8    = AVS_CS_PLANAR | AVS_CS_INTERLEAVED | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8                                              // Y   4:0:0 planar
156 };
157 
158 enum {
159   AVS_IT_BFF = 1<<0,
160   AVS_IT_TFF = 1<<1,
161   AVS_IT_FIELDBASED = 1<<2};
162 
163 enum {
164   AVS_FILTER_TYPE=1,
165   AVS_FILTER_INPUT_COLORSPACE=2,
166   AVS_FILTER_OUTPUT_TYPE=9,
167   AVS_FILTER_NAME=4,
168   AVS_FILTER_AUTHOR=5,
169   AVS_FILTER_VERSION=6,
170   AVS_FILTER_ARGS=7,
171   AVS_FILTER_ARGS_INFO=8,
172   AVS_FILTER_ARGS_DESCRIPTION=10,
173   AVS_FILTER_DESCRIPTION=11};
174 
175 enum {  //SUBTYPES
176   AVS_FILTER_TYPE_AUDIO=1,
177   AVS_FILTER_TYPE_VIDEO=2,
178   AVS_FILTER_OUTPUT_TYPE_SAME=3,
179   AVS_FILTER_OUTPUT_TYPE_DIFFERENT=4};
180 
181 enum {
182   // New 2.6 explicitly defined cache hints.
183   AVS_CACHE_NOTHING=10, // Do not cache video.
184   AVS_CACHE_WINDOW=11, // Hard protect upto X frames within a range of X from the current frame N.
185   AVS_CACHE_GENERIC=12, // LRU cache upto X frames.
186   AVS_CACHE_FORCE_GENERIC=13, // LRU cache upto X frames, override any previous CACHE_WINDOW.
187 
188   AVS_CACHE_GET_POLICY=30, // Get the current policy.
189   AVS_CACHE_GET_WINDOW=31, // Get the current window h_span.
190   AVS_CACHE_GET_RANGE=32, // Get the current generic frame range.
191 
192   AVS_CACHE_AUDIO=50, // Explicitly do cache audio, X byte cache.
193   AVS_CACHE_AUDIO_NOTHING=51, // Explicitly do not cache audio.
194   AVS_CACHE_AUDIO_NONE=52, // Audio cache off (auto mode), X byte intial cache.
195   AVS_CACHE_AUDIO_AUTO=53, // Audio cache on (auto mode), X byte intial cache.
196 
197   AVS_CACHE_GET_AUDIO_POLICY=70, // Get the current audio policy.
198   AVS_CACHE_GET_AUDIO_SIZE=71, // Get the current audio cache size.
199 
200   AVS_CACHE_PREFETCH_FRAME=100, // Queue request to prefetch frame N.
201   AVS_CACHE_PREFETCH_GO=101, // Action video prefetches.
202 
203   AVS_CACHE_PREFETCH_AUDIO_BEGIN=120, // Begin queue request transaction to prefetch audio (take critical section).
204   AVS_CACHE_PREFETCH_AUDIO_STARTLO=121, // Set low 32 bits of start.
205   AVS_CACHE_PREFETCH_AUDIO_STARTHI=122, // Set high 32 bits of start.
206   AVS_CACHE_PREFETCH_AUDIO_COUNT=123, // Set low 32 bits of length.
207   AVS_CACHE_PREFETCH_AUDIO_COMMIT=124, // Enqueue request transaction to prefetch audio (release critical section).
208   AVS_CACHE_PREFETCH_AUDIO_GO=125, // Action audio prefetches.
209 
210   AVS_CACHE_GETCHILD_CACHE_MODE=200, // Cache ask Child for desired video cache mode.
211   AVS_CACHE_GETCHILD_CACHE_SIZE=201, // Cache ask Child for desired video cache size.
212   AVS_CACHE_GETCHILD_AUDIO_MODE=202, // Cache ask Child for desired audio cache mode.
213   AVS_CACHE_GETCHILD_AUDIO_SIZE=203, // Cache ask Child for desired audio cache size.
214 
215   AVS_CACHE_GETCHILD_COST=220, // Cache ask Child for estimated processing cost.
216     AVS_CACHE_COST_ZERO=221, // Child response of zero cost (ptr arithmetic only).
217     AVS_CACHE_COST_UNIT=222, // Child response of unit cost (less than or equal 1 full frame blit).
218     AVS_CACHE_COST_LOW=223, // Child response of light cost. (Fast)
219     AVS_CACHE_COST_MED=224, // Child response of medium cost. (Real time)
220     AVS_CACHE_COST_HI=225, // Child response of heavy cost. (Slow)
221 
222   AVS_CACHE_GETCHILD_THREAD_MODE=240, // Cache ask Child for thread safetyness.
223     AVS_CACHE_THREAD_UNSAFE=241, // Only 1 thread allowed for all instances. 2.5 filters default!
224     AVS_CACHE_THREAD_CLASS=242, // Only 1 thread allowed for each instance. 2.6 filters default!
225     AVS_CACHE_THREAD_SAFE=243, //  Allow all threads in any instance.
226     AVS_CACHE_THREAD_OWN=244, // Safe but limit to 1 thread, internally threaded.
227 
228   AVS_CACHE_GETCHILD_ACCESS_COST=260, // Cache ask Child for preferred access pattern.
229     AVS_CACHE_ACCESS_RAND=261, // Filter is access order agnostic.
230     AVS_CACHE_ACCESS_SEQ0=262, // Filter prefers sequential access (low cost)
231     AVS_CACHE_ACCESS_SEQ1=263, // Filter needs sequential access (high cost)
232   };
233 
234 #define AVS_FRAME_ALIGN 16
235 
236 typedef struct AVS_Clip AVS_Clip;
237 typedef struct AVS_ScriptEnvironment AVS_ScriptEnvironment;
238 
239 /////////////////////////////////////////////////////////////////////
240 //
241 // AVS_VideoInfo
242 //
243 
244 // AVS_VideoInfo is layed out identicly to VideoInfo
245 typedef struct AVS_VideoInfo {
246   int width, height;    // width=0 means no video
247   unsigned fps_numerator, fps_denominator;
248   int num_frames;
249 
250   int pixel_type;
251 
252   int audio_samples_per_second;   // 0 means no audio
253   int sample_type;
254   INT64 num_audio_samples;
255   int nchannels;
256 
257   // Imagetype properties
258 
259   int image_type;
260 } AVS_VideoInfo;
261 
262 // useful functions of the above
avs_has_video(const AVS_VideoInfo * p)263 AVSC_INLINE int avs_has_video(const AVS_VideoInfo * p)
264         { return (p->width!=0); }
265 
avs_has_audio(const AVS_VideoInfo * p)266 AVSC_INLINE int avs_has_audio(const AVS_VideoInfo * p)
267         { return (p->audio_samples_per_second!=0); }
268 
avs_is_rgb(const AVS_VideoInfo * p)269 AVSC_INLINE int avs_is_rgb(const AVS_VideoInfo * p)
270         { return !!(p->pixel_type&AVS_CS_BGR); }
271 
avs_is_rgb24(const AVS_VideoInfo * p)272 AVSC_INLINE int avs_is_rgb24(const AVS_VideoInfo * p)
273         { return ((p->pixel_type&AVS_CS_BGR24)==AVS_CS_BGR24) && ((p->pixel_type & AVS_CS_SAMPLE_BITS_MASK) == AVS_CS_SAMPLE_BITS_8); }
274 
avs_is_rgb32(const AVS_VideoInfo * p)275 AVSC_INLINE int avs_is_rgb32(const AVS_VideoInfo * p)
276         { return ((p->pixel_type&AVS_CS_BGR32)==AVS_CS_BGR32) && ((p->pixel_type & AVS_CS_SAMPLE_BITS_MASK) == AVS_CS_SAMPLE_BITS_8); }
277 
avs_is_yuv(const AVS_VideoInfo * p)278 AVSC_INLINE int avs_is_yuv(const AVS_VideoInfo * p)
279         { return !!(p->pixel_type&AVS_CS_YUV ); }
280 
avs_is_yuy2(const AVS_VideoInfo * p)281 AVSC_INLINE int avs_is_yuy2(const AVS_VideoInfo * p)
282         { return (p->pixel_type & AVS_CS_YUY2) == AVS_CS_YUY2; }
283 
284 AVSC_API(int, avs_is_yv24)(const AVS_VideoInfo * p);
285 
286 AVSC_API(int, avs_is_yv16)(const AVS_VideoInfo * p);
287 
288 AVSC_API(int, avs_is_yv12)(const AVS_VideoInfo * p);
289 
290 AVSC_API(int, avs_is_yv411)(const AVS_VideoInfo * p);
291 
292 AVSC_API(int, avs_is_y8)(const AVS_VideoInfo * p);
293 
294 #ifdef AVSC_NO_DECLSPEC
avs_is_yv24(const AVS_VideoInfo * p)295 AVSC_INLINE int avs_is_yv24(const AVS_VideoInfo * p)
296         { return (p->pixel_type & AVS_CS_PLANAR_MASK) == (AVS_CS_YV24  & AVS_CS_PLANAR_FILTER); }
297 
avs_is_yv16(const AVS_VideoInfo * p)298 AVSC_INLINE int avs_is_yv16(const AVS_VideoInfo * p)
299         { return (p->pixel_type & AVS_CS_PLANAR_MASK) == (AVS_CS_YV16  & AVS_CS_PLANAR_FILTER); }
300 
avs_is_yv12(const AVS_VideoInfo * p)301 AVSC_INLINE int avs_is_yv12(const AVS_VideoInfo * p)
302         { return (p->pixel_type & AVS_CS_PLANAR_MASK) == (AVS_CS_YV12  & AVS_CS_PLANAR_FILTER); }
303 
avs_is_yv411(const AVS_VideoInfo * p)304 AVSC_INLINE int avs_is_yv411(const AVS_VideoInfo * p)
305         { return (p->pixel_type & AVS_CS_PLANAR_MASK) == (AVS_CS_YV411 & AVS_CS_PLANAR_FILTER); }
306 
avs_is_y8(const AVS_VideoInfo * p)307 AVSC_INLINE int avs_is_y8(const AVS_VideoInfo * p)
308         { return (p->pixel_type & AVS_CS_PLANAR_MASK) == (AVS_CS_Y8    & AVS_CS_PLANAR_FILTER); }
309 #endif
310 
311 #if 1 // AviSynth+ extension
312 AVSC_API(int, avs_is_rgb48)(const AVS_VideoInfo * p);
313 
314 AVSC_API(int, avs_is_rgb64)(const AVS_VideoInfo * p);
315 
316 AVSC_API(int, avs_is_yuv444p16)(const AVS_VideoInfo * p);
317 
318 AVSC_API(int, avs_is_yuv422p16)(const AVS_VideoInfo * p);
319 
320 AVSC_API(int, avs_is_yuv420p16)(const AVS_VideoInfo * p);
321 
322 AVSC_API(int, avs_is_y16)(const AVS_VideoInfo * p);
323 
324 AVSC_API(int, avs_is_yuv444ps)(const AVS_VideoInfo * p);
325 
326 AVSC_API(int, avs_is_yuv422ps)(const AVS_VideoInfo * p);
327 
328 AVSC_API(int, avs_is_yuv420ps)(const AVS_VideoInfo * p);
329 
330 AVSC_API(int, avs_is_y32)(const AVS_VideoInfo * p);
331 
332 AVSC_API(int, avs_is_444)(const AVS_VideoInfo * p);
333 
334 AVSC_API(int, avs_is_422)(const AVS_VideoInfo * p);
335 
336 AVSC_API(int, avs_is_420)(const AVS_VideoInfo * p);
337 
338 AVSC_API(int, avs_is_y)(const AVS_VideoInfo * p);
339 
340 AVSC_API(int, avs_is_yuva)(const AVS_VideoInfo * p);
341 
342 AVSC_API(int, avs_is_planar_rgb)(const AVS_VideoInfo * p);
343 
344 AVSC_API(int, avs_is_planar_rgba)(const AVS_VideoInfo * p);
345 
346 AVSC_API(int, avs_num_components)(const AVS_VideoInfo * p);
347 
348 AVSC_API(int, avs_component_size)(const AVS_VideoInfo * p);
349 
350 AVSC_API(int, avs_bits_per_component)(const AVS_VideoInfo * p);
351 #endif
352 
avs_is_property(const AVS_VideoInfo * p,int property)353 AVSC_INLINE int avs_is_property(const AVS_VideoInfo * p, int property)
354         { return ((p->image_type & property)==property ); }
355 
avs_is_planar(const AVS_VideoInfo * p)356 AVSC_INLINE int avs_is_planar(const AVS_VideoInfo * p)
357         { return !!(p->pixel_type & AVS_CS_PLANAR); }
358 
359 AVSC_API(int, avs_is_color_space)(const AVS_VideoInfo * p, int c_space);
360 
avs_is_field_based(const AVS_VideoInfo * p)361 AVSC_INLINE int avs_is_field_based(const AVS_VideoInfo * p)
362         { return !!(p->image_type & AVS_IT_FIELDBASED); }
363 
avs_is_parity_known(const AVS_VideoInfo * p)364 AVSC_INLINE int avs_is_parity_known(const AVS_VideoInfo * p)
365         { return ((p->image_type & AVS_IT_FIELDBASED)&&(p->image_type & (AVS_IT_BFF | AVS_IT_TFF))); }
366 
avs_is_bff(const AVS_VideoInfo * p)367 AVSC_INLINE int avs_is_bff(const AVS_VideoInfo * p)
368         { return !!(p->image_type & AVS_IT_BFF); }
369 
avs_is_tff(const AVS_VideoInfo * p)370 AVSC_INLINE int avs_is_tff(const AVS_VideoInfo * p)
371         { return !!(p->image_type & AVS_IT_TFF); }
372 
373 AVSC_API(int, avs_get_plane_width_subsampling)(const AVS_VideoInfo * p, int plane);
374 
375 AVSC_API(int, avs_get_plane_height_subsampling)(const AVS_VideoInfo * p, int plane);
376 
377 
378 AVSC_API(int, avs_bits_per_pixel)(const AVS_VideoInfo * p);
379 
380 AVSC_API(int, avs_bytes_from_pixels)(const AVS_VideoInfo * p, int pixels);
381 
382 AVSC_API(int, avs_row_size_p)(const AVS_VideoInfo * p, int plane);
383 
384 #ifndef AVSC_NO_DECLSPEC
avs_row_size(const AVS_VideoInfo * p)385 AVSC_INLINE int avs_row_size(const AVS_VideoInfo * p)
386         { return avs_row_size_p(p, 0); }
387 #endif
388 
389 AVSC_API(int, avs_bmp_size)(const AVS_VideoInfo * vi);
390 
avs_samples_per_second(const AVS_VideoInfo * p)391 AVSC_INLINE int avs_samples_per_second(const AVS_VideoInfo * p)
392         { return p->audio_samples_per_second; }
393 
394 
avs_bytes_per_channel_sample(const AVS_VideoInfo * p)395 AVSC_INLINE int avs_bytes_per_channel_sample(const AVS_VideoInfo * p)
396 {
397     switch (p->sample_type) {
398       case AVS_SAMPLE_INT8:  return sizeof(signed char);
399       case AVS_SAMPLE_INT16: return sizeof(signed short);
400       case AVS_SAMPLE_INT24: return 3;
401       case AVS_SAMPLE_INT32: return sizeof(signed int);
402       case AVS_SAMPLE_FLOAT: return sizeof(float);
403       default: return 0;
404     }
405 }
avs_bytes_per_audio_sample(const AVS_VideoInfo * p)406 AVSC_INLINE int avs_bytes_per_audio_sample(const AVS_VideoInfo * p)
407         { return p->nchannels*avs_bytes_per_channel_sample(p);}
408 
avs_audio_samples_from_frames(const AVS_VideoInfo * p,INT64 frames)409 AVSC_INLINE INT64 avs_audio_samples_from_frames(const AVS_VideoInfo * p, INT64 frames)
410         { return ((INT64)(frames) * p->audio_samples_per_second * p->fps_denominator / p->fps_numerator); }
411 
avs_frames_from_audio_samples(const AVS_VideoInfo * p,INT64 samples)412 AVSC_INLINE int avs_frames_from_audio_samples(const AVS_VideoInfo * p, INT64 samples)
413         { return (int)(samples * (INT64)p->fps_numerator / (INT64)p->fps_denominator / (INT64)p->audio_samples_per_second); }
414 
avs_audio_samples_from_bytes(const AVS_VideoInfo * p,INT64 bytes)415 AVSC_INLINE INT64 avs_audio_samples_from_bytes(const AVS_VideoInfo * p, INT64 bytes)
416         { return bytes / avs_bytes_per_audio_sample(p); }
417 
avs_bytes_from_audio_samples(const AVS_VideoInfo * p,INT64 samples)418 AVSC_INLINE INT64 avs_bytes_from_audio_samples(const AVS_VideoInfo * p, INT64 samples)
419         { return samples * avs_bytes_per_audio_sample(p); }
420 
avs_audio_channels(const AVS_VideoInfo * p)421 AVSC_INLINE int avs_audio_channels(const AVS_VideoInfo * p)
422         { return p->nchannels; }
423 
avs_sample_type(const AVS_VideoInfo * p)424 AVSC_INLINE int avs_sample_type(const AVS_VideoInfo * p)
425         { return p->sample_type;}
426 
427 // useful mutator
avs_set_property(AVS_VideoInfo * p,int property)428 AVSC_INLINE void avs_set_property(AVS_VideoInfo * p, int property)
429         { p->image_type|=property; }
430 
avs_clear_property(AVS_VideoInfo * p,int property)431 AVSC_INLINE void avs_clear_property(AVS_VideoInfo * p, int property)
432         { p->image_type&=~property; }
433 
avs_set_field_based(AVS_VideoInfo * p,int isfieldbased)434 AVSC_INLINE void avs_set_field_based(AVS_VideoInfo * p, int isfieldbased)
435         { if (isfieldbased) p->image_type|=AVS_IT_FIELDBASED; else p->image_type&=~AVS_IT_FIELDBASED; }
436 
avs_set_fps(AVS_VideoInfo * p,unsigned numerator,unsigned denominator)437 AVSC_INLINE void avs_set_fps(AVS_VideoInfo * p, unsigned numerator, unsigned denominator)
438 {
439     unsigned x=numerator, y=denominator;
440     while (y) {   // find gcd
441       unsigned t = x%y; x = y; y = t;
442     }
443     p->fps_numerator = numerator/x;
444     p->fps_denominator = denominator/x;
445 }
446 
avs_is_same_colorspace(AVS_VideoInfo * x,AVS_VideoInfo * y)447 AVSC_INLINE int avs_is_same_colorspace(AVS_VideoInfo * x, AVS_VideoInfo * y)
448 {
449         return (x->pixel_type == y->pixel_type)
450                 || (avs_is_yv12(x) && avs_is_yv12(y));
451 }
452 
453 /////////////////////////////////////////////////////////////////////
454 //
455 // AVS_VideoFrame
456 //
457 
458 // VideoFrameBuffer holds information about a memory block which is used
459 // for video data.  For efficiency, instances of this class are not deleted
460 // when the refcount reaches zero; instead they're stored in a linked list
461 // to be reused.  The instances are deleted when the corresponding AVS
462 // file is closed.
463 
464 // AVS_VideoFrameBuffer is layed out identicly to VideoFrameBuffer
465 // DO NOT USE THIS STRUCTURE DIRECTLY
466 typedef struct AVS_VideoFrameBuffer {
467   BYTE * data;
468   int data_size;
469   // sequence_number is incremented every time the buffer is changed, so
470   // that stale views can tell they're no longer valid.
471   volatile long sequence_number;
472 
473   volatile long refcount;
474 } AVS_VideoFrameBuffer;
475 
476 // VideoFrame holds a "window" into a VideoFrameBuffer.
477 
478 // AVS_VideoFrame is layed out identicly to IVideoFrame
479 // DO NOT USE THIS STRUCTURE DIRECTLY
480 typedef struct AVS_VideoFrame {
481   volatile long refcount;
482   AVS_VideoFrameBuffer * vfb;
483   int offset, pitch, row_size, height, offsetU, offsetV, pitchUV;  // U&V offsets are from top of picture.
484   int row_sizeUV, heightUV;
485 } AVS_VideoFrame;
486 
487 // Access functions for AVS_VideoFrame
488 AVSC_API(int, avs_get_pitch_p)(const AVS_VideoFrame * p, int plane);
489 
490 #ifdef AVSC_NO_DECLSPEC
avs_get_pitch_p(const AVS_VideoFrame * p,int plane)491 AVSC_INLINE int avs_get_pitch_p(const AVS_VideoFrame * p, int plane) {
492         switch (plane) {
493           case AVS_PLANAR_U:
494           case AVS_PLANAR_V:
495             return p->pitchUV;
496         }
497         return p->pitch;
498 }
499 #endif
500 
avs_get_pitch(const AVS_VideoFrame * p)501 AVSC_INLINE int avs_get_pitch(const AVS_VideoFrame * p) {
502         return avs_get_pitch_p(p, 0);}
503 
504 AVSC_API(int, avs_get_row_size_p)(const AVS_VideoFrame * p, int plane);
505 
avs_get_row_size(const AVS_VideoFrame * p)506 AVSC_INLINE int avs_get_row_size(const AVS_VideoFrame * p) {
507         return p->row_size; }
508 
509 AVSC_API(int, avs_get_height_p)(const AVS_VideoFrame * p, int plane);
510 
avs_get_height(const AVS_VideoFrame * p)511 AVSC_INLINE int avs_get_height(const AVS_VideoFrame * p) {
512         return p->height;}
513 
514 AVSC_API(const BYTE *, avs_get_read_ptr_p)(const AVS_VideoFrame * p, int plane);
515 
516 #ifdef AVSC_NO_DECLSPEC
avs_get_read_ptr_p(const AVS_VideoFrame * p,int plane)517 AVSC_INLINE const BYTE* avs_get_read_ptr_p(const AVS_VideoFrame * p, int plane) {
518         switch (plane) {
519           case AVS_PLANAR_U: return p->vfb->data + p->offsetU;
520           case AVS_PLANAR_V: return p->vfb->data + p->offsetV;
521           default:           return p->vfb->data + p->offset;
522         }
523 }
524 #endif
525 
avs_get_read_ptr(const AVS_VideoFrame * p)526 AVSC_INLINE const BYTE* avs_get_read_ptr(const AVS_VideoFrame * p) {
527         return avs_get_read_ptr_p(p, 0);}
528 
529 AVSC_API(int, avs_is_writable)(const AVS_VideoFrame * p);
530 
531 AVSC_API(BYTE *, avs_get_write_ptr_p)(const AVS_VideoFrame * p, int plane);
532 
533 #ifndef AVSC_NO_DECLSPEC
avs_get_write_ptr(const AVS_VideoFrame * p)534 AVSC_INLINE BYTE* avs_get_write_ptr(const AVS_VideoFrame * p) {
535         return avs_get_write_ptr_p(p, 0);}
536 #endif
537 
538 AVSC_API(void, avs_release_video_frame)(AVS_VideoFrame *);
539 // makes a shallow copy of a video frame
540 AVSC_API(AVS_VideoFrame *, avs_copy_video_frame)(AVS_VideoFrame *);
541 
542 #ifndef AVSC_NO_DECLSPEC
avs_release_frame(AVS_VideoFrame * f)543 AVSC_INLINE void avs_release_frame(AVS_VideoFrame * f)
544   {avs_release_video_frame(f);}
avs_copy_frame(AVS_VideoFrame * f)545 AVSC_INLINE AVS_VideoFrame * avs_copy_frame(AVS_VideoFrame * f)
546   {return avs_copy_video_frame(f);}
547 #endif
548 
549 /////////////////////////////////////////////////////////////////////
550 //
551 // AVS_Value
552 //
553 
554 // Treat AVS_Value as a fat pointer.  That is use avs_copy_value
555 // and avs_release_value appropiaty as you would if AVS_Value was
556 // a pointer.
557 
558 // To maintain source code compatibility with future versions of the
559 // avisynth_c API don't use the AVS_Value directly.  Use the helper
560 // functions below.
561 
562 // AVS_Value is layed out identicly to AVSValue
563 typedef struct AVS_Value AVS_Value;
564 struct AVS_Value {
565   short type;  // 'a'rray, 'c'lip, 'b'ool, 'i'nt, 'f'loat, 's'tring, 'v'oid, or 'l'ong
566                // for some function e'rror
567   short array_size;
568   union {
569     void * clip; // do not use directly, use avs_take_clip
570     char boolean;
571     int integer;
572     float floating_pt;
573     const char * string;
574     const AVS_Value * array;
575   } d;
576 };
577 
578 // AVS_Value should be initilized with avs_void.
579 // Should also set to avs_void after the value is released
580 // with avs_copy_value.  Consider it the equalvent of setting
581 // a pointer to NULL
582 static const AVS_Value avs_void = {'v'};
583 
584 AVSC_API(void, avs_copy_value)(AVS_Value * dest, AVS_Value src);
585 AVSC_API(void, avs_release_value)(AVS_Value);
586 
avs_defined(AVS_Value v)587 AVSC_INLINE int avs_defined(AVS_Value v) { return v.type != 'v'; }
avs_is_clip(AVS_Value v)588 AVSC_INLINE int avs_is_clip(AVS_Value v) { return v.type == 'c'; }
avs_is_bool(AVS_Value v)589 AVSC_INLINE int avs_is_bool(AVS_Value v) { return v.type == 'b'; }
avs_is_int(AVS_Value v)590 AVSC_INLINE int avs_is_int(AVS_Value v) { return v.type == 'i'; }
avs_is_float(AVS_Value v)591 AVSC_INLINE int avs_is_float(AVS_Value v) { return v.type == 'f' || v.type == 'i'; }
avs_is_string(AVS_Value v)592 AVSC_INLINE int avs_is_string(AVS_Value v) { return v.type == 's'; }
avs_is_array(AVS_Value v)593 AVSC_INLINE int avs_is_array(AVS_Value v) { return v.type == 'a'; }
avs_is_error(AVS_Value v)594 AVSC_INLINE int avs_is_error(AVS_Value v) { return v.type == 'e'; }
595 
596 AVSC_API(AVS_Clip *, avs_take_clip)(AVS_Value, AVS_ScriptEnvironment *);
597 AVSC_API(void, avs_set_to_clip)(AVS_Value *, AVS_Clip *);
598 
avs_as_bool(AVS_Value v)599 AVSC_INLINE int avs_as_bool(AVS_Value v)
600         { return v.d.boolean; }
avs_as_int(AVS_Value v)601 AVSC_INLINE int avs_as_int(AVS_Value v)
602         { return v.d.integer; }
avs_as_string(AVS_Value v)603 AVSC_INLINE const char * avs_as_string(AVS_Value v)
604         { return avs_is_error(v) || avs_is_string(v) ? v.d.string : 0; }
avs_as_float(AVS_Value v)605 AVSC_INLINE double avs_as_float(AVS_Value v)
606         { return avs_is_int(v) ? v.d.integer : v.d.floating_pt; }
avs_as_error(AVS_Value v)607 AVSC_INLINE const char * avs_as_error(AVS_Value v)
608         { return avs_is_error(v) ? v.d.string : 0; }
avs_as_array(AVS_Value v)609 AVSC_INLINE const AVS_Value * avs_as_array(AVS_Value v)
610         { return v.d.array; }
avs_array_size(AVS_Value v)611 AVSC_INLINE int avs_array_size(AVS_Value v)
612         { return avs_is_array(v) ? v.array_size : 1; }
avs_array_elt(AVS_Value v,int index)613 AVSC_INLINE AVS_Value avs_array_elt(AVS_Value v, int index)
614         { return avs_is_array(v) ? v.d.array[index] : v; }
615 
616 // only use these functions on an AVS_Value that does not already have
617 // an active value.  Remember, treat AVS_Value as a fat pointer.
avs_new_value_bool(int v0)618 AVSC_INLINE AVS_Value avs_new_value_bool(int v0)
619         { AVS_Value v; v.type = 'b'; v.d.boolean = v0 == 0 ? 0 : 1; return v; }
avs_new_value_int(int v0)620 AVSC_INLINE AVS_Value avs_new_value_int(int v0)
621         { AVS_Value v; v.type = 'i'; v.d.integer = v0; return v; }
avs_new_value_string(const char * v0)622 AVSC_INLINE AVS_Value avs_new_value_string(const char * v0)
623         { AVS_Value v; v.type = 's'; v.d.string = v0; return v; }
avs_new_value_float(float v0)624 AVSC_INLINE AVS_Value avs_new_value_float(float v0)
625         { AVS_Value v; v.type = 'f'; v.d.floating_pt = v0; return v;}
avs_new_value_error(const char * v0)626 AVSC_INLINE AVS_Value avs_new_value_error(const char * v0)
627         { AVS_Value v; v.type = 'e'; v.d.string = v0; return v; }
628 #ifndef AVSC_NO_DECLSPEC
avs_new_value_clip(AVS_Clip * v0)629 AVSC_INLINE AVS_Value avs_new_value_clip(AVS_Clip * v0)
630         { AVS_Value v; avs_set_to_clip(&v, v0); return v; }
631 #endif
avs_new_value_array(AVS_Value * v0,int size)632 AVSC_INLINE AVS_Value avs_new_value_array(AVS_Value * v0, int size)
633         { AVS_Value v; v.type = 'a'; v.d.array = v0; v.array_size = (short)size; return v; }
634 
635 /////////////////////////////////////////////////////////////////////
636 //
637 // AVS_Clip
638 //
639 
640 AVSC_API(void, avs_release_clip)(AVS_Clip *);
641 AVSC_API(AVS_Clip *, avs_copy_clip)(AVS_Clip *);
642 
643 AVSC_API(const char *, avs_clip_get_error)(AVS_Clip *); // return 0 if no error
644 
645 AVSC_API(const AVS_VideoInfo *, avs_get_video_info)(AVS_Clip *);
646 
647 AVSC_API(int, avs_get_version)(AVS_Clip *);
648 
649 AVSC_API(AVS_VideoFrame *, avs_get_frame)(AVS_Clip *, int n);
650 // The returned video frame must be released with avs_release_video_frame
651 
652 AVSC_API(int, avs_get_parity)(AVS_Clip *, int n);
653 // return field parity if field_based, else parity of first field in frame
654 
655 AVSC_API(int, avs_get_audio)(AVS_Clip *, void * buf,
656                              INT64 start, INT64 count);
657 // start and count are in samples
658 
659 AVSC_API(int, avs_set_cache_hints)(AVS_Clip *,
660                                    int cachehints, int frame_range);
661 
662 // This is the callback type used by avs_add_function
663 typedef AVS_Value (AVSC_CC * AVS_ApplyFunc)
664                         (AVS_ScriptEnvironment *, AVS_Value args, void * user_data);
665 
666 typedef struct AVS_FilterInfo AVS_FilterInfo;
667 struct AVS_FilterInfo
668 {
669   // these members should not be modified outside of the AVS_ApplyFunc callback
670   AVS_Clip * child;
671   AVS_VideoInfo vi;
672   AVS_ScriptEnvironment * env;
673   AVS_VideoFrame * (AVSC_CC * get_frame)(AVS_FilterInfo *, int n);
674   int (AVSC_CC * get_parity)(AVS_FilterInfo *, int n);
675   int (AVSC_CC * get_audio)(AVS_FilterInfo *, void * buf,
676                                   INT64 start, INT64 count);
677   int (AVSC_CC * set_cache_hints)(AVS_FilterInfo *, int cachehints,
678                                         int frame_range);
679   void (AVSC_CC * free_filter)(AVS_FilterInfo *);
680 
681   // Should be set when ever there is an error to report.
682   // It is cleared before any of the above methods are called
683   const char * error;
684   // this is to store whatever and may be modified at will
685   void * user_data;
686 };
687 
688 // Create a new filter
689 // fi is set to point to the AVS_FilterInfo so that you can
690 //   modify it once it is initilized.
691 // store_child should generally be set to true.  If it is not
692 //    set than ALL methods (the function pointers) must be defined
693 // If it is set than you do not need to worry about freeing the child
694 //    clip.
695 AVSC_API(AVS_Clip *, avs_new_c_filter)(AVS_ScriptEnvironment * e,
696                                        AVS_FilterInfo * * fi,
697                                        AVS_Value child, int store_child);
698 
699 /////////////////////////////////////////////////////////////////////
700 //
701 // AVS_ScriptEnvironment
702 //
703 
704 // For GetCPUFlags.  These are backwards-compatible with those in VirtualDub.
705 enum {
706                                 /* slowest CPU to support extension */
707   AVS_CPU_FORCE        = 0x01,   // N/A
708   AVS_CPU_FPU          = 0x02,   // 386/486DX
709   AVS_CPU_MMX          = 0x04,   // P55C, K6, PII
710   AVS_CPU_INTEGER_SSE  = 0x08,   // PIII, Athlon
711   AVS_CPU_SSE          = 0x10,   // PIII, Athlon XP/MP
712   AVS_CPU_SSE2         = 0x20,   // PIV, Hammer
713   AVS_CPU_3DNOW        = 0x40,   // K6-2
714   AVS_CPU_3DNOW_EXT    = 0x80,   // Athlon
715   AVS_CPU_X86_64       = 0xA0,   // Hammer (note: equiv. to 3DNow + SSE2,
716                                  // which only Hammer will have anyway)
717   AVS_CPUF_SSE3       = 0x100,   //  PIV+, K8 Venice
718   AVS_CPUF_SSSE3      = 0x200,   //  Core 2
719   AVS_CPUF_SSE4       = 0x400,   //  Penryn, Wolfdale, Yorkfield
720   AVS_CPUF_SSE4_1     = 0x400,
721 //AVS_CPUF_AVX        = 0x800,   //  Sandy Bridge, Bulldozer
722   AVS_CPUF_SSE4_2    = 0x1000,   //  Nehalem
723 //AVS_CPUF_AVX2      = 0x2000,   //  Haswell
724 //AVS_CPUF_AVX512    = 0x4000,   //  Knights Landing
725 };
726 
727 
728 AVSC_API(const char *, avs_get_error)(AVS_ScriptEnvironment *); // return 0 if no error
729 
730 AVSC_API(long, avs_get_cpu_flags)(AVS_ScriptEnvironment *);
731 AVSC_API(int, avs_check_version)(AVS_ScriptEnvironment *, int version);
732 
733 AVSC_API(char *, avs_save_string)(AVS_ScriptEnvironment *, const char* s, int length);
734 AVSC_API(char *, avs_sprintf)(AVS_ScriptEnvironment *, const char * fmt, ...);
735 
736 AVSC_API(char *, avs_vsprintf)(AVS_ScriptEnvironment *, const char * fmt, void* val);
737  // note: val is really a va_list; I hope everyone typedefs va_list to a pointer
738 
739 AVSC_API(int, avs_add_function)(AVS_ScriptEnvironment *,
740                                 const char * name, const char * params,
741                                 AVS_ApplyFunc apply, void * user_data);
742 
743 AVSC_API(int, avs_function_exists)(AVS_ScriptEnvironment *, const char * name);
744 
745 AVSC_API(AVS_Value, avs_invoke)(AVS_ScriptEnvironment *, const char * name,
746                                AVS_Value args, const char** arg_names);
747 // The returned value must be be released with avs_release_value
748 
749 AVSC_API(AVS_Value, avs_get_var)(AVS_ScriptEnvironment *, const char* name);
750 // The returned value must be be released with avs_release_value
751 
752 AVSC_API(int, avs_set_var)(AVS_ScriptEnvironment *, const char* name, AVS_Value val);
753 
754 AVSC_API(int, avs_set_global_var)(AVS_ScriptEnvironment *, const char* name, const AVS_Value val);
755 
756 //void avs_push_context(AVS_ScriptEnvironment *, int level=0);
757 //void avs_pop_context(AVS_ScriptEnvironment *);
758 
759 AVSC_API(AVS_VideoFrame *, avs_new_video_frame_a)(AVS_ScriptEnvironment *,
760                                           const AVS_VideoInfo * vi, int align);
761 // align should be at least 16
762 
763 #ifndef AVSC_NO_DECLSPEC
764 AVSC_INLINE
avs_new_video_frame(AVS_ScriptEnvironment * env,const AVS_VideoInfo * vi)765 AVS_VideoFrame * avs_new_video_frame(AVS_ScriptEnvironment * env,
766                                      const AVS_VideoInfo * vi)
767   {return avs_new_video_frame_a(env,vi,AVS_FRAME_ALIGN);}
768 
769 AVSC_INLINE
avs_new_frame(AVS_ScriptEnvironment * env,const AVS_VideoInfo * vi)770 AVS_VideoFrame * avs_new_frame(AVS_ScriptEnvironment * env,
771                                const AVS_VideoInfo * vi)
772   {return avs_new_video_frame_a(env,vi,AVS_FRAME_ALIGN);}
773 #endif
774 
775 
776 AVSC_API(int, avs_make_writable)(AVS_ScriptEnvironment *, AVS_VideoFrame * * pvf);
777 
778 AVSC_API(void, avs_bit_blt)(AVS_ScriptEnvironment *, BYTE* dstp, int dst_pitch, const BYTE* srcp, int src_pitch, int row_size, int height);
779 
780 typedef void (AVSC_CC *AVS_ShutdownFunc)(void* user_data, AVS_ScriptEnvironment * env);
781 AVSC_API(void, avs_at_exit)(AVS_ScriptEnvironment *, AVS_ShutdownFunc function, void * user_data);
782 
783 AVSC_API(AVS_VideoFrame *, avs_subframe)(AVS_ScriptEnvironment *, AVS_VideoFrame * src, int rel_offset, int new_pitch, int new_row_size, int new_height);
784 // The returned video frame must be be released
785 
786 AVSC_API(int, avs_set_memory_max)(AVS_ScriptEnvironment *, int mem);
787 
788 AVSC_API(int, avs_set_working_dir)(AVS_ScriptEnvironment *, const char * newdir);
789 
790 // avisynth.dll exports this; it's a way to use it as a library, without
791 // writing an AVS script or without going through AVIFile.
792 AVSC_API(AVS_ScriptEnvironment *, avs_create_script_environment)(int version);
793 
794 // this symbol is the entry point for the plugin and must
795 // be defined
796 AVSC_EXPORT
797 const char * AVSC_CC avisynth_c_plugin_init(AVS_ScriptEnvironment* env);
798 
799 
800 AVSC_API(void, avs_delete_script_environment)(AVS_ScriptEnvironment *);
801 
802 
803 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);
804 // The returned video frame must be be released
805 
806 #ifdef AVSC_NO_DECLSPEC
807 // use LoadLibrary and related functions to dynamically load Avisynth instead of declspec(dllimport)
808 /*
809   The following functions needs to have been declared, probably from windows.h
810 
811   void* malloc(size_t)
812   void free(void*);
813 
814   HMODULE LoadLibrary(const char*);
815   void* GetProcAddress(HMODULE, const char*);
816   FreeLibrary(HMODULE);
817 */
818 
819 
820 typedef struct AVS_Library AVS_Library;
821 
822 #define AVSC_DECLARE_FUNC(name) name##_func name
823 
824 struct AVS_Library {
825   HMODULE handle;
826 
827   AVSC_DECLARE_FUNC(avs_add_function);
828   AVSC_DECLARE_FUNC(avs_at_exit);
829   AVSC_DECLARE_FUNC(avs_bit_blt);
830   AVSC_DECLARE_FUNC(avs_check_version);
831   AVSC_DECLARE_FUNC(avs_clip_get_error);
832   AVSC_DECLARE_FUNC(avs_copy_clip);
833   AVSC_DECLARE_FUNC(avs_copy_value);
834   AVSC_DECLARE_FUNC(avs_copy_video_frame);
835   AVSC_DECLARE_FUNC(avs_create_script_environment);
836   AVSC_DECLARE_FUNC(avs_delete_script_environment);
837   AVSC_DECLARE_FUNC(avs_function_exists);
838   AVSC_DECLARE_FUNC(avs_get_audio);
839   AVSC_DECLARE_FUNC(avs_get_cpu_flags);
840   AVSC_DECLARE_FUNC(avs_get_frame);
841   AVSC_DECLARE_FUNC(avs_get_parity);
842   AVSC_DECLARE_FUNC(avs_get_var);
843   AVSC_DECLARE_FUNC(avs_get_version);
844   AVSC_DECLARE_FUNC(avs_get_video_info);
845   AVSC_DECLARE_FUNC(avs_invoke);
846   AVSC_DECLARE_FUNC(avs_make_writable);
847   AVSC_DECLARE_FUNC(avs_new_c_filter);
848   AVSC_DECLARE_FUNC(avs_new_video_frame_a);
849   AVSC_DECLARE_FUNC(avs_release_clip);
850   AVSC_DECLARE_FUNC(avs_release_value);
851   AVSC_DECLARE_FUNC(avs_release_video_frame);
852   AVSC_DECLARE_FUNC(avs_save_string);
853   AVSC_DECLARE_FUNC(avs_set_cache_hints);
854   AVSC_DECLARE_FUNC(avs_set_global_var);
855   AVSC_DECLARE_FUNC(avs_set_memory_max);
856   AVSC_DECLARE_FUNC(avs_set_to_clip);
857   AVSC_DECLARE_FUNC(avs_set_var);
858   AVSC_DECLARE_FUNC(avs_set_working_dir);
859   AVSC_DECLARE_FUNC(avs_sprintf);
860   AVSC_DECLARE_FUNC(avs_subframe);
861   AVSC_DECLARE_FUNC(avs_subframe_planar);
862   AVSC_DECLARE_FUNC(avs_take_clip);
863   AVSC_DECLARE_FUNC(avs_vsprintf);
864 
865   AVSC_DECLARE_FUNC(avs_get_error);
866   AVSC_DECLARE_FUNC(avs_is_yv24);
867   AVSC_DECLARE_FUNC(avs_is_yv16);
868   AVSC_DECLARE_FUNC(avs_is_yv12);
869   AVSC_DECLARE_FUNC(avs_is_yv411);
870   AVSC_DECLARE_FUNC(avs_is_y8);
871   AVSC_DECLARE_FUNC(avs_is_color_space);
872 
873   AVSC_DECLARE_FUNC(avs_get_plane_width_subsampling);
874   AVSC_DECLARE_FUNC(avs_get_plane_height_subsampling);
875   AVSC_DECLARE_FUNC(avs_bits_per_pixel);
876   AVSC_DECLARE_FUNC(avs_bytes_from_pixels);
877   AVSC_DECLARE_FUNC(avs_row_size_p);
878   AVSC_DECLARE_FUNC(avs_bmp_size);
879   AVSC_DECLARE_FUNC(avs_get_pitch_p);
880   AVSC_DECLARE_FUNC(avs_get_row_size_p);
881   AVSC_DECLARE_FUNC(avs_get_height_p);
882   AVSC_DECLARE_FUNC(avs_get_read_ptr_p);
883   AVSC_DECLARE_FUNC(avs_is_writable);
884   AVSC_DECLARE_FUNC(avs_get_write_ptr_p);
885 };
886 
887 #undef AVSC_DECLARE_FUNC
888 
889 
avs_load_library()890 AVSC_INLINE AVS_Library * avs_load_library() {
891   AVS_Library *library = (AVS_Library *)malloc(sizeof(AVS_Library));
892   if (library == NULL)
893     return NULL;
894   library->handle = LoadLibrary("avisynth");
895   if (library->handle == NULL)
896     goto fail;
897 
898 #define __AVSC_STRINGIFY(x) #x
899 #define AVSC_STRINGIFY(x) __AVSC_STRINGIFY(x)
900 #define AVSC_LOAD_FUNC(name) {\
901   library->name = (name##_func) GetProcAddress(library->handle, AVSC_STRINGIFY(name));\
902   if (library->name == NULL)\
903     goto fail;\
904 }
905 
906   AVSC_LOAD_FUNC(avs_add_function);
907   AVSC_LOAD_FUNC(avs_at_exit);
908   AVSC_LOAD_FUNC(avs_bit_blt);
909   AVSC_LOAD_FUNC(avs_check_version);
910   AVSC_LOAD_FUNC(avs_clip_get_error);
911   AVSC_LOAD_FUNC(avs_copy_clip);
912   AVSC_LOAD_FUNC(avs_copy_value);
913   AVSC_LOAD_FUNC(avs_copy_video_frame);
914   AVSC_LOAD_FUNC(avs_create_script_environment);
915   AVSC_LOAD_FUNC(avs_delete_script_environment);
916   AVSC_LOAD_FUNC(avs_function_exists);
917   AVSC_LOAD_FUNC(avs_get_audio);
918   AVSC_LOAD_FUNC(avs_get_cpu_flags);
919   AVSC_LOAD_FUNC(avs_get_frame);
920   AVSC_LOAD_FUNC(avs_get_parity);
921   AVSC_LOAD_FUNC(avs_get_var);
922   AVSC_LOAD_FUNC(avs_get_version);
923   AVSC_LOAD_FUNC(avs_get_video_info);
924   AVSC_LOAD_FUNC(avs_invoke);
925   AVSC_LOAD_FUNC(avs_make_writable);
926   AVSC_LOAD_FUNC(avs_new_c_filter);
927   AVSC_LOAD_FUNC(avs_new_video_frame_a);
928   AVSC_LOAD_FUNC(avs_release_clip);
929   AVSC_LOAD_FUNC(avs_release_value);
930   AVSC_LOAD_FUNC(avs_release_video_frame);
931   AVSC_LOAD_FUNC(avs_save_string);
932   AVSC_LOAD_FUNC(avs_set_cache_hints);
933   AVSC_LOAD_FUNC(avs_set_global_var);
934   AVSC_LOAD_FUNC(avs_set_memory_max);
935   AVSC_LOAD_FUNC(avs_set_to_clip);
936   AVSC_LOAD_FUNC(avs_set_var);
937   AVSC_LOAD_FUNC(avs_set_working_dir);
938   AVSC_LOAD_FUNC(avs_sprintf);
939   AVSC_LOAD_FUNC(avs_subframe);
940   AVSC_LOAD_FUNC(avs_subframe_planar);
941   AVSC_LOAD_FUNC(avs_take_clip);
942   AVSC_LOAD_FUNC(avs_vsprintf);
943 
944   AVSC_LOAD_FUNC(avs_get_error);
945   AVSC_LOAD_FUNC(avs_is_yv24);
946   AVSC_LOAD_FUNC(avs_is_yv16);
947   AVSC_LOAD_FUNC(avs_is_yv12);
948   AVSC_LOAD_FUNC(avs_is_yv411);
949   AVSC_LOAD_FUNC(avs_is_y8);
950   AVSC_LOAD_FUNC(avs_is_color_space);
951 
952   AVSC_LOAD_FUNC(avs_get_plane_width_subsampling);
953   AVSC_LOAD_FUNC(avs_get_plane_height_subsampling);
954   AVSC_LOAD_FUNC(avs_bits_per_pixel);
955   AVSC_LOAD_FUNC(avs_bytes_from_pixels);
956   AVSC_LOAD_FUNC(avs_row_size_p);
957   AVSC_LOAD_FUNC(avs_bmp_size);
958   AVSC_LOAD_FUNC(avs_get_pitch_p);
959   AVSC_LOAD_FUNC(avs_get_row_size_p);
960   AVSC_LOAD_FUNC(avs_get_height_p);
961   AVSC_LOAD_FUNC(avs_get_read_ptr_p);
962   AVSC_LOAD_FUNC(avs_is_writable);
963   AVSC_LOAD_FUNC(avs_get_write_ptr_p);
964 
965 #undef __AVSC_STRINGIFY
966 #undef AVSC_STRINGIFY
967 #undef AVSC_LOAD_FUNC
968 
969   return library;
970 
971 fail:
972   free(library);
973   return NULL;
974 }
975 
avs_free_library(AVS_Library * library)976 AVSC_INLINE void avs_free_library(AVS_Library *library) {
977   if (library == NULL)
978     return;
979   FreeLibrary(library->handle);
980   free(library);
981 }
982 #endif
983 
984 #endif
985