]> git.sesse.net Git - vlc/blob - modules/visualization/galaktos/plugin.c
Merge branch 'master' of git@git.videolan.org:vlc
[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
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43 static int  Open         ( vlc_object_t * );
44 static void Close        ( vlc_object_t * );
45
46 vlc_module_begin();
47     set_description( _("GaLaktos visualization plugin") );
48     set_capability( "visualization", 0 );
49     set_callbacks( Open, Close );
50     add_shortcut( "galaktos" );
51 vlc_module_end();
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 typedef struct aout_filter_sys_t
57 {
58     galaktos_thread_t *p_thread;
59
60 } aout_filter_sys_t;
61
62 static void DoWork   ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
63                        aout_buffer_t * );
64
65 static void Thread   ( vlc_object_t * );
66
67 static char *TitleGet( vlc_object_t * );
68
69
70 extern GLuint RenderTargetTextureID;
71
72 /*****************************************************************************
73  * Open: open a scope effect plugin
74  *****************************************************************************/
75 static int Open( vlc_object_t *p_this )
76 {
77     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
78     aout_filter_sys_t *p_sys;
79     galaktos_thread_t *p_thread;
80
81     if ( p_filter->input.i_format != VLC_FOURCC('f','l','3','2' )
82          || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
83     {
84         msg_Warn( p_filter, "bad input or output format" );
85         return VLC_EGENERIC;
86     }
87     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
88     {
89         msg_Warn( p_filter, "input and output formats are not similar" );
90         return VLC_EGENERIC;
91     }
92
93     p_filter->pf_do_work = DoWork;
94     p_filter->b_in_place = 1;
95
96     /* Allocate structure */
97     p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );
98
99     /* Create galaktos thread */
100     p_sys->p_thread = p_thread =
101         vlc_object_create( p_filter, sizeof( galaktos_thread_t ) );
102     vlc_object_attach( p_thread, p_this );
103
104 /*
105     var_Create( p_thread, "galaktos-width", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
106     var_Get( p_thread, "galaktos-width", &width );
107     var_Create( p_thread, "galaktos-height", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
108     var_Get( p_thread, "galaktos-height", &height );
109 */
110     p_thread->i_cur_sample = 0;
111     bzero( p_thread->p_data, 2*2*512 );
112
113     p_thread->i_width = 600;
114     p_thread->i_height = 600;
115     p_thread->b_fullscreen = 0;
116     galaktos_init( p_thread );
117
118     p_thread->i_channels = aout_FormatNbChannels( &p_filter->input );
119
120     p_thread->psz_title = TitleGet( VLC_OBJECT( p_filter ) );
121
122     if( vlc_thread_create( p_thread, "galaktos update thread", Thread,
123                            VLC_THREAD_PRIORITY_LOW, false ) )
124     {
125         msg_Err( p_filter, "cannot lauch galaktos thread" );
126         free( p_thread->psz_title );
127         vlc_object_detach( p_thread );
128         vlc_object_release( p_thread );
129         free( p_sys );
130         return VLC_EGENERIC;
131     }
132
133     return VLC_SUCCESS;
134 }
135
136 /*****************************************************************************
137  * float to s16 conversion
138  *****************************************************************************/
139 static inline int16_t FloatToInt16( float f )
140 {
141     if( f >= 1.0 )
142         return 32767;
143     else if( f < -1.0 )
144         return -32768;
145     else
146         return (int16_t)( f * 32768.0 );
147 }
148
149 /*****************************************************************************
150  * DoWork: process samples buffer
151  *****************************************************************************
152  * This function queues the audio buffer to be processed by the galaktos thread
153  *****************************************************************************/
154 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
155                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
156 {
157     int i_samples;
158     int i_channels;
159     float *p_float;
160     galaktos_thread_t *p_thread = p_filter->p_sys->p_thread;
161
162     p_float = (float *)p_in_buf->p_buffer;
163     i_channels = p_thread->i_channels;
164
165     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
166     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes;
167
168     for( i_samples = p_in_buf->i_nb_samples; i_samples > 0; i_samples-- )
169     {
170         int i_cur_sample = p_thread->i_cur_sample;
171
172         p_thread->p_data[0][i_cur_sample] = FloatToInt16( p_float[0] );
173         if( i_channels > 1 )
174         {
175             p_thread->p_data[1][i_cur_sample] = FloatToInt16( p_float[1] );
176         }
177         p_float += i_channels;
178
179         if( ++(p_thread->i_cur_sample) == 512 )
180         {
181             addPCM( p_thread->p_data );
182             p_thread->i_cur_sample = 0;
183         }
184     }
185 }
186
187 /*****************************************************************************
188  * Thread:
189  *****************************************************************************/
190 static void Thread( vlc_object_t *p_this )
191 {
192     galaktos_thread_t *p_thread = (galaktos_thread_t*)p_this;
193
194     int count=0;
195     double realfps=0,fpsstart=0;
196     int timed=0;
197     int timestart=0;
198     int mspf=0;
199
200     /* Get on OpenGL provider */
201     p_thread->p_opengl =
202         (vout_thread_t *)vlc_object_create( p_this, VLC_OBJECT_OPENGL );
203     if( p_thread->p_opengl == NULL )
204     {
205         msg_Err( p_thread, "out of memory" );
206         return;
207     }
208     vlc_object_attach( p_thread->p_opengl, p_this );
209
210     /* Initialize vout parameters */
211     vout_InitFormat( &p_thread->p_opengl->fmt_in,
212                      VLC_FOURCC('R','V','3','2'),
213                      p_thread->i_width, p_thread->i_height, 1 );
214     p_thread->p_opengl->i_window_width = p_thread->i_width;
215     p_thread->p_opengl->i_window_height = p_thread->i_height;
216     p_thread->p_opengl->render.i_width = p_thread->i_width;
217     p_thread->p_opengl->render.i_height = p_thread->i_width;
218     p_thread->p_opengl->render.i_aspect = VOUT_ASPECT_FACTOR;
219     p_thread->p_opengl->b_scale = true;
220     p_thread->p_opengl->b_fullscreen = false;
221     p_thread->p_opengl->i_alignment = 0;
222     p_thread->p_opengl->fmt_in.i_sar_num = 1;
223     p_thread->p_opengl->fmt_in.i_sar_den = 1;
224     p_thread->p_opengl->fmt_render = p_thread->p_opengl->fmt_in;
225
226     p_thread->p_module =
227         module_Need( p_thread->p_opengl, "opengl provider", NULL, 0 );
228     if( p_thread->p_module == NULL )
229     {
230         msg_Err( p_thread, "unable to initialize OpenGL" );
231         vlc_object_detach( p_thread->p_opengl );
232         vlc_object_release( p_thread->p_opengl );
233         return;
234     }
235
236     p_thread->p_opengl->pf_init( p_thread->p_opengl );
237
238     setup_opengl( p_thread->i_width, p_thread->i_height );
239     CreateRenderTarget(512, &RenderTargetTextureID, NULL);
240
241     timestart=mdate()/1000;
242
243     while( !p_thread->b_die )
244     {
245         mspf = 1000 / 60;
246         if( galaktos_update( p_thread ) == 1 )
247         {
248             vlc_object_kill( p_thread );
249         }
250         free( p_thread->psz_title );
251         p_thread->psz_title = NULL;
252
253         if (++count%100==0)
254         {
255             realfps=100/((mdate()/1000-fpsstart)/1000);
256  //           printf("%f\n",realfps);
257             fpsstart=mdate()/1000;
258         }
259         //framerate limiter
260         timed=mspf-(mdate()/1000-timestart);
261       //   printf("%d,%d\n",time,mspf);
262         if (timed>0) msleep(1000*timed);
263     //     printf("Limiter %d\n",(mdate()/1000-timestart));
264         timestart=mdate()/1000;
265     }
266
267     /* Free the openGL provider */
268     module_Unneed( p_thread->p_opengl, p_thread->p_module );
269     vlc_object_detach( p_thread->p_opengl );
270     vlc_object_release( p_thread->p_opengl );
271 }
272
273 /*****************************************************************************
274  * Close: close the plugin
275  *****************************************************************************/
276 static void Close( vlc_object_t *p_this )
277 {
278     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
279     aout_filter_sys_t *p_sys = p_filter->p_sys;
280
281     /* Stop galaktos Thread */
282     vlc_object_kill( p_sys->p_thread );
283
284     galaktos_done( p_sys->p_thread );
285
286     vlc_thread_join( p_sys->p_thread );
287
288     /* Free data */
289     vlc_object_detach( p_sys->p_thread );
290     vlc_object_release( p_sys->p_thread );
291
292     free( p_sys );
293 }
294
295 static char *TitleGet( vlc_object_t *p_this )
296 {
297     char *psz_title = NULL;
298     input_thread_t *p_input =
299         vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
300
301     if( p_input )
302     {
303         char *psz_orig = input_item_GetURI( input_GetItem( p_input ) );
304         char *psz = strrchr( psz_orig, '/' );
305
306         if( psz )
307         {
308             psz++;
309         }
310         else
311         {
312             psz = psz_orig;
313         }
314         if( psz && *psz )
315         {
316             psz_title = strdup( psz );
317         }
318         free( psz_orig );
319         vlc_object_release( p_input );
320     }
321
322     return psz_title;
323 }