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