]> git.sesse.net Git - vlc/blob - modules/visualization/galaktos/plugin.c
e8043e51702fac435101944f7807dacecc4a3a60
[vlc] / modules / visualization / galaktos / plugin.c
1 /*****************************************************************************
2  * plugin.c:
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #include "plugin.h"
32 #include "glx.h"
33 #include "main.h"
34 #include "PCM.h"
35 #include "video_init.h"
36 #include <GL/glu.h>
37
38 #include <vlc/input.h>
39 #include <vlc/vout.h>
40 #include "aout_internal.h"
41
42 /*****************************************************************************
43  * Module descriptor
44  *****************************************************************************/
45 static int  Open         ( vlc_object_t * );
46 static void Close        ( vlc_object_t * );
47
48 vlc_module_begin();
49     set_description( _("GaLaktos visualization plugin") );
50     set_capability( "audio filter", 0 );
51     set_callbacks( Open, Close );
52     add_shortcut( "galaktos" );
53 vlc_module_end();
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 typedef struct aout_filter_sys_t
59 {
60     galaktos_thread_t *p_thread;
61
62 } aout_filter_sys_t;
63
64 static void DoWork   ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
65                        aout_buffer_t * );
66
67 static void Thread   ( vlc_object_t * );
68
69 static char *TitleGet( vlc_object_t * );
70
71
72 extern GLuint RenderTargetTextureID;
73
74 /*****************************************************************************
75  * Open: open a scope effect plugin
76  *****************************************************************************/
77 static int Open( vlc_object_t *p_this )
78 {
79     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
80     aout_filter_sys_t *p_sys;
81     galaktos_thread_t     *p_thread;
82     vlc_value_t       width, height;
83
84     if ( p_filter->input.i_format != VLC_FOURCC('f','l','3','2' )
85          || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
86     {
87         msg_Warn( p_filter, "Bad input or output format" );
88         return VLC_EGENERIC;
89     }
90     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
91     {
92         msg_Warn( p_filter, "input and output formats are not similar" );
93         return VLC_EGENERIC;
94     }
95
96     p_filter->pf_do_work = DoWork;
97     p_filter->b_in_place = 1;
98
99     /* Allocate structure */
100     p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );
101
102     /* Create galaktos thread */
103     p_sys->p_thread = p_thread =
104         vlc_object_create( p_filter, sizeof( galaktos_thread_t ) );
105     vlc_object_attach( p_thread, p_this );
106
107 /*
108     var_Create( p_thread, "galaktos-width", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
109     var_Get( p_thread, "galaktos-width", &width );
110     var_Create( p_thread, "galaktos-height", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
111     var_Get( p_thread, "galaktos-height", &height );
112 */
113     p_thread->i_cur_sample = 0;
114     bzero( p_thread->p_data, 2*2*512 );
115
116     p_thread->i_width = 600;
117     p_thread->i_height = 600;
118     p_thread->b_fullscreen = 0;
119     galaktos_glx_init( p_thread );
120     galaktos_init( p_thread );
121
122     p_thread->i_channels = aout_FormatNbChannels( &p_filter->input );
123
124     p_thread->psz_title = TitleGet( VLC_OBJECT( p_filter ) );
125
126     if( vlc_thread_create( p_thread, "galaktos update thread", Thread,
127                            VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
128     {
129         msg_Err( p_filter, "cannot lauch galaktos thread" );
130         if( p_thread->psz_title ) free( p_thread->psz_title );
131         vlc_object_detach( p_thread );
132         vlc_object_destroy( p_thread );
133         free( p_sys );
134         return VLC_EGENERIC;
135     }
136
137     return VLC_SUCCESS;
138 }
139
140
141 /*****************************************************************************
142  * float to s16 conversion
143  *****************************************************************************/
144 static inline int16_t FloatToInt16( float f )
145 {
146     if( f >= 1.0 )
147         return 32767;
148     else if( f < -1.0 )
149         return -32768;
150     else
151         return (int16_t)( f * 32768.0 );
152 }
153
154
155 /*****************************************************************************
156  * DoWork: process samples buffer
157  *****************************************************************************
158  * This function queues the audio buffer to be processed by the galaktos thread
159  *****************************************************************************/
160 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
161                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
162 {
163     int i_samples;
164     int i_channels;
165     float *p_float;
166     galaktos_thread_t *p_thread = p_filter->p_sys->p_thread;
167
168     p_float = (float *)p_in_buf->p_buffer;
169     i_channels = p_thread->i_channels;
170
171     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
172     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes;
173
174     for( i_samples = p_in_buf->i_nb_samples; i_samples > 0; i_samples-- )
175     {
176         int i_cur_sample = p_thread->i_cur_sample;
177
178         p_thread->p_data[0][i_cur_sample] = FloatToInt16( p_float[0] );
179         if( i_channels > 1 )
180         {
181             p_thread->p_data[1][i_cur_sample] = FloatToInt16( p_float[1] );
182         }
183         p_float += i_channels;
184
185         if( ++(p_thread->i_cur_sample) == 512 )
186         {
187             addPCM( p_thread->p_data );
188             p_thread->i_cur_sample = 0;
189         }
190     }
191 }
192
193 /*****************************************************************************
194  * Thread:
195  *****************************************************************************/
196 static void Thread( vlc_object_t *p_this )
197 {
198     galaktos_thread_t *p_thread = (galaktos_thread_t*)p_this;
199
200     int count=0;
201     double realfps=0,fpsstart=0;
202     int timed=0;
203     int timestart=0;
204     int mspf=0;
205
206     galaktos_glx_activate_window( p_thread );
207     setup_opengl( p_thread->i_width, p_thread->i_height );
208     CreateRenderTarget(512, &RenderTargetTextureID, NULL);
209
210     timestart=mdate()/1000;
211
212     while( !p_thread->b_die )
213     {
214         mspf = 1000 / 60;
215         if( galaktos_update( p_thread ) == 1 )
216         {
217             p_thread->b_die = 1;
218         }
219         if( p_thread->psz_title )
220         {
221             free( p_thread->psz_title );
222             p_thread->psz_title = NULL;
223         }
224
225         if (++count%100==0)
226         {
227             realfps=100/((mdate()/1000-fpsstart)/1000);
228  //           printf("%f\n",realfps);
229             fpsstart=mdate()/1000;
230         }
231         //framerate limiter
232         timed=mspf-(mdate()/1000-timestart);
233       //   printf("%d,%d\n",time,mspf);
234         if (timed>0) msleep(1000*timed);
235     //     printf("Limiter %d\n",(mdate()/1000-timestart));
236         timestart=mdate()/1000;
237     }
238
239     galaktos_glx_done( p_thread );
240 }
241
242 /*****************************************************************************
243  * Close: close the plugin
244  *****************************************************************************/
245 static void Close( vlc_object_t *p_this )
246 {
247     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
248     aout_filter_sys_t *p_sys = p_filter->p_sys;
249
250     /* Stop galaktos Thread */
251     p_sys->p_thread->b_die = VLC_TRUE;
252
253     galaktos_done( p_sys->p_thread );
254
255     vlc_thread_join( p_sys->p_thread );
256
257     /* Free data */
258     vlc_object_detach( p_sys->p_thread );
259
260     vlc_object_destroy( p_sys->p_thread );
261
262     free( p_sys );
263 }
264
265 static char *TitleGet( vlc_object_t *p_this )
266 {
267     char *psz_title = NULL;
268     input_thread_t *p_input =
269         vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
270
271     if( p_input )
272     {
273         char *psz = strrchr( p_input->input.p_item->psz_uri, '/' );
274
275         if( psz )
276         {
277             psz++;
278         }
279         else
280         {
281             psz = p_input->input.p_item->psz_uri;
282         }
283         if( psz && *psz )
284         {
285             psz_title = strdup( psz );
286         }
287         vlc_object_release( p_input );
288     }
289
290     return psz_title;
291 }