1 /*****************************************************************************
2 * projectm: visualization module based on libprojectM
3 *****************************************************************************
4 * Copyright (C) 2009 the VideoLAN team
7 * Authors: RĂ©mi Duraffort <ivoire@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * You should have received a copy of the GNU General Public License along with
20 * this program; if not, write to the Free Software Foundation, Inc., 51
21 * Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
27 #ifndef __STDC_CONSTANT_MACROS
28 # define __STDC_CONSTANT_MACROS
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
35 #include <vlc_vout_wrapper.h>
36 #include <vlc_filter.h>
38 #include <libprojectM/projectM.hpp>
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close ( vlc_object_t * );
47 #define CONFIG_TEXT N_("projectM configuration file")
48 #define CONFIG_LONGTEXT N_("File that will be used to configure the projectM " \
51 #define WIDTH_TEXT N_("Video width")
52 #define WIDTH_LONGTEXT N_("The width of the video window, in pixels.")
54 #define HEIGHT_TEXT N_("Video height")
55 #define HEIGHT_LONGTEXT N_("The height of the video window, in pixels.")
58 set_shortname( N_("projectM"))
59 set_description( N_("libprojectM effect") )
60 set_capability( "visualization2", 0 )
61 set_category( CAT_AUDIO )
62 set_subcategory( SUBCAT_AUDIO_VISUAL )
63 add_file( "projectm-config", "/usr/share/projectM/config.inp", NULL,
64 CONFIG_TEXT, CONFIG_LONGTEXT, true )
65 add_integer( "projectm-width", 800, NULL, WIDTH_TEXT, WIDTH_LONGTEXT,
67 add_integer( "projectm-height", 640, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT,
69 add_shortcut( "projectm" )
70 set_callbacks( Open, Close )
74 /*****************************************************************************
76 *****************************************************************************/
85 vout_thread_t *p_vout;
88 /* libprojectM objects */
106 static block_t *DoWork( filter_t *, block_t * );
107 static void *Thread( void * );
111 * @param p_this: the filter object
112 * @return VLC_SUCCESS or vlc error codes
114 static int Open( vlc_object_t * p_this )
116 filter_t *p_filter = (filter_t *)p_this;
119 /* Test the audio format */
120 if( p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 ||
121 p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32 )
123 msg_Warn( p_filter, "bad input or output format" );
126 if( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
128 msg_Warn( p_filter, "input and outut are not similar" );
132 p_filter->pf_audio_filter = DoWork;
134 p_sys = p_filter->p_sys = (filter_sys_t*)malloc( sizeof( *p_sys ) );
138 /* Create the object for the thread */
139 vlc_sem_init( &p_sys->ready, 0 );
140 p_sys->b_error = false;
141 p_sys->i_width = var_CreateGetInteger( p_filter, "projectm-width" );
142 p_sys->i_height = var_CreateGetInteger( p_filter, "projectm-height" );
143 p_sys->i_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
144 p_sys->psz_config = var_CreateGetString( p_filter, "projectm-config" );
145 vlc_mutex_init( &p_sys->lock );
146 p_sys->p_buffer = NULL;
147 p_sys->i_buffer_size = 0;
148 p_sys->i_nb_samples = 0;
150 /* Create the thread */
151 if( vlc_clone( &p_sys->thread, Thread, p_filter, VLC_THREAD_PRIORITY_LOW ) )
154 vlc_sem_wait( &p_sys->ready );
157 vlc_join( p_sys->thread, NULL );
164 vlc_sem_destroy( &p_sys->ready );
172 * @param p_this: the filter object
174 static void Close( vlc_object_t *p_this )
176 filter_t *p_filter = (filter_t *)p_this;
177 filter_sys_t *p_sys = p_filter->p_sys;
179 /* Stop the thread */
180 vlc_cancel( p_sys->thread );
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 free( p_sys->p_buffer );
187 free( p_sys->psz_config );
193 * Do the actual work with the new sample
194 * @param p_aout: audio output object
195 * @param p_filter: filter object
196 * @param p_in_buf: input buffer
197 * @param p_out_buf: output buffer
199 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
201 filter_sys_t *p_sys = p_filter->p_sys;
203 vlc_mutex_lock( &p_sys->lock );
204 if( p_sys->i_buffer_size > 0 )
206 p_sys->i_nb_samples = __MIN( p_sys->i_buffer_size,
207 p_in_buf->i_nb_samples );
209 const float *p_src = (float*)p_in_buf->p_buffer;
210 for( int i = 0; i < p_sys->i_nb_samples; i++ )
213 for( int j = 0; j < p_sys->i_channels; j++ )
214 v += p_src[p_sys->i_channels * i + j];
215 p_sys->p_buffer[i] = v / p_sys->i_channels;
218 vlc_mutex_unlock( &p_sys->lock );
224 * Variable callback for the dummy vout
226 static int VoutCallback( vlc_object_t *p_vout, char const *psz_name,
227 vlc_value_t oldv, vlc_value_t newv, void *p_data )
229 vout_display_t *p_vd = (vout_display_t*)p_data;
231 if( !strcmp(psz_name, "fullscreen") )
233 vout_SetDisplayFullscreen( p_vd, newv.b_bool );
238 * Clean up function when Thread() is cancelled.
240 static void ThreadCleanup( void *p_data )
242 filter_t *p_filter = (filter_t*)p_data;
243 filter_sys_t *p_sys = p_filter->p_sys;
246 delete p_sys->p_projectm;
248 /* Free the openGL provider */
249 vout_DeleteDisplay( p_sys->p_vd, NULL );
250 vlc_object_release( p_sys->p_vout );
255 * ProjectM update thread which do the rendering
256 * @param p_this: the p_thread object
258 static void *Thread( void *p_data )
260 filter_t *p_filter = (filter_t*)p_data;
261 filter_sys_t *p_sys = p_filter->p_sys;
262 int cancel = vlc_savecancel();
266 /* Create the openGL provider */
268 (vout_thread_t *)vlc_object_create( p_filter, sizeof(vout_thread_t) );
273 video_format_Init( &fmt, 0 );
274 video_format_Setup( &fmt, VLC_CODEC_RGB32,
275 p_sys->i_width, p_sys->i_height, 0, 1 );
279 vout_display_state_t state;
280 memset( &state, 0, sizeof(state) );
281 state.cfg.display.sar.num = 1;
282 state.cfg.display.sar.den = 1;
283 state.cfg.is_display_filled = true;
284 state.cfg.zoom.num = 1;
285 state.cfg.zoom.den = 1;
289 p_sys->p_vd = vout_NewDisplay( p_sys->p_vout, &fmt, &state, "opengl",
293 vlc_object_release( p_sys->p_vout );
296 var_Create( p_sys->p_vout, "fullscreen", VLC_VAR_BOOL );
297 var_AddCallback( p_sys->p_vout, "fullscreen", VoutCallback, p_sys->p_vd );
299 gl = vout_GetDisplayOpengl( p_sys->p_vd );
302 vout_DeleteDisplay( p_sys->p_vd, NULL );
303 vlc_object_release( p_sys->p_vout );
306 vlc_cleanup_push( ThreadCleanup, p_filter );
308 /* Create the projectM object */
309 p_sys->p_projectm = new projectM( p_sys->psz_config );
310 p_sys->i_buffer_size = p_sys->p_projectm->pcm()->maxsamples;
311 p_sys->p_buffer = (float*)calloc( p_sys->i_buffer_size,
314 vlc_sem_post( &p_sys->ready );
316 /* TODO: Give to projectm the name of the input
317 p_sys->p_projectm->projectM_setTitle( "" ); */
320 int i_last_width = 0;
321 int i_last_height = 0;
324 const mtime_t i_deadline = mdate() + CLOCK_FREQ / 50; /* 50 fps max */
325 /* Manage the events */
326 vout_ManageDisplay( p_sys->p_vd, true );
327 if( p_sys->p_vd->cfg->display.width != i_last_width ||
328 p_sys->p_vd->cfg->display.height != i_last_height )
330 /* FIXME it is not perfect as we will have black bands */
331 vout_display_place_t place;
332 vout_display_PlacePicture( &place, &p_sys->p_vd->source, p_sys->p_vd->cfg, false );
333 p_sys->p_projectm->projectM_resetGL( place.width, place.height );
335 i_last_width = p_sys->p_vd->cfg->display.width;
336 i_last_height = p_sys->p_vd->cfg->display.height;
339 /* Render the image and swap the buffers */
340 vlc_mutex_lock( &p_sys->lock );
341 if( p_sys->i_nb_samples > 0 )
343 p_sys->p_projectm->pcm()->addPCMfloat( p_sys->p_buffer,
344 p_sys->i_nb_samples );
345 p_sys->i_nb_samples = 0;
347 vlc_mutex_unlock( &p_sys->lock );
349 p_sys->p_projectm->renderFrame();
352 vlc_restorecancel( cancel );
354 cancel = vlc_savecancel();
356 if( !vout_opengl_Lock(gl) )
358 vout_opengl_Swap( gl );
359 vout_opengl_Unlock( gl );
366 p_sys->b_error = true;
367 vlc_sem_post( &p_sys->ready );