]> git.sesse.net Git - vlc/blob - modules/visualization/galaktos/plugin.c
Remove most stray semi-colons in module descriptions
[vlc] / modules / visualization / galaktos / plugin.c
1 /*****************************************************************************
2  * plugin.c:
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet <asmax@videolan.org>
8  *          Implementation of the winamp plugin MilkDrop
9  *          based on projectM http://xmms-projectm.sourceforge.net
10  *          and SciVi http://xmms-scivi.sourceforge.net
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #include "plugin.h"
32 #include "main.h"
33 #include "PCM.h"
34 #include "video_init.h"
35 #include <GL/glu.h>
36
37 #include <vlc_input.h>
38 #include <vlc_playlist.h>
39 #include <vlc_plugin.h>
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 static int  Open         ( vlc_object_t * );
45 static void Close        ( vlc_object_t * );
46
47 vlc_module_begin ()
48     set_description( N_("GaLaktos visualization plugin") )
49     set_capability( "visualization", 0 )
50     set_callbacks( Open, Close )
51     add_shortcut( "galaktos" )
52 vlc_module_end ()
53
54 /*****************************************************************************
55  * Local prototypes
56  *****************************************************************************/
57 typedef struct aout_filter_sys_t
58 {
59     galaktos_thread_t *p_thread;
60
61 } aout_filter_sys_t;
62
63 static void DoWork   ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
64                        aout_buffer_t * );
65
66 static void* Thread   ( vlc_object_t * );
67
68 static char *TitleGet( vlc_object_t * );
69
70
71 extern GLuint RenderTargetTextureID;
72
73 /*****************************************************************************
74  * Open: open a scope effect plugin
75  *****************************************************************************/
76 static int Open( vlc_object_t *p_this )
77 {
78     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
79     aout_filter_sys_t *p_sys;
80     galaktos_thread_t *p_thread;
81
82     if ( p_filter->input.i_format != VLC_FOURCC('f','l','3','2' )
83          || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
84     {
85         msg_Warn( p_filter, "bad input or output format" );
86         return VLC_EGENERIC;
87     }
88     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
89     {
90         msg_Warn( p_filter, "input and output formats are not similar" );
91         return VLC_EGENERIC;
92     }
93
94     p_filter->pf_do_work = DoWork;
95     p_filter->b_in_place = 1;
96
97     /* Allocate structure */
98     p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );
99
100     /* Create galaktos thread */
101     p_sys->p_thread = p_thread =
102         vlc_object_create( p_filter, sizeof( galaktos_thread_t ) );
103     vlc_object_attach( p_thread, p_this );
104
105 /*
106     var_Create( p_thread, "galaktos-width", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
107     var_Get( p_thread, "galaktos-width", &width );
108     var_Create( p_thread, "galaktos-height", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
109     var_Get( p_thread, "galaktos-height", &height );
110 */
111     p_thread->i_cur_sample = 0;
112     bzero( p_thread->p_data, 2*2*512 );
113
114     p_thread->i_width = 600;
115     p_thread->i_height = 600;
116     p_thread->b_fullscreen = 0;
117     galaktos_init( p_thread );
118
119     p_thread->i_channels = aout_FormatNbChannels( &p_filter->input );
120
121     p_thread->psz_title = TitleGet( VLC_OBJECT( p_filter ) );
122
123     if( vlc_thread_create( p_thread, "galaktos update thread", Thread,
124                            VLC_THREAD_PRIORITY_LOW, false ) )
125     {
126         msg_Err( p_filter, "cannot lauch galaktos thread" );
127         free( p_thread->psz_title );
128         vlc_object_detach( p_thread );
129         vlc_object_release( p_thread );
130         free( p_sys );
131         return VLC_EGENERIC;
132     }
133
134     return VLC_SUCCESS;
135 }
136
137 /*****************************************************************************
138  * float to s16 conversion
139  *****************************************************************************/
140 static inline int16_t FloatToInt16( float f )
141 {
142     if( f >= 1.0 )
143         return 32767;
144     else if( f < -1.0 )
145         return -32768;
146     else
147         return (int16_t)( f * 32768.0 );
148 }
149
150 /*****************************************************************************
151  * DoWork: process samples buffer
152  *****************************************************************************
153  * This function queues the audio buffer to be processed by the galaktos thread
154  *****************************************************************************/
155 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
156                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
157 {
158     int i_samples;
159     int i_channels;
160     float *p_float;
161     galaktos_thread_t *p_thread = p_filter->p_sys->p_thread;
162
163     p_float = (float *)p_in_buf->p_buffer;
164     i_channels = p_thread->i_channels;
165
166     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
167     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes;
168
169     for( i_samples = p_in_buf->i_nb_samples; i_samples > 0; i_samples-- )
170     {
171         int i_cur_sample = p_thread->i_cur_sample;
172
173         p_thread->p_data[0][i_cur_sample] = FloatToInt16( p_float[0] );
174         if( i_channels > 1 )
175         {
176             p_thread->p_data[1][i_cur_sample] = FloatToInt16( p_float[1] );
177         }
178         p_float += i_channels;
179
180         if( ++(p_thread->i_cur_sample) == 512 )
181         {
182             addPCM( p_thread->p_data );
183             p_thread->i_cur_sample = 0;
184         }
185     }
186 }
187
188 /*****************************************************************************
189  * Thread:
190  *****************************************************************************/
191 static void* Thread( vlc_object_t *p_this )
192 {
193     galaktos_thread_t *p_thread = (galaktos_thread_t*)p_this;
194
195     int count=0;
196     double realfps=0,fpsstart=0;
197     int timed=0;
198     int timestart=0;
199     int mspf=0;
200     int canc = vlc_savecancel ();
201
202     /* Get on OpenGL provider */
203     p_thread->p_opengl =
204         (vout_thread_t *)vlc_object_create( p_this, sizeof( vout_thread_t ) );
205     if( p_thread->p_opengl == NULL )
206     {
207         vlc_restorecancel (canc);
208         return NULL;
209     }
210     vlc_object_attach( p_thread->p_opengl, p_this );
211
212     /* Initialize vout parameters */
213     vout_InitFormat( &p_thread->p_opengl->fmt_in,
214                      VLC_FOURCC('R','V','3','2'),
215                      p_thread->i_width, p_thread->i_height, 1 );
216     p_thread->p_opengl->i_window_width = p_thread->i_width;
217     p_thread->p_opengl->i_window_height = p_thread->i_height;
218     p_thread->p_opengl->render.i_width = p_thread->i_width;
219     p_thread->p_opengl->render.i_height = p_thread->i_width;
220     p_thread->p_opengl->render.i_aspect = VOUT_ASPECT_FACTOR;
221     p_thread->p_opengl->b_scale = true;
222     p_thread->p_opengl->b_fullscreen = false;
223     p_thread->p_opengl->i_alignment = 0;
224     p_thread->p_opengl->fmt_in.i_sar_num = 1;
225     p_thread->p_opengl->fmt_in.i_sar_den = 1;
226     p_thread->p_opengl->fmt_render = p_thread->p_opengl->fmt_in;
227
228     p_thread->p_module =
229         module_need( p_thread->p_opengl, "opengl provider", NULL, 0 );
230     if( p_thread->p_module == NULL )
231     {
232         msg_Err( p_thread, "unable to initialize OpenGL" );
233         vlc_object_detach( p_thread->p_opengl );
234         vlc_object_release( p_thread->p_opengl );
235         vlc_restorecancel (canc);
236         return NULL;
237     }
238
239     p_thread->p_opengl->pf_init( p_thread->p_opengl );
240
241     setup_opengl( p_thread->i_width, p_thread->i_height );
242     CreateRenderTarget(512, &RenderTargetTextureID, NULL);
243
244     timestart=mdate()/1000;
245
246     while( vlc_object_alive (p_thread) )
247     {
248         mspf = 1000 / 60;
249         if( galaktos_update( p_thread ) == 1 )
250         {
251             vlc_object_kill( p_thread );
252         }
253         free( p_thread->psz_title );
254         p_thread->psz_title = NULL;
255
256         mtime_t now = mdate();
257         if (++count%100==0)
258         {
259             realfps=100/((now/1000-fpsstart)/1000);
260  //           printf("%f\n",realfps);
261             fpsstart=now/1000;
262         }
263         //framerate limiter
264         timed=mspf-(now/1000-timestart);
265       //   printf("%d,%d\n",time,mspf);
266         if (timed>0) msleep(1000*timed);
267     //     printf("Limiter %d\n",(mdate()/1000-timestart));
268         timestart=mdate()/1000;
269     }
270
271     /* Free the openGL provider */
272     module_unneed( p_thread->p_opengl, p_thread->p_module );
273     vlc_object_detach( p_thread->p_opengl );
274     vlc_object_release( p_thread->p_opengl );
275     vlc_restorecancel (canc);
276     return NULL;
277 }
278
279 /*****************************************************************************
280  * Close: close the plugin
281  *****************************************************************************/
282 static void Close( vlc_object_t *p_this )
283 {
284     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
285     aout_filter_sys_t *p_sys = p_filter->p_sys;
286
287     /* Stop galaktos Thread */
288     vlc_object_kill( p_sys->p_thread );
289
290     galaktos_done( p_sys->p_thread );
291
292     vlc_thread_join( p_sys->p_thread );
293
294     /* Free data */
295     vlc_object_detach( p_sys->p_thread );
296     vlc_object_release( p_sys->p_thread );
297
298     free( p_sys );
299 }
300
301 static char *TitleGet( vlc_object_t *p_this )
302 {
303     char *psz_title = NULL;
304     input_thread_t *p_input =
305         vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
306
307     if( p_input )
308     {
309         char *psz_orig = input_item_GetURI( input_GetItem( p_input ) );
310         char *psz = strrchr( psz_orig, '/' );
311
312         if( psz )
313         {
314             psz++;
315         }
316         else
317         {
318             psz = psz_orig;
319         }
320         if( psz && *psz )
321         {
322             psz_title = strdup( psz );
323         }
324         free( psz_orig );
325         vlc_object_release( p_input );
326     }
327
328     return psz_title;
329 }