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.

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