r_avi.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 <stdlib.h>
  26. #include <vfw.h>
  27. #include <commctrl.h>
  28. #include "resource.h"
  29. #include "r_defs.h"
  30. #include "../Agave/Language/api_language.h"
  31. #ifndef LASER
  32. #define MOD_NAME "Render / AVI"
  33. #define C_THISCLASS C_AVIClass
  34. class C_THISCLASS : public C_RBASE {
  35. protected:
  36. public:
  37. C_THISCLASS();
  38. void reinit(int, int);
  39. void loadAvi(char *name);
  40. void closeAvi(void);
  41. virtual ~C_THISCLASS();
  42. virtual int render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h);
  43. virtual char *get_desc() { static char desc[128]; return (!desc[0]?WASABI_API_LNGSTRING_BUF(IDS_RENDER_AVI,desc,128):desc); }
  44. virtual HWND conf(HINSTANCE hInstance, HWND hwndParent);
  45. virtual void load_config(unsigned char *data, int len);
  46. virtual int save_config(unsigned char *data);
  47. int enabled;
  48. char ascName[MAX_PATH];
  49. int lastWidth, lastHeight;
  50. HDRAWDIB hDrawDib;
  51. PAVISTREAM PAVIVideo;
  52. PGETFRAME PgetFrame;
  53. HBITMAP hRetBitmap;
  54. HBITMAP hOldBitmap;
  55. HDC hDesktopDC;
  56. HDC hBitmapDC;
  57. LPBITMAPINFOHEADER lpFrame;
  58. BITMAPINFO bi;
  59. int blend, blendavg, adapt, persist;
  60. int loaded, rendering;
  61. int lFrameIndex;
  62. int length;
  63. unsigned int speed;
  64. unsigned int lastspeed;
  65. int *old_image,old_image_w,old_image_h;
  66. };
  67. static C_THISCLASS *g_ConfigThis; // global configuration dialog pointer
  68. static HINSTANCE g_hDllInstance; // global DLL instance pointer (not needed in this example, but could be useful)
  69. // configuration read/write
  70. C_THISCLASS::C_THISCLASS() // set up default configuration
  71. {
  72. AVIFileInit ( ) ;
  73. hDrawDib = DrawDibOpen ( ) ;
  74. lastWidth=0;
  75. lastHeight=0;
  76. lFrameIndex=0;
  77. loaded=0;
  78. rendering=0;
  79. length=0;
  80. blend=0;
  81. adapt=0;
  82. blendavg=1;
  83. persist=6;
  84. enabled=1;
  85. speed=0;
  86. lastspeed=0;
  87. old_image=NULL; old_image_h=0; old_image_w=0;
  88. }
  89. C_THISCLASS::~C_THISCLASS()
  90. {
  91. closeAvi();
  92. SelectObject (hBitmapDC, hOldBitmap);
  93. DeleteDC (hBitmapDC);
  94. ReleaseDC (NULL, hDesktopDC);
  95. AVIFileExit ( );
  96. DrawDibClose (hDrawDib);
  97. if(old_image) {
  98. GlobalFree(old_image);
  99. old_image=NULL;
  100. old_image_h=old_image_w=0;
  101. }
  102. }
  103. void C_THISCLASS::loadAvi(char *name)
  104. {
  105. char pathfile[MAX_PATH];
  106. if (loaded) closeAvi();
  107. wsprintf(pathfile,"%s\\%s",g_path, name);
  108. if (AVIStreamOpenFromFile ((PAVISTREAM FAR *) &PAVIVideo, pathfile, streamtypeVIDEO, 0, OF_READ | OF_SHARE_EXCLUSIVE, NULL) != 0)
  109. {
  110. // MessageBox(NULL, "An error occured while trying to open a file. Effect is disabled", "Warning", 0);
  111. return;
  112. }
  113. PgetFrame = AVIStreamGetFrameOpen (PAVIVideo, NULL);
  114. length = AVIStreamLength(PAVIVideo);
  115. lFrameIndex = 0;
  116. lpFrame = (LPBITMAPINFOHEADER) AVIStreamGetFrame (PgetFrame, 0);
  117. reinit(lastWidth, lastHeight);
  118. loaded=1;
  119. }
  120. void C_THISCLASS::closeAvi(void)
  121. {
  122. if (loaded)
  123. {
  124. while (rendering);
  125. loaded=0;
  126. AVIStreamGetFrameClose (PgetFrame);
  127. AVIStreamRelease (PAVIVideo);
  128. }
  129. }
  130. #define GET_INT() (data[pos]|(data[pos+1]<<8)|(data[pos+2]<<16)|(data[pos+3]<<24))
  131. void C_THISCLASS::load_config(unsigned char *data, int len) // read configuration of max length "len" from data.
  132. {
  133. int pos=0;
  134. char *p=ascName;
  135. if (len-pos >= 4) { enabled=GET_INT(); pos+=4; }
  136. if (len-pos >= 4) { blend=GET_INT(); pos+=4; }
  137. if (len-pos >= 4) { blendavg=GET_INT(); pos+=4; }
  138. while (data[pos] && len-pos > 0 && p-ascName < sizeof(ascName)-1) *p++=data[pos++];
  139. *p=0; pos++;
  140. if (len-pos >= 4) { adapt=GET_INT(); pos+=4; }
  141. if (len-pos >= 4) { persist=GET_INT(); pos+=4; }
  142. if (len-pos >= 4) { speed=GET_INT(); pos+=4; }
  143. if (*ascName)
  144. loadAvi(ascName);
  145. }
  146. #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
  147. int C_THISCLASS::save_config(unsigned char *data) // write configuration to data, return length. config data should not exceed 64k.
  148. {
  149. int pos=0;
  150. PUT_INT(enabled); pos+=4;
  151. PUT_INT(blend); pos+=4;
  152. PUT_INT(blendavg); pos+=4;
  153. strcpy((char *)data+pos, ascName);
  154. pos+=strlen(ascName)+1;
  155. PUT_INT(adapt); pos+=4;
  156. PUT_INT(persist); pos+=4;
  157. PUT_INT(speed); pos+=4;
  158. return pos;
  159. }
  160. void C_THISCLASS::reinit(int w, int h)
  161. {
  162. if (lastWidth || lastHeight)
  163. {
  164. SelectObject (hBitmapDC, hOldBitmap);
  165. DeleteDC (hBitmapDC);
  166. ReleaseDC (NULL, hDesktopDC);
  167. }
  168. hDesktopDC = GetDC (NULL);
  169. hRetBitmap = CreateCompatibleBitmap (hDesktopDC, w, h);
  170. hBitmapDC = CreateCompatibleDC (hDesktopDC);
  171. hOldBitmap = (HBITMAP) SelectObject (hBitmapDC, hRetBitmap);
  172. bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  173. bi.bmiHeader.biWidth = w;
  174. bi.bmiHeader.biHeight = h;
  175. bi.bmiHeader.biPlanes = 1;
  176. bi.bmiHeader.biBitCount = 32;
  177. bi.bmiHeader.biCompression = BI_RGB;
  178. bi.bmiHeader.biSizeImage = 0;
  179. bi.bmiHeader.biXPelsPerMeter = 0;
  180. bi.bmiHeader.biYPelsPerMeter = 0;
  181. bi.bmiHeader.biClrUsed = 0;
  182. bi.bmiHeader.biClrImportant = 0;
  183. }
  184. // render function
  185. // render should return 0 if it only used framebuffer, or 1 if the new output data is in fbout. this is
  186. // used when you want to do something that you'd otherwise need to make a copy of the framebuffer.
  187. // w and h are the width and height of the screen, in pixels.
  188. // isBeat is 1 if a beat has been detected.
  189. // visdata is in the format of [spectrum:0,wave:1][channel][band].
  190. int C_THISCLASS::render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h)
  191. {
  192. int *p,*d;
  193. int i,j;
  194. static int persistCount=0;
  195. if (!enabled || !loaded) return 0;
  196. if(h!=old_image_h||w!=old_image_w) {
  197. if(old_image)
  198. GlobalFree(old_image);
  199. old_image=(int *)GlobalAlloc(GMEM_FIXED,sizeof(int)*w*h);
  200. old_image_h=h; old_image_w=w;
  201. }
  202. if((lastspeed+speed)>GetTickCount()) {
  203. memcpy(fbout,old_image,sizeof(int)*w*h);
  204. } else {
  205. lastspeed=GetTickCount();
  206. rendering=1;
  207. if (lastWidth != w || lastHeight != h)
  208. {
  209. lastWidth = w;
  210. lastHeight = h;
  211. reinit(w, h);
  212. }
  213. if (isBeat&0x80000000) return 0;
  214. if (!length) return 0;
  215. lFrameIndex %= length;
  216. lpFrame = (LPBITMAPINFOHEADER) AVIStreamGetFrame (PgetFrame, lFrameIndex++);
  217. DrawDibDraw (hDrawDib, hBitmapDC, 0, 0, lastWidth, lastHeight, lpFrame,
  218. NULL, 0, 0, (int) lpFrame ->biWidth, (int) lpFrame ->biHeight, 0);
  219. GetDIBits(hBitmapDC, hRetBitmap, 0, h, (void *)fbout, &bi, DIB_RGB_COLORS);
  220. rendering=0;
  221. memcpy(old_image,fbout,sizeof(int)*w*h);
  222. }
  223. if (isBeat)
  224. persistCount=persist;
  225. else
  226. if (persistCount>0) persistCount--;
  227. p = fbout;
  228. d = framebuffer+w*(h-1);
  229. if (blend || (adapt && (isBeat || persistCount)))
  230. for (i=0;i<h;i++)
  231. {
  232. for (j=0;j<w;j++)
  233. {
  234. *d=BLEND(*p, *d);
  235. d++;
  236. p++;
  237. }
  238. d -= w*2;
  239. }
  240. else
  241. if (blendavg || adapt)
  242. for (i=0;i<h;i++)
  243. {
  244. for (j=0;j<w;j++)
  245. {
  246. *d=BLEND_AVG(*p, *d);
  247. d++;
  248. p++;
  249. }
  250. d -= w*2;
  251. }
  252. else
  253. for (i=0;i<h;i++)
  254. {
  255. memcpy(d, p, w*4);
  256. p+=w;
  257. d-=w;
  258. }
  259. return 0;
  260. }
  261. // configuration dialog stuff
  262. static void EnableWindows(HWND hwndDlg)
  263. {
  264. EnableWindow(GetDlgItem(hwndDlg,IDC_PERSIST),g_ConfigThis->adapt);
  265. EnableWindow(GetDlgItem(hwndDlg,IDC_PERSIST_TITLE),g_ConfigThis->adapt);
  266. }
  267. static BOOL CALLBACK g_DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  268. {
  269. switch (uMsg)
  270. {
  271. case WM_INITDIALOG:
  272. SendDlgItemMessage(hwndDlg, IDC_PERSIST, TBM_SETRANGE, TRUE, MAKELONG(0, 32));
  273. SendDlgItemMessage(hwndDlg, IDC_PERSIST, TBM_SETPOS, TRUE, g_ConfigThis->persist);
  274. SendDlgItemMessage(hwndDlg, IDC_SPEED, TBM_SETTICFREQ, 50, 0);
  275. SendDlgItemMessage(hwndDlg, IDC_SPEED, TBM_SETRANGE, TRUE, MAKELONG(0, 1000));
  276. SendDlgItemMessage(hwndDlg, IDC_SPEED, TBM_SETPOS, TRUE, g_ConfigThis->speed);
  277. if (g_ConfigThis->enabled) CheckDlgButton(hwndDlg,IDC_CHECK1,BST_CHECKED);
  278. if (g_ConfigThis->blend) CheckDlgButton(hwndDlg,IDC_ADDITIVE,BST_CHECKED);
  279. if (g_ConfigThis->blendavg) CheckDlgButton(hwndDlg,IDC_5050,BST_CHECKED);
  280. if (g_ConfigThis->adapt) CheckDlgButton(hwndDlg,IDC_ADAPT,BST_CHECKED);
  281. if (!g_ConfigThis->adapt && !g_ConfigThis->blend && !g_ConfigThis->blendavg)
  282. CheckDlgButton(hwndDlg,IDC_REPLACE,BST_CHECKED);
  283. EnableWindows(hwndDlg);
  284. loadComboBox(GetDlgItem(hwndDlg,OBJ_COMBO),"*.AVI",g_ConfigThis->ascName);
  285. return 1;
  286. case WM_NOTIFY:
  287. {
  288. if (LOWORD(wParam) == IDC_PERSIST)
  289. g_ConfigThis->persist = SendDlgItemMessage(hwndDlg, IDC_PERSIST, TBM_GETPOS, 0, 0);
  290. if (LOWORD(wParam) == IDC_SPEED)
  291. g_ConfigThis->speed = SendDlgItemMessage(hwndDlg, IDC_SPEED, TBM_GETPOS, 0, 0);
  292. }
  293. case WM_COMMAND:
  294. if ((LOWORD(wParam) == IDC_CHECK1) ||
  295. (LOWORD(wParam) == IDC_ADDITIVE) ||
  296. (LOWORD(wParam) == IDC_REPLACE) ||
  297. (LOWORD(wParam) == IDC_ADAPT) ||
  298. (LOWORD(wParam) == IDC_5050) )
  299. {
  300. g_ConfigThis->enabled=IsDlgButtonChecked(hwndDlg,IDC_CHECK1)?1:0;
  301. g_ConfigThis->blend=IsDlgButtonChecked(hwndDlg,IDC_ADDITIVE)?1:0;
  302. g_ConfigThis->blendavg=IsDlgButtonChecked(hwndDlg,IDC_5050)?1:0;
  303. g_ConfigThis->adapt=IsDlgButtonChecked(hwndDlg,IDC_ADAPT)?1:0;
  304. EnableWindows(hwndDlg);
  305. }
  306. if (HIWORD(wParam) == CBN_SELCHANGE && LOWORD(wParam) == OBJ_COMBO) // handle clicks to combo box
  307. {
  308. int sel = SendDlgItemMessage(hwndDlg, OBJ_COMBO, CB_GETCURSEL, 0, 0);
  309. if (sel != -1)
  310. {
  311. SendDlgItemMessage(hwndDlg, OBJ_COMBO, CB_GETLBTEXT, sel, (LPARAM)g_ConfigThis->ascName);
  312. if (*(g_ConfigThis->ascName))
  313. g_ConfigThis->loadAvi(g_ConfigThis->ascName);
  314. }
  315. }
  316. return 0;
  317. }
  318. return 0;
  319. }
  320. HWND C_THISCLASS::conf(HINSTANCE hInstance, HWND hwndParent) // return NULL if no config dialog possible
  321. {
  322. g_ConfigThis = this;
  323. return WASABI_API_CREATEDIALOG(IDD_CFG_AVI,hwndParent,g_DlgProc);
  324. }
  325. // export stuff
  326. C_RBASE *R_AVI(char *desc) // creates a new effect object if desc is NULL, otherwise fills in desc with description
  327. {
  328. if (desc) { strcpy(desc,MOD_NAME); return NULL; }
  329. return (C_RBASE *) new C_THISCLASS();
  330. }
  331. #else//!LASER
  332. C_RBASE *R_AVI(char *desc) { return NULL; }
  333. #endif