demux: libmp4: add and parse 3DDS uuid
[vlc.git] / modules / visualization / projectm.cpp
blobaee1ffb81446b7514e1c5750ba9ff808d8174a1e
1 /*****************************************************************************
2 * projectm.cpp: visualization module based on libprojectM
3 *****************************************************************************
4 * Copyright © 2009-2011 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Rémi Duraffort <ivoire@videolan.org>
8 * Laurent Aimar
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_aout.h>
32 #include <vlc_vout_window.h>
33 #include <vlc_opengl.h>
34 #include <vlc_filter.h>
35 #include <vlc_rand.h>
37 #include <libprojectM/projectM.hpp>
39 /*****************************************************************************
40 * Module descriptor
41 *****************************************************************************/
42 static int Open ( vlc_object_t * );
43 static void Close ( vlc_object_t * );
45 #define CONFIG_TEXT N_("projectM configuration file")
46 #define CONFIG_LONGTEXT N_("File that will be used to configure the projectM " \
47 "module.")
49 #define PRESET_PATH_TXT N_("projectM preset path")
50 #define PRESET_PATH_LONGTXT N_("Path to the projectM preset directory")
52 #define TITLE_FONT_TXT N_("Title font")
53 #define TITLE_FONT_LONGTXT N_("Font used for the titles")
55 #define MENU_FONT_TXT N_("Font menu")
56 #define MENU_FONT_LONGTXT N_("Font used for the menus")
58 #define WIDTH_TEXT N_("Video width")
59 #define WIDTH_LONGTEXT N_("The width of the video window, in pixels.")
61 #define HEIGHT_TEXT N_("Video height")
62 #define HEIGHT_LONGTEXT N_("The height of the video window, in pixels.")
64 #define MESHX_TEXT N_("Mesh width")
65 #define MESHX_LONGTEXT N_("The width of the mesh, in pixels.")
67 #define MESHY_TEXT N_("Mesh height")
68 #define MESHY_LONGTEXT N_("The height of the mesh, in pixels.")
70 #define TEXTURE_TEXT N_("Texture size")
71 #define TEXTURE_LONGTEXT N_("The size of the texture, in pixels.")
73 #ifdef _WIN32
74 # define FONT_PATH "C:\\WINDOWS\\Fonts\\arial.ttf"
75 # define FONT_PATH_MENU "C:\\WINDOWS\\Fonts\\arial.ttf"
76 # define PRESET_PATH NULL
77 #else
78 # define FONT_PATH "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf"
79 # define FONT_PATH_MENU "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf"
80 # define PRESET_PATH "/usr/share/projectM/presets"
81 #endif
83 #ifdef DEFAULT_FONT_FILE
84 #undef FONT_PATH
85 #define FONT_PATH DEFAULT_FONT_FILE
86 #endif
88 #ifdef DEFAULT_MONOSPACE_FONT_FILE
89 #undef FONT_PATH_MENU
90 #define FONT_PATH_MENU DEFAULT_MONOSPACE_FONT_FILE
91 #endif
93 vlc_module_begin ()
94 set_shortname( N_("projectM"))
95 set_description( N_("libprojectM effect") )
96 set_capability( "visualization", 0 )
97 set_category( CAT_AUDIO )
98 set_subcategory( SUBCAT_AUDIO_VISUAL )
99 #ifndef HAVE_PROJECTM2
100 add_loadfile( "projectm-config", "/usr/share/projectM/config.inp",
101 CONFIG_TEXT, CONFIG_LONGTEXT, true )
102 #else
103 add_directory( "projectm-preset-path", PRESET_PATH,
104 PRESET_PATH_TXT, PRESET_PATH_LONGTXT, true )
105 add_loadfile( "projectm-title-font", FONT_PATH,
106 TITLE_FONT_TXT, TITLE_FONT_LONGTXT, true )
107 add_loadfile( "projectm-menu-font", FONT_PATH_MENU,
108 MENU_FONT_TXT, MENU_FONT_LONGTXT, true )
109 #endif
110 add_integer( "projectm-width", 800, WIDTH_TEXT, WIDTH_LONGTEXT,
111 false )
112 add_integer( "projectm-height", 500, HEIGHT_TEXT, HEIGHT_LONGTEXT,
113 false )
114 add_integer( "projectm-meshx", 32, MESHX_TEXT, MESHX_LONGTEXT,
115 false )
116 add_integer( "projectm-meshy", 24, MESHY_TEXT, MESHY_LONGTEXT,
117 false )
118 add_integer( "projectm-texture-size", 1024, TEXTURE_TEXT, TEXTURE_LONGTEXT,
119 false )
120 add_shortcut( "projectm" )
121 set_callbacks( Open, Close )
122 vlc_module_end ()
125 /*****************************************************************************
126 * Local prototypes
127 *****************************************************************************/
128 struct filter_sys_t
130 /* */
131 vlc_thread_t thread;
133 /* Opengl */
134 vlc_gl_t *gl;
136 /* audio info */
137 int i_channels;
139 /* */
140 vlc_mutex_t lock;
141 bool b_quit;
142 float *p_buffer;
143 unsigned i_buffer_size;
144 unsigned i_nb_samples;
148 static block_t *DoWork( filter_t *, block_t * );
149 static void *Thread( void * );
152 * Open the module
153 * @param p_this: the filter object
154 * @return VLC_SUCCESS or vlc error codes
156 static int Open( vlc_object_t * p_this )
158 filter_t *p_filter = (filter_t *)p_this;
159 filter_sys_t *p_sys;
161 p_sys = p_filter->p_sys = (filter_sys_t*)malloc( sizeof( *p_sys ) );
162 if( !p_sys )
163 return VLC_ENOMEM;
165 /* Create the object for the thread */
166 p_sys->b_quit = false;
167 p_sys->i_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
168 vlc_mutex_init( &p_sys->lock );
169 p_sys->p_buffer = NULL;
170 p_sys->i_buffer_size = 0;
171 p_sys->i_nb_samples = 0;
173 /* Create the OpenGL context */
174 vout_window_cfg_t cfg;
176 memset(&cfg, 0, sizeof (cfg));
177 cfg.width = var_CreateGetInteger( p_filter, "projectm-width" );
178 cfg.height = var_CreateGetInteger( p_filter, "projectm-height" );
180 p_sys->gl = vlc_gl_surface_Create( VLC_OBJECT(p_filter), &cfg, NULL );
181 if( p_sys->gl == NULL )
182 goto error;
184 /* Create the thread */
185 if( vlc_clone( &p_sys->thread, Thread, p_filter,
186 VLC_THREAD_PRIORITY_LOW ) )
188 vlc_gl_surface_Destroy( p_sys->gl );
189 goto error;
192 p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
193 p_filter->fmt_out.audio = p_filter->fmt_in.audio;
194 p_filter->pf_audio_filter = DoWork;
195 return VLC_SUCCESS;
197 error:
198 vlc_mutex_destroy( &p_sys->lock );
199 free (p_sys );
200 return VLC_EGENERIC;
205 * Close the module
206 * @param p_this: the filter object
208 static void Close( vlc_object_t *p_this )
210 filter_t *p_filter = (filter_t *)p_this;
211 filter_sys_t *p_sys = p_filter->p_sys;
213 /* Stop the thread
214 * XXX vlc_cleanup_push does not seems to work with C++ so no
215 * vlc_cancel()... */
216 vlc_mutex_lock( &p_sys->lock );
217 p_sys->b_quit = true;
218 vlc_mutex_unlock( &p_sys->lock );
220 vlc_join( p_sys->thread, NULL );
222 /* Free the ressources */
223 vlc_gl_surface_Destroy( p_sys->gl );
224 vlc_mutex_destroy( &p_sys->lock );
225 free( p_sys->p_buffer );
226 free( p_sys );
231 * Do the actual work with the new sample
232 * @param p_aout: audio output object
233 * @param p_filter: filter object
234 * @param p_in_buf: input buffer
235 * @param p_out_buf: output buffer
237 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
239 filter_sys_t *p_sys = p_filter->p_sys;
241 vlc_mutex_lock( &p_sys->lock );
242 if( p_sys->i_buffer_size > 0 )
244 p_sys->i_nb_samples = __MIN( p_sys->i_buffer_size,
245 p_in_buf->i_nb_samples );
247 const float *p_src = (float*)p_in_buf->p_buffer;
248 for( unsigned i = 0; i < p_sys->i_nb_samples; i++ )
250 float v = 0;
251 for( int j = 0; j < p_sys->i_channels; j++ )
252 v += p_src[p_sys->i_channels * i + j];
253 p_sys->p_buffer[i] = v / p_sys->i_channels;
256 vlc_mutex_unlock( &p_sys->lock );
258 return p_in_buf;
262 * ProjectM update thread which do the rendering
263 * @param p_this: the p_thread object
265 static void *Thread( void *p_data )
267 filter_t *p_filter = (filter_t*)p_data;
268 filter_sys_t *p_sys = p_filter->p_sys;
269 vlc_gl_t *gl = p_sys->gl;
270 locale_t loc;
271 locale_t oldloc;
273 projectM *p_projectm;
274 #ifndef HAVE_PROJECTM2
275 char *psz_config;
276 #else
277 char *psz_preset_path;
278 char *psz_title_font;
279 char *psz_menu_font;
280 projectM::Settings settings;
281 #endif
283 if( vlc_gl_MakeCurrent( gl ) != VLC_SUCCESS )
285 msg_Err( p_filter, "Can't attach gl context" );
286 return NULL;
289 /* Work-around the projectM locale bug */
290 loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
291 oldloc = uselocale (loc);
293 /* Create the projectM object */
294 #ifndef HAVE_PROJECTM2
295 psz_config = var_InheritString( p_filter, "projectm-config" );
296 p_projectm = new projectM( psz_config );
297 free( psz_config );
298 #else
299 psz_preset_path = var_InheritString( p_filter, "projectm-preset-path" );
300 #ifdef _WIN32
301 if ( psz_preset_path == NULL )
303 char *psz_data_path = config_GetDataDir();
304 asprintf( &psz_preset_path, "%s" DIR_SEP "visualization", psz_data_path );
305 free( psz_data_path );
307 #endif
309 psz_title_font = var_InheritString( p_filter, "projectm-title-font" );
310 psz_menu_font = var_InheritString( p_filter, "projectm-menu-font" );
312 settings.meshX = var_InheritInteger( p_filter, "projectm-meshx" );
313 settings.meshY = var_InheritInteger( p_filter, "projectm-meshy" );
314 settings.fps = 35;
315 settings.textureSize = var_InheritInteger( p_filter, "projectm-texture-size" );
316 settings.windowWidth = var_InheritInteger( p_filter, "projectm-width" );
317 settings.windowHeight = var_CreateGetInteger( p_filter, "projectm-height" );
318 settings.presetURL = psz_preset_path;
319 settings.titleFontURL = psz_title_font;
320 settings.menuFontURL = psz_menu_font;
321 settings.smoothPresetDuration = 5;
322 settings.presetDuration = 30;
323 settings.beatSensitivity = 10;
324 settings.aspectCorrection = 1;
325 settings.easterEgg = 1;
326 settings.shuffleEnabled = 1;
327 settings.softCutRatingsEnabled= false;
329 p_projectm = new projectM( settings );
331 free( psz_menu_font );
332 free( psz_title_font );
333 free( psz_preset_path );
334 #endif /* HAVE_PROJECTM2 */
336 p_sys->i_buffer_size = p_projectm->pcm()->maxsamples;
337 p_sys->p_buffer = (float*)calloc( p_sys->i_buffer_size,
338 sizeof( float ) );
340 /* Choose a preset randomly or projectM will always show the first one */
341 if ( p_projectm->getPlaylistSize() > 0 )
342 p_projectm->selectPreset( (unsigned)vlc_mrand48() % p_projectm->getPlaylistSize() );
344 /* */
345 for( ;; )
347 const mtime_t i_deadline = mdate() + CLOCK_FREQ / 50; /* 50 fps max */
349 /* Manage the events */
350 unsigned width, height;
351 bool quit;
353 if( vlc_gl_surface_CheckSize( gl, &width, &height ) )
354 p_projectm->projectM_resetGL( width, height );
356 /* Render the image and swap the buffers */
357 vlc_mutex_lock( &p_sys->lock );
358 if( p_sys->i_nb_samples > 0 )
360 p_projectm->pcm()->addPCMfloat( p_sys->p_buffer,
361 p_sys->i_nb_samples );
362 p_sys->i_nb_samples = 0;
364 quit = p_sys->b_quit;
365 vlc_mutex_unlock( &p_sys->lock );
367 if( quit )
368 break;
370 p_projectm->renderFrame();
372 /* */
373 mwait( i_deadline );
375 vlc_gl_Swap( gl );
378 delete p_projectm;
380 if (loc != (locale_t)0)
382 uselocale (oldloc);
383 freelocale (loc);
386 vlc_gl_ReleaseCurrent( gl );
387 return NULL;