1 #include "PresetFrameIO.hpp"
2 #include "wipemalloc.h"
3 #include <math.h>
4 #include <cassert>
5 #include <iostream>
6 #include <cmath>
7 #include "Renderer/BeatDetect.hpp"
8 
PresetInputs()9 PresetInputs::PresetInputs() : PipelineContext()
10 {
11 }
12 
update(const BeatDetect & music,const PipelineContext & context)13 void PresetInputs::update(const BeatDetect & music, const PipelineContext & context) {
14 
15     // Reflect new values form the beat detection unit
16     this->bass = music.bass;
17     this->mid = music.mid;
18     this->treb = music.treb;
19     this->bass_att = music.bass_att;
20     this->mid_att = music.mid_att;
21     this->treb_att = music.treb_att;
22 
23     // Reflect new values from the pipeline context
24     this->fps = context.fps;
25     this->time = context.time;
26 
27     this->frame = context.frame;
28     this->progress = context.progress;
29 }
30 
Initialize(int gx,int gy)31 void PresetInputs::Initialize ( int gx, int gy )
32 {
33 	int x, y;
34 
35 	this->gx =gx;
36 	this->gy= gy;
37 
38 
39 	/// @bug no clue if this block belongs here
40 	// ***
41 	progress = 0;
42 	frame = 1;
43 
44 	x_per_pixel = 0;
45 	y_per_pixel = 0;
46 	rad_per_pixel = 0;
47 	ang_per_pixel = 0;
48 	// ***
49 
50 	this->x_mesh= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
51 	for ( x = 0; x < gx; x++ )
52 	{
53 		this->x_mesh[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
54 	}
55 	this->y_mesh= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
56 	for ( x = 0; x <gx; x++ )
57 	{
58 		this->y_mesh[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
59 	}
60 	this->rad_mesh= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
61 	for ( x = 0; x < gx; x++ )
62 	{
63 		this->rad_mesh[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
64 	}
65 	this->theta_mesh= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
66 	for ( x = 0; x <gx; x++ )
67 	{
68 		this->theta_mesh[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
69 	}
70 
71 	this->origtheta= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
72 	for ( x = 0; x < gx; x++ )
73 	{
74 		this->origtheta[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
75 	}
76 	this->origrad= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
77 	for ( x = 0; x < gx; x++ )
78 	{
79 		this->origrad[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
80 	}
81 	this->origx= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
82 	for ( x = 0; x < gx; x++ )
83 	{
84 		this->origx[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
85 	}
86 	this->origy= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
87 	for ( x = 0; x < gx; x++ )
88 	{
89 		this->origy[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
90 	}
91 
92 	for ( x=0;x<gx;x++ )
93 	{
94 		for ( y=0;y<gy;y++ )
95 		{
96 			this->origx[x][y]=x/ ( float ) ( gx-1 );
97 			this->origy[x][y]=- ( ( y/ ( float ) ( gy-1 ) )-1 );
98 			this->origrad[x][y]=hypot ( ( this->origx[x][y]-.5 ) *2, ( this->origy[x][y]-.5 ) *2 ) * .7071067;
99 			this->origtheta[x][y]=atan2 ( ( ( this->origy[x][y]-.5 ) *2 ), ( ( this->origx[x][y]-.5 ) *2 ) );
100 		}
101 	}
102 
103 
104 
105 }
106 
PresetOutputs()107 PresetOutputs::PresetOutputs() : Pipeline()
108 {}
109 
~PresetOutputs()110 PresetOutputs::~PresetOutputs()
111 {
112 	assert(this->gx > 0);
113 
114 	for ( int x = 0; x < this->gx; x++ )
115 	{
116 		free(this->sx_mesh[x]);
117 		free(this->sy_mesh[x]);
118 		free(this->dy_mesh[x]);
119 		free(this->dx_mesh[x]);
120 		free(this->cy_mesh[x]);
121 		free(this->cx_mesh[x]);
122 
123 		free(this->warp_mesh[x]);
124 		free(this->zoom_mesh[x]);
125 		free(this->zoomexp_mesh[x]);
126 		free(this->rot_mesh[x]);
127 		free(this->orig_x[x]);
128 		free(this->orig_y[x]);
129 		free(this->rad_mesh[x]);
130 	}
131 
132 		free(this->rad_mesh);
133 		free(this->sx_mesh);
134 		free(this->sy_mesh);
135 		free(this->dy_mesh);
136 		free(this->dx_mesh);
137 		free(this->cy_mesh);
138 		free(this->cx_mesh);
139 		free(this->warp_mesh);
140 		free(this->zoom_mesh);
141 		free(this->zoomexp_mesh);
142 		free(this->rot_mesh);
143 		free(this->orig_x);
144 		free(this->orig_y);
145 
146 }
147 
Render(const BeatDetect & music,const PipelineContext & context)148 void PresetOutputs::Render(const BeatDetect &music, const PipelineContext &context)
149 {
150 	PerPixelMath(context);
151 
152 	drawables.clear();
153 
154 	drawables.push_back(&mv);
155 
156 	for (PresetOutputs::cshape_container::iterator pos = customShapes.begin();
157 			pos != customShapes.end(); ++pos)
158 			{
159 				if( (*pos)->enabled==1)	drawables.push_back((*pos));
160 			}
161 
162 	for (PresetOutputs::cwave_container::iterator pos = customWaves.begin();
163 			pos != customWaves.end(); ++pos)
164 			{
165 				if( (*pos)->enabled==1)	drawables.push_back((*pos));
166 			}
167 
168     	drawables.push_back(&wave);
169 	if(bDarkenCenter==1) drawables.push_back(&darkenCenter);
170 	drawables.push_back(&border);
171 
172 	compositeDrawables.clear();
173 	compositeDrawables.push_back(&videoEcho);
174 
175 	if (bBrighten==1)
176 		compositeDrawables.push_back(&brighten);
177 
178 	if (bDarken==1)
179 		compositeDrawables.push_back(&darken);
180 
181 	if (bSolarize==1)
182 		compositeDrawables.push_back(&solarize);
183 
184 	if (bInvert==1)
185 		compositeDrawables.push_back(&invert);
186 }
187 
188 
PerPixelMath(const PipelineContext & context)189 void PresetOutputs::PerPixelMath(const PipelineContext &context)
190 {
191 
192 	int x, y;
193 	float fZoom2, fZoom2Inv;
194 
195 	for (x = 0; x < gx; x++)
196 	{
197 		for (y = 0; y < gy; y++)
198 		{
199       fZoom2 = std::pow(this->zoom_mesh[x][y], std::pow(this->zoomexp_mesh[x][y],
200 					rad_mesh[x][y] * 2.0f - 1.0f));
201 			fZoom2Inv = 1.0f / fZoom2;
202 			this->x_mesh[x][y] = this->orig_x[x][y] * 0.5f * fZoom2Inv + 0.5f;
203 			this->y_mesh[x][y] = this->orig_y[x][y] * 0.5f * fZoom2Inv + 0.5f;
204 		}
205 	}
206 
207 	for (x = 0; x < gx; x++)
208 	{
209 		for (y = 0; y < gy; y++)
210 		{
211 			this->x_mesh[x][y] = (this->x_mesh[x][y] - this->cx_mesh[x][y])
212 					/ this->sx_mesh[x][y] + this->cx_mesh[x][y];
213 		}
214 	}
215 
216 	for (x = 0; x < gx; x++)
217 	{
218 		for (y = 0; y < gy; y++)
219 		{
220 			this->y_mesh[x][y] = (this->y_mesh[x][y] - this->cy_mesh[x][y])
221 					/ this->sy_mesh[x][y] + this->cy_mesh[x][y];
222 		}
223 	}
224 
225 	float fWarpTime = context.time * this->fWarpAnimSpeed;
226 	float fWarpScaleInv = 1.0f / this->fWarpScale;
227 	float f[4];
228 	f[0] = 11.68f + 4.0f * cosf(fWarpTime * 1.413f + 10);
229 	f[1] = 8.77f + 3.0f * cosf(fWarpTime * 1.113f + 7);
230 	f[2] = 10.54f + 3.0f * cosf(fWarpTime * 1.233f + 3);
231 	f[3] = 11.49f + 4.0f * cosf(fWarpTime * 0.933f + 5);
232 
233 	for (x = 0; x < gx; x++)
234 	{
235 		for (y = 0; y < gy; y++)
236 		{
237 			this->x_mesh[x][y] += this->warp_mesh[x][y] * 0.0035f * sinf(fWarpTime * 0.333f
238 					+ fWarpScaleInv * (this->orig_x[x][y] * f[0] - this->orig_y[x][y] * f[3]));
239 			this->y_mesh[x][y] += this->warp_mesh[x][y] * 0.0035f * cosf(fWarpTime * 0.375f
240 					- fWarpScaleInv * (this->orig_x[x][y] * f[2] + this->orig_y[x][y] * f[1]));
241 			this->x_mesh[x][y] += this->warp_mesh[x][y] * 0.0035f * cosf(fWarpTime * 0.753f
242 					- fWarpScaleInv * (this->orig_x[x][y] * f[1] - this->orig_y[x][y] * f[2]));
243 			this->y_mesh[x][y] += this->warp_mesh[x][y] * 0.0035f * sinf(fWarpTime * 0.825f
244 					+ fWarpScaleInv * (this->orig_x[x][y] * f[0] + this->orig_y[x][y] * f[3]));
245 		}
246 	}
247 	for (x = 0; x < gx; x++)
248 	{
249 		for (y = 0; y < gy; y++)
250 		{
251 			float u2 = this->x_mesh[x][y] - this->cx_mesh[x][y];
252 			float v2 = this->y_mesh[x][y] - this->cy_mesh[x][y];
253 
254 			float cos_rot = cosf(this->rot_mesh[x][y]);
255 			float sin_rot = sinf(this->rot_mesh[x][y]);
256 
257 			this->x_mesh[x][y] = u2 * cos_rot - v2 * sin_rot + this->cx_mesh[x][y];
258 			this->y_mesh[x][y] = u2 * sin_rot + v2 * cos_rot + this->cy_mesh[x][y];
259 
260 		}
261 	}
262 
263 	for (x = 0; x < gx; x++)
264 		for (y = 0; y < gy; y++)
265 			this->x_mesh[x][y] -= this->dx_mesh[x][y];
266 
267 	for (x = 0; x < gx; x++)
268 		for (y = 0; y < gy; y++)
269 			this->y_mesh[x][y] -= this->dy_mesh[x][y];
270 
271 }
272 
273 
Initialize(int gx,int gy)274 void PresetOutputs::Initialize ( int gx, int gy )
275 {
276 
277 	assert(gx > 0);
278 	this->gx = gx;
279 	this->gy= gy;
280 
281 	staticPerPixel = true;
282 	setStaticPerPixel(gx,gy);
283 
284 	assert(this->gx > 0);
285 	int x;
286 	this->x_mesh= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
287 	for ( x = 0; x < gx; x++ )
288 	{
289 		this->x_mesh[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
290 	}
291 	this->y_mesh= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
292 	for ( x = 0; x < gx; x++ )
293 	{
294 		this->y_mesh[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
295 	}
296 	this->sx_mesh= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
297 	for ( x = 0; x < gx; x++ )
298 	{
299 		this->sx_mesh[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
300 	}
301 	this->sy_mesh= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
302 	for ( x = 0; x < gx; x++ )
303 	{
304 		this->sy_mesh[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
305 	}
306 	this->dx_mesh= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
307 	for ( x = 0; x < gx; x++ )
308 	{
309 		this->dx_mesh[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
310 	}
311 	this->dy_mesh= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
312 	for ( x = 0; x < gx; x++ )
313 	{
314 		this->dy_mesh[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
315 	}
316 	this->cx_mesh= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
317 	for ( x = 0; x < gx; x++ )
318 	{
319 		this->cx_mesh[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
320 	}
321 	this->cy_mesh= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
322 	for ( x = 0; x < gx; x++ )
323 	{
324 		this->cy_mesh[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
325 	}
326 	this->zoom_mesh= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
327 	for ( x = 0; x < gx; x++ )
328 	{
329 		this->zoom_mesh[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
330 	}
331 	this->zoomexp_mesh= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
332 	for ( x = 0; x < gx; x++ )
333 	{
334 		this->zoomexp_mesh[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
335 	}
336 	this->rot_mesh= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
337 	for ( x = 0; x < gx; x++ )
338 	{
339 		this->rot_mesh[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
340 	}
341 
342 	this->warp_mesh= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
343 	for ( x = 0; x < gx; x++ )
344 	{
345 		this->warp_mesh[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
346 	}
347 	this->rad_mesh= ( float ** ) wipemalloc ( gx * sizeof ( float * ) );
348 	for ( x = 0; x < gx; x++ )
349 	{
350 		this->rad_mesh[x] = ( float * ) wipemalloc ( gy * sizeof ( float ) );
351 	}
352 	this->orig_x = (float **) wipemalloc(gx * sizeof(float *));
353 	for (x = 0; x < gx; x++)
354 	{
355 		this->orig_x[x] = (float *) wipemalloc(gy * sizeof(float));
356 	}
357 	this->orig_y = (float **) wipemalloc(gx * sizeof(float *));
358 	for (x = 0; x < gx; x++)
359 	{
360 		this->orig_y[x] = (float *) wipemalloc(gy * sizeof(float));
361 	}
362 
363 		//initialize reference grid values
364 		for (x = 0; x < gx; x++)
365 		{
366 			for (int y = 0; y < gy; y++)
367 			{
368 				float origx = x / (float) (gx - 1);
369 				float origy = -((y / (float) (gy - 1)) - 1);
370 
371 				rad_mesh[x][y]=hypot ( ( origx-.5 ) *2, ( origy-.5 ) *2 ) * .7071067;
372 				orig_x[x][y] = (origx - .5) * 2;
373 				orig_y[x][y] = (origy - .5) * 2;
374 			}
375 		}
376 }
377 
~PresetInputs()378 PresetInputs::~PresetInputs()
379 {
380 	for ( int x = 0; x < this->gx; x++ )
381 	{
382 
383 
384 		free ( this->origtheta[x] );
385 		free ( this->origrad[x] );
386 		free ( this->origx[x] );
387 		free ( this->origy[x] );
388 
389 		free ( this->x_mesh[x] );
390 		free ( this->y_mesh[x] );
391 		free ( this->rad_mesh[x] );
392 		free ( this->theta_mesh[x] );
393 
394 	}
395 
396 
397 	free ( this->origx );
398 	free ( this->origy );
399 	free ( this->origrad );
400 	free ( this->origtheta );
401 
402 	free ( this->x_mesh );
403 	free ( this->y_mesh );
404 	free ( this->rad_mesh );
405 	free ( this->theta_mesh );
406 
407 	this->origx = NULL;
408 	this->origy = NULL;
409 	this->origtheta = NULL;
410 	this->origrad = NULL;
411 
412 	this->x_mesh = NULL;
413 	this->y_mesh = NULL;
414 	this->rad_mesh = NULL;
415 	this->theta_mesh = NULL;
416 }
417 
418 
resetMesh()419 void PresetInputs::resetMesh()
420 {
421 	int x,y;
422 
423 	assert ( x_mesh );
424 	assert ( y_mesh );
425 	assert ( rad_mesh );
426 	assert ( theta_mesh );
427 
428 	for ( x=0;x<this->gx;x++ )
429 	{
430 		for ( y=0;y<this->gy;y++ )
431 		{
432 			x_mesh[x][y]=this->origx[x][y];
433 			y_mesh[x][y]=this->origy[x][y];
434 			rad_mesh[x][y]=this->origrad[x][y];
435 			theta_mesh[x][y]=this->origtheta[x][y];
436 		}
437 	}
438 
439 }
440 
441 
442 #ifdef USE_MERGE_PRESET_CODE
MergePresets(PresetOutputs & A,PresetOutputs & B,double ratio,int gx,int gy)443 void PresetMerger::MergePresets(PresetOutputs & A, PresetOutputs & B, double ratio, int gx, int gy)
444 {
445 
446 double invratio = 1.0 - ratio;
447   //Merge Simple Waveforms
448   //
449   // All the mess is because of Waveform 7, which is two lines.
450   //
451 
452 
453   //Merge Custom Shapes and Custom Waves
454 
455   for (PresetOutputs::cshape_container::iterator pos = A.customShapes.begin();
456 	pos != A.customShapes.end(); ++pos)
457     {
458        (*pos)->a *= invratio;
459        (*pos)->a2 *= invratio;
460        (*pos)->border_a *= invratio;
461     }
462 
463   for (PresetOutputs::cshape_container::iterator pos = B.customShapes.begin();
464 	pos != B.customShapes.end(); ++pos)
465     {
466        (*pos)->a *= ratio;
467        (*pos)->a2 *= ratio;
468        (*pos)->border_a *= ratio;
469 
470         A.customShapes.push_back(*pos);
471 
472     }
473  for (PresetOutputs::cwave_container::iterator pos = A.customWaves.begin();
474 	pos != A.customWaves.end(); ++pos)
475     {
476        (*pos)->a *= invratio;
477       for (int x=0; x <   (*pos)->samples; x++)
478 	{
479 	   (*pos)->a_mesh[x]= (*pos)->a_mesh[x]*invratio;
480 	}
481     }
482 
483   for (PresetOutputs::cwave_container::iterator pos = B.customWaves.begin();
484 	pos != B.customWaves.end(); ++pos)
485     {
486        (*pos)->a *= ratio;
487       for (int x=0; x < (*pos)->samples; x++)
488 	{
489 	   (*pos)->a_mesh[x]= (*pos)->a_mesh[x]*ratio;
490 	}
491        A.customWaves.push_back(*pos);
492     }
493 
494 
495   //Interpolate Per-Pixel mesh
496 
497   for (int x=0;x<gx;x++)
498     {
499       for(int y=0;y<gy;y++)
500 	{
501 	  A.x_mesh[x][y]  = A.x_mesh[x][y]* invratio + B.x_mesh[x][y]*ratio;
502 	}
503     }
504  for (int x=0;x<gx;x++)
505     {
506       for(int y=0;y<gy;y++)
507 	{
508 	  A.y_mesh[x][y]  = A.y_mesh[x][y]* invratio + B.y_mesh[x][y]*ratio;
509 	}
510     }
511 
512 
513 
514  //Interpolate PerFrame floats
515 
516   A.screenDecay = A.screenDecay * invratio + B.screenDecay * ratio;
517 
518   A.wave.r = A.wave.r* invratio + B.wave.r*ratio;
519   A.wave.g = A.wave.g* invratio + B.wave.g*ratio;
520   A.wave.b = A.wave.b* invratio + B.wave.b*ratio;
521   A.wave.a = A.wave.a* invratio + B.wave.a*ratio;
522   A.wave.x = A.wave.x* invratio + B.wave.x*ratio;
523   A.wave.y = A.wave.y* invratio + B.wave.y*ratio;
524   A.wave.mystery = A.wave.mystery* invratio + B.wave.mystery*ratio;
525 
526   A.border.outer_size = A.border.outer_size* invratio + B.border.outer_size*ratio;
527   A.border.outer_r = A.border.outer_r* invratio + B.border.outer_r*ratio;
528   A.border.outer_g = A.border.outer_g* invratio + B.border.outer_g*ratio;
529   A.border.outer_b = A.border.outer_b* invratio + B.border.outer_b*ratio;
530   A.border.outer_a = A.border.outer_a* invratio + B.border.outer_a*ratio;
531 
532   A.border.inner_size = A.border.inner_size* invratio + B.border.inner_size*ratio;
533   A.border.inner_r = A.border.inner_r* invratio + B.border.inner_r*ratio;
534   A.border.inner_g = A.border.inner_g* invratio + B.border.inner_g*ratio;
535   A.border.inner_b = A.border.inner_b* invratio + B.border.inner_b*ratio;
536   A.border.inner_a = A.border.inner_a* invratio + B.border.inner_a*ratio;
537 
538   A.mv.a  = A.mv.a* invratio + B.mv.a*ratio;
539   A.mv.r  = A.mv.r* invratio + B.mv.r*ratio;
540   A.mv.g  = A.mv.g* invratio + B.mv.g*ratio;
541   A.mv.b  = A.mv.b* invratio + B.mv.b*ratio;
542   A.mv.length = A.mv.length* invratio + B.mv.length*ratio;
543   A.mv.x_num = A.mv.x_num* invratio + B.mv.x_num*ratio;
544   A.mv.y_num = A.mv.y_num* invratio + B.mv.y_num*ratio;
545   A.mv.y_offset = A.mv.y_offset* invratio + B.mv.y_offset*ratio;
546   A.mv.x_offset = A.mv.x_offset* invratio + B.mv.x_offset*ratio;
547 
548 
549   A.fRating = A.fRating* invratio + B.fRating*ratio;
550   A.fGammaAdj = A.fGammaAdj* invratio + B.fGammaAdj*ratio;
551   A.videoEcho.zoom = A.videoEcho.zoom* invratio + B.videoEcho.zoom*ratio;
552   A.videoEcho.a = A.videoEcho.a* invratio + B.videoEcho.a*ratio;
553 
554 
555   A.fWarpAnimSpeed = A.fWarpAnimSpeed* invratio + B.fWarpAnimSpeed*ratio;
556   A.fWarpScale = A.fWarpScale* invratio + B.fWarpScale*ratio;
557   A.fShader = A.fShader* invratio + B.fShader*ratio;
558 
559   //Switch bools and discrete values halfway.  Maybe we should do some interesting stuff here.
560 
561   if (ratio > 0.5)
562     {
563       A.videoEcho.orientation = B.videoEcho.orientation;
564       A.textureWrap = B.textureWrap;
565       A.bDarkenCenter = B.bDarkenCenter;
566       A.bRedBlueStereo = B.bRedBlueStereo;
567       A.bBrighten = B.bBrighten;
568       A.bDarken = B.bDarken;
569       A.bSolarize = B.bSolarize;
570       A.bInvert = B.bInvert;
571       A.bMotionVectorsOn = B.bMotionVectorsOn;
572     }
573 
574   return;
575 }
576 #endif
577