You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

881 lines
32KB

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