r_videodelay.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. // video delay
  25. // copyright tom holden, 2002
  26. // mail: [email protected]
  27. #include <windows.h>
  28. #include "resource.h"
  29. #include "r_defs.h"
  30. #include "../Agave/Language/api_language.h"
  31. #ifndef LASER
  32. #define MOD_NAME "Trans / Video Delay"
  33. #define C_DELAY C_VideoDelayClass
  34. class C_DELAY : public C_RBASE
  35. {
  36. protected:
  37. public:
  38. // standard ape members
  39. C_DELAY();
  40. virtual ~C_DELAY();
  41. virtual int render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h);
  42. virtual HWND conf(HINSTANCE hInstance, HWND hwndParent);
  43. virtual char *get_desc();
  44. virtual void load_config(unsigned char *data, int len);
  45. virtual int save_config(unsigned char *data);
  46. // saved members
  47. bool enabled;
  48. bool usebeats;
  49. unsigned int delay;
  50. // unsaved members
  51. LPVOID buffer;
  52. LPVOID inoutpos;
  53. unsigned long buffersize;
  54. unsigned long virtualbuffersize;
  55. unsigned long oldvirtualbuffersize;
  56. unsigned long framessincebeat;
  57. unsigned long framedelay;
  58. unsigned long framemem;
  59. unsigned long oldframemem;
  60. };
  61. // global configuration dialog pointer
  62. static C_DELAY *g_Delay;
  63. // global DLL instance pointer
  64. static HINSTANCE g_hDllInstance;
  65. // configuration screen
  66. static BOOL CALLBACK g_DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  67. {
  68. char value[16];
  69. int val;
  70. unsigned int objectcode, objectmessage;
  71. HWND hwndEdit;
  72. switch (uMsg)
  73. {
  74. case WM_INITDIALOG: //init
  75. CheckDlgButton(hwndDlg,IDC_CHECK1,g_Delay->enabled);
  76. CheckDlgButton(hwndDlg,IDC_RADIO1,g_Delay->usebeats);
  77. CheckDlgButton(hwndDlg,IDC_RADIO2,!g_Delay->usebeats);
  78. hwndEdit = GetDlgItem(hwndDlg,IDC_EDIT1);
  79. _itoa(g_Delay->delay,value,10);
  80. SetWindowText(hwndEdit,value);
  81. return 1;
  82. case WM_COMMAND:
  83. objectcode = LOWORD(wParam);
  84. objectmessage = HIWORD(wParam);
  85. // see if enable checkbox is checked
  86. if (objectcode == IDC_CHECK1)
  87. {
  88. g_Delay->enabled = IsDlgButtonChecked(hwndDlg,IDC_CHECK1)==1;
  89. return 0;
  90. }
  91. // see if beats radiobox is checked
  92. if (objectcode == IDC_RADIO1)
  93. {
  94. if(IsDlgButtonChecked(hwndDlg,IDC_RADIO1)==1)
  95. {
  96. g_Delay->usebeats = true;
  97. CheckDlgButton(hwndDlg,IDC_RADIO2,BST_UNCHECKED);
  98. g_Delay->framedelay = 0;
  99. g_Delay->framessincebeat = 0;
  100. hwndEdit = GetDlgItem(hwndDlg,IDC_EDIT1); //new
  101. if (g_Delay->delay>16) { //new
  102. g_Delay->delay = 16; //new
  103. SetWindowText(hwndEdit,"16"); //new
  104. } //new
  105. }
  106. else g_Delay->usebeats = false;
  107. return 0;
  108. }
  109. // see if frames radiobox is checked
  110. if (objectcode == IDC_RADIO2)
  111. {
  112. if(IsDlgButtonChecked(hwndDlg,IDC_RADIO2)==1)
  113. {
  114. g_Delay->usebeats = false;
  115. CheckDlgButton(hwndDlg,IDC_RADIO1,BST_UNCHECKED);
  116. g_Delay->framedelay = g_Delay->delay;
  117. }
  118. else g_Delay->usebeats = true;
  119. return 0;
  120. }
  121. //get and put data from the delay box
  122. if (objectcode == IDC_EDIT1)
  123. {
  124. hwndEdit = GetDlgItem(hwndDlg,IDC_EDIT1);
  125. if (objectmessage == EN_CHANGE)
  126. {
  127. GetWindowText(hwndEdit,value,16);
  128. val = atoi(value);
  129. if (g_Delay->usebeats) {if (val > 16) val = 16;} //new
  130. else {if (val > 200) val = 200;} //new
  131. g_Delay->delay = val;
  132. g_Delay->framedelay = g_Delay->usebeats?0:g_Delay->delay;
  133. }
  134. else if (objectmessage == EN_KILLFOCUS)
  135. {
  136. _itoa(g_Delay->delay,value,10);
  137. SetWindowText(hwndEdit,value);
  138. }
  139. return 0;
  140. }
  141. }
  142. return 0;
  143. }
  144. // set up default configuration
  145. C_DELAY::C_DELAY()
  146. {
  147. // enable
  148. enabled = true;
  149. usebeats = false;
  150. delay = 10;
  151. framedelay = 10;
  152. framessincebeat = 0;
  153. buffersize = 1;
  154. virtualbuffersize = 1;
  155. oldvirtualbuffersize = 1;
  156. buffer = VirtualAlloc(NULL,buffersize,MEM_COMMIT,PAGE_READWRITE);
  157. inoutpos = buffer;
  158. }
  159. // virtual destructor
  160. C_DELAY::~C_DELAY()
  161. {
  162. VirtualFree(buffer,buffersize,MEM_DECOMMIT);
  163. }
  164. // RENDER FUNCTION:
  165. // render should return 0 if it only used framebuffer, or 1 if the new output data is in fbout
  166. // w and h are the-width and height of the screen, in pixels.
  167. // isBeat is 1 if a beat has been detected.
  168. // visdata is in the format of [spectrum:0,wave:1][channel][band].
  169. int C_DELAY::render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h)
  170. {
  171. if (isBeat&0x80000000) return 0;
  172. framemem = w*h*4;
  173. if (usebeats)
  174. {
  175. if (isBeat)
  176. {
  177. framedelay = framessincebeat*delay; //changed
  178. if (framedelay > 400) framedelay = 400; //new
  179. framessincebeat = 0;
  180. }
  181. framessincebeat++;
  182. }
  183. if (enabled && framedelay!=0)
  184. {
  185. virtualbuffersize = framedelay*framemem;
  186. if (framemem == oldframemem)
  187. {
  188. if (virtualbuffersize != oldvirtualbuffersize)
  189. {
  190. if (virtualbuffersize > oldvirtualbuffersize)
  191. {
  192. if (virtualbuffersize > buffersize)
  193. {
  194. // allocate new memory
  195. if (!VirtualFree(buffer,buffersize,MEM_DECOMMIT)) return 0;
  196. if (usebeats)
  197. {
  198. buffersize = 2*virtualbuffersize;
  199. if (buffersize > framemem*400) buffersize = framemem*400; //new
  200. buffer = VirtualAlloc(NULL,buffersize,MEM_COMMIT,PAGE_READWRITE);
  201. if (buffer == NULL)
  202. {
  203. buffersize = virtualbuffersize;
  204. buffer = VirtualAlloc(NULL,buffersize,MEM_COMMIT,PAGE_READWRITE);
  205. }
  206. }
  207. else
  208. {
  209. buffersize = virtualbuffersize;
  210. buffer = VirtualAlloc(NULL,buffersize,MEM_COMMIT,PAGE_READWRITE);
  211. }
  212. inoutpos = buffer;
  213. if (buffer == NULL)
  214. {
  215. framedelay = 0;
  216. framessincebeat = 0;
  217. return 0;
  218. }
  219. }
  220. else
  221. {
  222. unsigned long size = (((unsigned long)buffer)+oldvirtualbuffersize) - ((unsigned long)inoutpos);
  223. unsigned long l = ((unsigned long)buffer)+virtualbuffersize;
  224. unsigned long d = l - size;
  225. MoveMemory((LPVOID)d, inoutpos, size);
  226. for (l = (unsigned long)inoutpos; l < d; l += framemem) CopyMemory((LPVOID)l,(LPVOID)d,framemem);
  227. }
  228. }
  229. else
  230. { // virtualbuffersize < oldvirtualbuffersize
  231. unsigned long presegsize = ((unsigned long)inoutpos)-((unsigned long)buffer)+framemem;
  232. if (presegsize > virtualbuffersize)
  233. {
  234. MoveMemory(buffer,(LPVOID)(((unsigned long)buffer)+presegsize-virtualbuffersize),virtualbuffersize);
  235. inoutpos = (LPVOID)(((unsigned long)buffer)+virtualbuffersize-framemem);
  236. }
  237. else if (presegsize < virtualbuffersize) MoveMemory((LPVOID)(((unsigned long)inoutpos)+framemem),(LPVOID)(((unsigned long)buffer)+oldvirtualbuffersize+presegsize-virtualbuffersize),virtualbuffersize-presegsize);
  238. }
  239. oldvirtualbuffersize = virtualbuffersize;
  240. }
  241. }
  242. else
  243. {
  244. // allocate new memory
  245. if (!VirtualFree(buffer,buffersize,MEM_DECOMMIT)) return 0;
  246. if (usebeats)
  247. {
  248. buffersize = 2*virtualbuffersize;
  249. buffer = VirtualAlloc(NULL,buffersize,MEM_COMMIT,PAGE_READWRITE);
  250. if (buffer == NULL)
  251. {
  252. buffersize = virtualbuffersize;
  253. buffer = VirtualAlloc(NULL,buffersize,MEM_COMMIT,PAGE_READWRITE);
  254. }
  255. }
  256. else
  257. {
  258. buffersize = virtualbuffersize;
  259. buffer = VirtualAlloc(NULL,buffersize,MEM_COMMIT,PAGE_READWRITE);
  260. }
  261. inoutpos = buffer;
  262. if (buffer == NULL)
  263. {
  264. framedelay = 0;
  265. framessincebeat = 0;
  266. return 0;
  267. }
  268. oldvirtualbuffersize = virtualbuffersize;
  269. }
  270. oldframemem = framemem;
  271. CopyMemory(fbout,inoutpos,framemem);
  272. CopyMemory(inoutpos,framebuffer,framemem);
  273. inoutpos = (LPVOID)(((unsigned long)inoutpos)+framemem);
  274. if ((unsigned long)inoutpos>=((unsigned long)buffer)+virtualbuffersize) inoutpos = buffer;
  275. return 1;
  276. }
  277. else return 0;
  278. }
  279. HWND C_DELAY::conf(HINSTANCE hInstance, HWND hwndParent) // return NULL if no config dialog possible
  280. {
  281. g_Delay = this;
  282. return WASABI_API_CREATEDIALOG(IDD_CFG_VIDEODELAY,hwndParent,g_DlgProc);
  283. }
  284. char *C_DELAY::get_desc(void)
  285. {
  286. static char desc[128]; return (!desc[0]?WASABI_API_LNGSTRING_BUF(IDS_TRANS_VIDEO_DELAY,desc,128):desc);
  287. }
  288. // load_/save_config are called when saving and loading presets (.avs files)
  289. #define GET_INT() (data[pos]|(data[pos+1]<<8)|(data[pos+2]<<16)|(data[pos+3]<<24))
  290. void C_DELAY::load_config(unsigned char *data, int len) // read configuration of max length "len" from data.
  291. {
  292. int pos=0;
  293. // always ensure there is data to be loaded
  294. if (len-pos >= 4)
  295. {
  296. // load activation toggle
  297. enabled=(GET_INT()==1);
  298. pos+=4;
  299. }
  300. if (len-pos >= 4)
  301. {
  302. // load beats toggle
  303. usebeats=(GET_INT()==1);
  304. pos+=4;
  305. }
  306. if (len-pos >= 4)
  307. {
  308. // load delay
  309. delay=GET_INT();
  310. if (usebeats) {if (delay > 16) delay = 16;} //new
  311. else {if (delay > 200) delay = 200;} //new
  312. pos+=4;
  313. }
  314. }
  315. // write configuration to data, return length. config data should not exceed 64k.
  316. #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
  317. int C_DELAY::save_config(unsigned char *data)
  318. {
  319. int pos=0;
  320. PUT_INT((int)enabled);
  321. pos+=4;
  322. PUT_INT((int)usebeats);
  323. pos+=4;
  324. PUT_INT((unsigned int)delay);
  325. pos+=4;
  326. return pos;
  327. }
  328. // export stuff
  329. C_RBASE *R_VideoDelay(char *desc) // creates a new effect object if desc is NULL, otherwise fills in desc with description
  330. {
  331. if (desc)
  332. {
  333. strcpy(desc,MOD_NAME);
  334. return NULL;
  335. }
  336. return (C_RBASE *) new C_DELAY();
  337. }
  338. #endif