1 /*****************************************************************************
2 * vsxu.cpp: visualization module wrapper for Vovoid VSXu
3 *****************************************************************************
4 * Copyright © 2009-2012 the VideoLAN team, Vovoid Media Technologies
7 * Authors: Rémi Duraffort <ivoire@videolan.org>
9 * Jonatan "jaw" Wallmander
11 * Used the projectM implementation as reference for this file.
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
18 * This program is distributed in the hope that it will be useful, but WITHOUT
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
23 * You should have received a copy of the GNU General Public License along with
24 * this program; if not, write to the Free Software Foundation, Inc., 51
25 * Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
31 #ifndef __STDC_CONSTANT_MACROS
32 # define __STDC_CONSTANT_MACROS
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
39 #include <vlc_vout_wrapper.h>
40 #include <vlc_opengl.h>
42 // vsxu manager include
43 #include <vsx_manager.h>
44 #include <logo_intro.h>
46 // class to handle cyclic buffer
47 #include "cyclic_buffer.h"
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Open ( vlc_object_t * );
53 static void Close ( vlc_object_t * );
55 #define WIDTH_TEXT N_("Video width")
56 #define WIDTH_LONGTEXT N_("The width of the video window, in pixels.")
58 #define HEIGHT_TEXT N_("Video height")
59 #define HEIGHT_LONGTEXT N_("The height of the video window, in pixels.")
62 set_shortname( N_("vsxu"))
63 set_description( N_("vsxu") )
64 set_capability( "visualization2", 0 )
65 set_category( CAT_AUDIO )
66 set_subcategory( SUBCAT_AUDIO_VISUAL )
67 add_integer( "vsxu-width", 1280, WIDTH_TEXT, WIDTH_LONGTEXT,
69 add_integer( "vsxu-height", 800, HEIGHT_TEXT, HEIGHT_LONGTEXT,
71 add_shortcut( "vsxu" )
72 set_callbacks( Open, Close )
76 /*****************************************************************************
78 *****************************************************************************/
88 // mutex around the cyclic block
89 vlc_mutex_t cyclic_block_mutex;
91 // cyclic buffer to cache sound frames in
92 cyclic_block_queue* vsxu_cyclic_buffer;
102 static block_t *DoWork( filter_t *, block_t * );
103 static void *Thread( void * );
107 * @param p_this: the filter object
108 * @return VLC_SUCCESS or vlc error codes
110 static int Open( vlc_object_t * p_this )
112 filter_t *p_filter = (filter_t *)p_this;
115 /* Test the audio format */
116 if( p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 ||
117 p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32 )
119 msg_Warn( p_filter, "bad input or output format" );
122 if( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
124 msg_Warn( p_filter, "input and outut are not similar" );
128 p_sys = p_filter->p_sys = (filter_sys_t*)malloc( sizeof( *p_sys ) );
129 if( unlikely( !p_sys ) )
134 /* Create the object for the thread */
135 vlc_sem_init( &p_sys->ready, 0 );
136 p_sys->b_error = false;
137 p_sys->b_quit = false;
138 p_sys->i_width = var_InheritInteger( p_filter, "vsxu-width" );
139 p_sys->i_height = var_InheritInteger( p_filter, "vsxu-height" );
140 p_sys->i_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
141 vlc_mutex_init( &p_sys->lock );
142 vlc_mutex_init( &p_sys->cyclic_block_mutex );
143 p_sys->vsxu_cyclic_buffer = new cyclic_block_queue();
146 /* Create the thread */
147 if( vlc_clone( &p_sys->thread, Thread, p_filter, VLC_THREAD_PRIORITY_LOW ) )
151 vlc_sem_wait( &p_sys->ready );
154 vlc_join( p_sys->thread, NULL );
158 p_filter->pf_audio_filter = DoWork;
163 vlc_sem_destroy( &p_sys->ready );
170 * @param p_this: the filter object
172 static void Close( vlc_object_t *p_this )
174 filter_t *p_filter = (filter_t *)p_this;
175 filter_sys_t *p_sys = p_filter->p_sys;
177 vlc_mutex_lock( &p_sys->lock );
178 p_sys->b_quit = true;
179 vlc_mutex_unlock( &p_sys->lock );
181 vlc_join( p_sys->thread, NULL );
183 /* Free the ressources */
184 vlc_sem_destroy( &p_sys->ready );
185 vlc_mutex_destroy( &p_sys->lock );
186 delete p_sys->vsxu_cyclic_buffer;
191 * Do the actual work with the new sample
192 * @param p_filter: filter object
193 * @param p_in_buf: input buffer
195 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
197 filter_sys_t *p_sys = p_filter->p_sys;
199 vlc_mutex_lock( &p_sys->lock );
200 vlc_mutex_lock( &p_sys->cyclic_block_mutex );
202 unsigned i_nb_samples = __MIN( 1024,
203 p_in_buf->i_nb_samples );
205 const float *p_src = (float*)p_in_buf->p_buffer;
206 // iterate block holder
207 size_t i_bh_data_iter = 0;
209 // calc 512-byte-aligned sample count to grab, we don't need more
210 unsigned i_num_samples = i_nb_samples - i_nb_samples % 512;
212 // muls are cheaper than divs
213 float f_onedivchannels = 1.0f / (float)p_sys->i_channels;
215 block_holder* p_block_holder = p_sys->vsxu_cyclic_buffer->get_insertion_object();
216 p_block_holder->pts = p_in_buf->i_pts;
217 for( unsigned i = 0; i < i_num_samples; i++ )
220 for( int j = 0; j < p_sys->i_channels; j++ )
222 f_v += p_src[p_sys->i_channels * i + j];
225 // insert into our little cyclic buffer
226 p_block_holder->data[i_bh_data_iter] = f_v * f_onedivchannels;
228 if (i_bh_data_iter == 512 && i < i_num_samples-256)
230 p_block_holder = p_sys->vsxu_cyclic_buffer->get_insertion_object();
231 p_block_holder->pts = p_in_buf->i_pts + 11609;
237 vlc_mutex_unlock( &p_sys->cyclic_block_mutex );
238 vlc_mutex_unlock( &p_sys->lock );
243 * Variable callback for the dummy vout
245 static int VoutCallback( vlc_object_t *p_vout, char const *psz_name,
246 vlc_value_t oldv, vlc_value_t newv, void *p_data )
248 VLC_UNUSED( p_vout ); VLC_UNUSED( oldv );
249 vout_display_t *p_vd = (vout_display_t*)p_data;
251 if( !strcmp(psz_name, "fullscreen") )
253 vout_SetDisplayFullscreen( p_vd, newv.b_bool );
259 * VSXu update thread which do the rendering
260 * @param p_this: the p_thread object
262 static void *Thread( void *p_data )
264 filter_t *p_filter = (filter_t*)p_data;
265 filter_sys_t *p_sys = p_filter->p_sys;
267 // our abstract manager holder
268 vsx_manager_abs* manager = 0;
270 // temp audio buffer for sending to vsxu through manager
271 float f_sample_buf[512];
274 vsx_logo_intro* intro = 0;
276 vout_display_t *p_vd;
281 unsigned int i_last_width = 0;
282 unsigned int i_last_height = 0;
286 /* Create the openGL provider */
287 vout_thread_t *p_vout;
289 p_vout = (vout_thread_t *)vlc_object_create( p_filter, sizeof(vout_thread_t) );
293 video_format_Init( &fmt, 0 );
294 video_format_Setup( &fmt, VLC_CODEC_RGB32,
295 p_sys->i_width, p_sys->i_height, 0, 1 );
299 vout_display_state_t state;
300 memset( &state, 0, sizeof(state) );
301 state.cfg.display.sar.num = 1;
302 state.cfg.display.sar.den = 1;
303 state.cfg.is_display_filled = true;
304 state.cfg.zoom.num = 1;
305 state.cfg.zoom.den = 1;
309 p_vd = vout_NewDisplay( p_vout, &fmt, &state, "opengl", 300000, 1000000 );
312 vlc_object_release( p_vout );
315 var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
316 var_AddCallback( p_vout, "fullscreen", VoutCallback, p_vd );
318 gl = vout_GetDisplayOpengl( p_vd );
321 var_DelCallback( p_vout, "fullscreen", VoutCallback, p_vd );
322 vout_DeleteDisplay( p_vd, NULL );
323 vlc_object_release( p_vout );
327 // tell main thread we are ready
328 vlc_sem_post( &p_sys->ready );
332 /* Manage the events */
333 vout_ManageDisplay( p_vd, true );
334 if( p_vd->cfg->display.width != i_last_width ||
335 p_vd->cfg->display.height != i_last_height )
337 /* FIXME it is not perfect as we will have black bands */
338 vout_display_place_t place;
339 vout_display_PlacePicture( &place, &p_vd->source, p_vd->cfg, false );
341 i_last_width = p_vd->cfg->display.width;
342 i_last_height = p_vd->cfg->display.height;
345 // look for control commands from outside the thread
346 vlc_mutex_lock( &p_sys->lock );
351 vlc_mutex_unlock( &p_sys->lock );
355 // only run this once
358 // create a new manager
359 manager = manager_factory();
361 // init manager with the shared path and sound input type.
362 manager->init( 0, "media_player" );
364 // only show logo once
365 // keep track of iterations
366 static int i_iterations = 0;
367 if ( i_iterations++ < 1 )
369 intro = new vsx_logo_intro();
370 intro->set_destroy_textures( false );
374 // lock cyclic buffer mutex and copy floats
375 vlc_mutex_lock( &p_sys->cyclic_block_mutex );
376 block_holder* bh = p_sys->vsxu_cyclic_buffer->consume();
377 memcpy( &f_sample_buf[0], (void*)(&bh->data[0]), sizeof(float) * 512 );
378 vlc_mutex_unlock( &p_sys->cyclic_block_mutex );
380 // send sound pointer to vsxu
381 manager->set_sound_wave( &f_sample_buf[0] );
383 // render vsxu engine
384 if (manager) manager->render();
387 if (intro) intro->draw();
390 if( !vlc_gl_Lock(gl) )
397 // stop vsxu nicely (unloads textures and frees memory)
398 if (manager) manager->stop();
400 // call manager factory to destruct our manager object
401 if (manager) manager_destroy( manager );
403 // delete the intro (if ever allocated)
404 if (intro) delete intro;
406 var_DelCallback( p_vout, "fullscreen", VoutCallback, p_vd );
408 // clean out vlc opengl stuff
409 vout_DeleteDisplay( p_vd, NULL );
410 vlc_object_release( p_vout );
412 // clean up the cyclic buffer
413 vlc_mutex_lock( &p_sys->cyclic_block_mutex );
414 p_sys->vsxu_cyclic_buffer->reset();
415 vlc_mutex_unlock( &p_sys->cyclic_block_mutex );
421 p_sys->b_error = true;
422 vlc_sem_post( &p_sys->ready );