1
0

rlib.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. LICENSE
  3. -------
  4. Copyright 2005 Nullsoft, Inc.
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. * Neither the name of Nullsoft nor the names of its contributors may be used to
  14. endorse or promote products derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  21. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  22. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include <windows.h>
  25. #include "r_defs.h"
  26. #include "r_unkn.h"
  27. #include "r_list.h"
  28. #include "rlib.h"
  29. #include "ape.h"
  30. #include "avs_eelif.h"
  31. #include "resource.h"
  32. #include "../Agave/Language/api_language.h"
  33. #define PUT_INT(y) data[pos]=(y)&255; data[pos+1]=(y>>8)&255; data[pos+2]=(y>>16)&255; data[pos+3]=(y>>24)&255
  34. #define GET_INT() (data[pos]|(data[pos+1]<<8)|(data[pos+2]<<16)|(data[pos+3]<<24))
  35. void C_RBASE::load_string(RString &s,unsigned char *data, int &pos, int len) // read configuration of max length "len" from data.
  36. {
  37. int size=GET_INT(); pos += 4;
  38. if (size > 0 && len-pos >= size)
  39. {
  40. s.resize(size);
  41. memcpy(s.get(), data+pos, size);
  42. pos+=size;
  43. }
  44. else
  45. {
  46. s.resize(1);
  47. s.get()[0]=0;
  48. }
  49. }
  50. void C_RBASE::save_string(unsigned char *data, int &pos, RString &text)
  51. {
  52. if (text.get() && text.get()[0])
  53. {
  54. char *p=text.get();
  55. int x=32768;
  56. while (x-- && *p) p++;
  57. if (*p)
  58. {
  59. MessageBox(NULL,"Yo, this is some long ass shit","FUCK!",MB_OK);
  60. //FUCKO
  61. }
  62. int l=(strlen(text.get())+1);
  63. PUT_INT(l); pos+=4;
  64. memcpy(data+pos, text.get(), strlen(text.get())+1);
  65. pos+=strlen(text.get())+1;
  66. }
  67. else
  68. {
  69. PUT_INT(0);
  70. pos+=4;
  71. }
  72. }
  73. void C_RLibrary::add_dofx(void *rf, int has_r2)
  74. {
  75. if ((NumRetrFuncs&7)==0||!RetrFuncs)
  76. {
  77. void *newdl=(void *)GlobalAlloc(GMEM_FIXED,sizeof(rfStruct)*(NumRetrFuncs+8));
  78. if (!newdl)
  79. {
  80. char title[64];
  81. MessageBox(NULL,WASABI_API_LNGSTRING(IDS_OUT_OF_MEMORY),
  82. WASABI_API_LNGSTRING_BUF(IDS_AVS_CRITICAL_ERROR,title,64),MB_OK);
  83. ExitProcess(0);
  84. }
  85. memset(newdl,0,sizeof(rfStruct)*(NumRetrFuncs+8));
  86. if (RetrFuncs)
  87. {
  88. memcpy(newdl,RetrFuncs,NumRetrFuncs*sizeof(rfStruct));
  89. GlobalFree(RetrFuncs);
  90. }
  91. RetrFuncs=(rfStruct*)newdl;
  92. }
  93. RetrFuncs[NumRetrFuncs].is_r2=has_r2;
  94. *((void**)&RetrFuncs[NumRetrFuncs].rf) = rf;
  95. NumRetrFuncs++;
  96. }
  97. // declarations for built-in effects
  98. #define DECLARE_EFFECT(name) extern C_RBASE *(name)(char *desc); add_dofx((void*)name,0);
  99. #define DECLARE_EFFECT2(name) extern C_RBASE *(name)(char *desc); add_dofx((void*)name,1);
  100. void C_RLibrary::initfx(void)
  101. {
  102. DECLARE_EFFECT(R_SimpleSpectrum);
  103. DECLARE_EFFECT(R_DotPlane);
  104. DECLARE_EFFECT(R_OscStars);
  105. DECLARE_EFFECT(R_FadeOut);
  106. DECLARE_EFFECT(R_BlitterFB);
  107. DECLARE_EFFECT(R_NFClear);
  108. DECLARE_EFFECT2(R_Blur);
  109. DECLARE_EFFECT(R_BSpin);
  110. DECLARE_EFFECT(R_Parts);
  111. DECLARE_EFFECT(R_RotBlit);
  112. DECLARE_EFFECT(R_SVP);
  113. DECLARE_EFFECT2(R_ColorFade);
  114. DECLARE_EFFECT(R_ContrastEnhance);
  115. DECLARE_EFFECT(R_RotStar);
  116. DECLARE_EFFECT(R_OscRings);
  117. DECLARE_EFFECT2(R_Trans);
  118. DECLARE_EFFECT(R_Scat);
  119. DECLARE_EFFECT(R_DotGrid);
  120. DECLARE_EFFECT(R_Stack);
  121. DECLARE_EFFECT(R_DotFountain);
  122. DECLARE_EFFECT2(R_Water);
  123. DECLARE_EFFECT(R_Comment);
  124. DECLARE_EFFECT2(R_Brightness);
  125. DECLARE_EFFECT(R_Interleave);
  126. DECLARE_EFFECT(R_Grain);
  127. DECLARE_EFFECT(R_Clear);
  128. DECLARE_EFFECT(R_Mirror);
  129. DECLARE_EFFECT(R_StarField);
  130. DECLARE_EFFECT(R_Text);
  131. DECLARE_EFFECT(R_Bump);
  132. DECLARE_EFFECT(R_Mosaic);
  133. DECLARE_EFFECT(R_WaterBump);
  134. DECLARE_EFFECT(R_AVI);
  135. DECLARE_EFFECT(R_Bpm);
  136. DECLARE_EFFECT(R_Picture);
  137. DECLARE_EFFECT(R_DDM);
  138. DECLARE_EFFECT(R_SScope);
  139. DECLARE_EFFECT(R_Invert);
  140. DECLARE_EFFECT(R_Onetone);
  141. DECLARE_EFFECT(R_Timescope);
  142. DECLARE_EFFECT(R_LineMode);
  143. DECLARE_EFFECT(R_Interferences);
  144. DECLARE_EFFECT(R_Shift);
  145. DECLARE_EFFECT2(R_DMove);
  146. DECLARE_EFFECT(R_FastBright);
  147. DECLARE_EFFECT(R_DColorMod);
  148. }
  149. static const struct
  150. {
  151. char id[33];
  152. int newidx;
  153. } NamedApeToBuiltinTrans[] =
  154. {
  155. {"Winamp Brightness APE v1", 22},
  156. {"Winamp Interleave APE v1", 23},
  157. {"Winamp Grain APE v1", 24 },
  158. {"Winamp ClearScreen APE v1", 25},
  159. {"Nullsoft MIRROR v1", 26},
  160. {"Winamp Starfield v1", 27},
  161. {"Winamp Text v1", 28 },
  162. {"Winamp Bump v1", 29 },
  163. {"Winamp Mosaic v1", 30 },
  164. {"Winamp AVIAPE v1", 32},
  165. {"Nullsoft Picture Rendering v1", 34},
  166. {"Winamp Interf APE v1", 41}
  167. };
  168. void C_RLibrary::initbuiltinape(void)
  169. {
  170. #define ADD(sym) extern C_RBASE * sym(char *desc); _add_dll(0,sym,"Builtin_" #sym, 0)
  171. #define ADD2(sym,name) extern C_RBASE * sym(char *desc); _add_dll(0,sym,name, 0)
  172. #ifdef LASER
  173. ADD(RLASER_Cone);
  174. ADD(RLASER_BeatHold);
  175. ADD(RLASER_Line);
  176. ADD(RLASER_Bren); // not including it for now
  177. ADD(RLASER_Transform);
  178. #else
  179. ADD2(R_ChannelShift,"Channel Shift");
  180. ADD2(R_ColorReduction,"Color Reduction");
  181. ADD2(R_Multiplier,"Multiplier");
  182. ADD2(R_VideoDelay,"Holden04: Video Delay");
  183. ADD2(R_MultiDelay,"Holden05: Multi Delay");
  184. #endif
  185. #undef ADD
  186. #undef ADD2
  187. }
  188. void C_RLibrary::_add_dll(HINSTANCE hlib,class C_RBASE *(__cdecl *cre)(char *),char *inf, int is_r2)
  189. {
  190. if ((NumDLLFuncs&7)==0||!DLLFuncs)
  191. {
  192. DLLInfo *newdl=(DLLInfo *)GlobalAlloc(GMEM_FIXED,sizeof(DLLInfo)*(NumDLLFuncs+8));
  193. if (!newdl)
  194. {
  195. char title[64];
  196. MessageBox(NULL,WASABI_API_LNGSTRING(IDS_OUT_OF_MEMORY),
  197. WASABI_API_LNGSTRING_BUF(IDS_AVS_CRITICAL_ERROR,title,64),MB_OK);
  198. ExitProcess(0);
  199. }
  200. memset(newdl,0,sizeof(DLLInfo)*(NumDLLFuncs+8));
  201. if (DLLFuncs)
  202. {
  203. memcpy(newdl,DLLFuncs,NumDLLFuncs*sizeof(DLLInfo));
  204. GlobalFree(DLLFuncs);
  205. }
  206. DLLFuncs=newdl;
  207. }
  208. DLLFuncs[NumDLLFuncs].hDllInstance=hlib;
  209. DLLFuncs[NumDLLFuncs].createfunc=cre;
  210. DLLFuncs[NumDLLFuncs].idstring=inf;
  211. DLLFuncs[NumDLLFuncs].is_r2=is_r2;
  212. NumDLLFuncs++;
  213. }
  214. static APEinfo ext_info=
  215. {
  216. 3,
  217. 0,
  218. &g_line_blend_mode,
  219. NSEEL_VM_alloc,
  220. NSEEL_VM_free,
  221. AVS_EEL_IF_resetvars,
  222. NSEEL_VM_regvar,
  223. AVS_EEL_IF_Compile,
  224. AVS_EEL_IF_Execute,
  225. NSEEL_code_free,
  226. compilerfunctionlist,
  227. getGlobalBuffer,
  228. };
  229. void C_RLibrary::initdll()
  230. {
  231. ext_info.global_registers=NSEEL_getglobalregs();
  232. HANDLE h;
  233. WIN32_FIND_DATA d;
  234. char dirmask[MAX_PATH*2];
  235. #ifdef LASER
  236. wsprintf(dirmask,"%s\\*.lpe",g_path);
  237. #else
  238. wsprintf(dirmask,"%s\\*.ape",g_path);
  239. #endif
  240. h = FindFirstFile(dirmask,&d);
  241. if (h != INVALID_HANDLE_VALUE)
  242. {
  243. do {
  244. char s[MAX_PATH];
  245. HINSTANCE hlib;
  246. wsprintf(s,"%s\\%s",g_path,d.cFileName);
  247. hlib=LoadLibrary(s);
  248. if (hlib)
  249. {
  250. int cre;
  251. char *inf;
  252. void (*sei)(HINSTANCE hDllInstance, APEinfo *ptr);
  253. *(void**)&sei = (void *) GetProcAddress(hlib,"_AVS_APE_SetExtInfo");
  254. if (sei)
  255. sei(hlib,&ext_info);
  256. #ifdef LASER
  257. int (*retr)(HINSTANCE hDllInstance, char **info, int *create, C_LineListBase *linelist);
  258. retr = (int (*)(HINSTANCE, char ** ,int *, C_LineListBase*)) GetProcAddress(hlib,"_AVS_LPE_RetrFunc");
  259. if (retr && retr(hlib,&inf,&cre,g_laser_linelist))
  260. {
  261. _add_dll(hlib,(class C_RBASE *(__cdecl *)(char *))cre,inf,0);
  262. }
  263. else FreeLibrary(hlib);
  264. #else
  265. int (*retr)(HINSTANCE hDllInstance, char **info, int *create);
  266. retr = (int (*)(HINSTANCE, char ** ,int *)) GetProcAddress(hlib,"_AVS_APE_RetrFuncEXT2");
  267. if (retr && retr(hlib,&inf,&cre))
  268. {
  269. _add_dll(hlib,(class C_RBASE *(__cdecl *)(char *))cre,inf,1);
  270. }
  271. else
  272. {
  273. retr = (int (*)(HINSTANCE, char ** ,int *)) GetProcAddress(hlib,"_AVS_APE_RetrFunc");
  274. if (retr && retr(hlib,&inf,&cre))
  275. {
  276. _add_dll(hlib,(class C_RBASE *(__cdecl *)(char *))cre,inf,0);
  277. }
  278. else FreeLibrary(hlib);
  279. }
  280. #endif
  281. }
  282. } while (FindNextFile(h,&d));
  283. FindClose(h);
  284. }
  285. }
  286. int C_RLibrary::GetRendererDesc(int which, char *str)
  287. {
  288. *str=0;
  289. if (which >= 0 && which < NumRetrFuncs)
  290. {
  291. RetrFuncs[which].rf(str);
  292. return 1;
  293. }
  294. if (which >= DLLRENDERBASE)
  295. {
  296. which-=DLLRENDERBASE;
  297. if (which < NumDLLFuncs)
  298. {
  299. DLLFuncs[which].createfunc(str);
  300. return (int)DLLFuncs[which].idstring;
  301. }
  302. }
  303. return 0;
  304. }
  305. C_RBASE *C_RLibrary::CreateRenderer(int *which, int *has_r2)
  306. {
  307. if (has_r2) *has_r2=0;
  308. if (*which >= 0 && *which < NumRetrFuncs)
  309. {
  310. if (has_r2) *has_r2 = RetrFuncs[*which].is_r2;
  311. return RetrFuncs[*which].rf(NULL);
  312. }
  313. if (*which == LIST_ID)
  314. return (C_RBASE *) new C_RenderListClass();
  315. if (*which >= DLLRENDERBASE)
  316. {
  317. int x;
  318. char *p=(char *)*which;
  319. for (x = 0; x < NumDLLFuncs; x ++)
  320. {
  321. if (!DLLFuncs[x].createfunc) break;
  322. if (DLLFuncs[x].idstring)
  323. {
  324. if (!strncmp(p,DLLFuncs[x].idstring,32))
  325. {
  326. *which=(int)DLLFuncs[x].idstring;
  327. return DLLFuncs[x].createfunc(NULL);
  328. }
  329. }
  330. }
  331. for (x = 0; x < sizeof(NamedApeToBuiltinTrans)/sizeof(NamedApeToBuiltinTrans[0]); x ++)
  332. {
  333. if (!strncmp(p,NamedApeToBuiltinTrans[x].id,32))
  334. {
  335. *which=NamedApeToBuiltinTrans[x].newidx;
  336. if (has_r2) *has_r2 = RetrFuncs[*which].is_r2;
  337. return RetrFuncs[*which].rf(NULL);
  338. }
  339. }
  340. }
  341. int r=*which;
  342. *which=UNKN_ID;
  343. C_UnknClass *p=new C_UnknClass();
  344. p->SetID(r,(r >= DLLRENDERBASE)?(char*)r:"");
  345. return (C_RBASE *)p;
  346. }
  347. C_RLibrary::C_RLibrary()
  348. {
  349. DLLFuncs=NULL;
  350. NumDLLFuncs=0;
  351. RetrFuncs=0;
  352. NumRetrFuncs=0;
  353. initfx();
  354. initdll();
  355. initbuiltinape();
  356. }
  357. C_RLibrary::~C_RLibrary()
  358. {
  359. if (RetrFuncs) GlobalFree(RetrFuncs);
  360. RetrFuncs=0;
  361. NumRetrFuncs=0;
  362. if (DLLFuncs)
  363. {
  364. int x;
  365. for (x = 0; x < NumDLLFuncs; x ++)
  366. {
  367. if (DLLFuncs[x].hDllInstance)
  368. FreeLibrary(DLLFuncs[x].hDllInstance);
  369. }
  370. GlobalFree(DLLFuncs);
  371. }
  372. DLLFuncs=NULL;
  373. NumDLLFuncs=0;
  374. }
  375. HINSTANCE C_RLibrary::GetRendererInstance(int which, HINSTANCE hThisInstance)
  376. {
  377. if (which < DLLRENDERBASE || which == UNKN_ID || which == LIST_ID) return hThisInstance;
  378. int x;
  379. char *p=(char *)which;
  380. for (x = 0; x < NumDLLFuncs; x ++)
  381. {
  382. if (DLLFuncs[x].idstring)
  383. {
  384. if (!strncmp(p,DLLFuncs[x].idstring,32))
  385. {
  386. if (DLLFuncs[x].hDllInstance)
  387. return DLLFuncs[x].hDllInstance;
  388. break;
  389. }
  390. }
  391. }
  392. return hThisInstance;
  393. }
  394. void *g_n_buffers[NBUF];
  395. int g_n_buffers_w[NBUF],g_n_buffers_h[NBUF];
  396. void *getGlobalBuffer(int w, int h, int n, int do_alloc)
  397. {
  398. if (n < 0 || n >= NBUF) return 0;
  399. if (!g_n_buffers[n] || g_n_buffers_w[n] != w || g_n_buffers_h[n] != h)
  400. {
  401. if (g_n_buffers[n]) GlobalFree(g_n_buffers[n]);
  402. if (do_alloc)
  403. {
  404. g_n_buffers_w[n]=w;
  405. g_n_buffers_h[n]=h;
  406. return g_n_buffers[n]=GlobalAlloc(GPTR,sizeof(int)*w*h);
  407. }
  408. g_n_buffers[n]=NULL;
  409. g_n_buffers_w[n]=0;
  410. g_n_buffers_h[n]=0;
  411. return 0;
  412. }
  413. return g_n_buffers[n];
  414. }