]> git.sesse.net Git - vlc/blob - modules/visualization/goom.c
* modules/visualizations/goom.c: added --goom-width/height config variables.
[vlc] / modules / visualization / goom.c
1 /*****************************************************************************
2  * goom.c: based on libgoom (see http://ios.free.fr/?page=projet&quoi=1)
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>                                              /* strdup() */
29 #include <errno.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/input.h>
33 #include <vlc/aout.h>
34 #include <vlc/vout.h>
35 #include "aout_internal.h"
36
37 #include "goom_core.h"
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 static int  Open         ( vlc_object_t * );
43 static void Close        ( vlc_object_t * );
44
45 #define WIDTH_TEXT N_("Goom display width")
46 #define HEIGHT_TEXT N_("Goom display height")
47 #define RES_LONGTEXT N_("Allows you to change the resolution of the " \
48     "goom display (bigger resolution will be prettier but more CPU intensive)")
49
50 vlc_module_begin();
51     set_description( _("goom effect") );
52     set_capability( "audio filter", 0 );
53     add_integer( "goom-width", 320, NULL,
54                  WIDTH_TEXT, RES_LONGTEXT, VLC_FALSE );
55     add_integer( "goom-height", 240, NULL,
56                  HEIGHT_TEXT, RES_LONGTEXT, VLC_FALSE );
57     set_callbacks( Open, Close );
58     add_shortcut( "goom" );
59 vlc_module_end();
60
61 /*****************************************************************************
62  * Local prototypes
63  *****************************************************************************/
64
65 typedef struct
66 {
67     VLC_COMMON_MEMBERS
68     vout_thread_t *p_vout;
69
70     char          *psz_title;
71
72     vlc_mutex_t   lock;
73     vlc_cond_t    wait;
74
75     mtime_t       i_pts;
76     int           i_samples;
77     int16_t       samples[2][512];
78 } goom_thread_t;
79
80 typedef struct aout_filter_sys_t
81 {
82     goom_thread_t *p_thread;
83
84 } aout_filter_sys_t;
85
86 static void DoWork   ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
87                        aout_buffer_t * );
88
89 static void Thread   ( vlc_object_t * );
90
91 static char *TitleGet( vlc_object_t * );
92
93 /*****************************************************************************
94  * Open: open a scope effect plugin
95  *****************************************************************************/
96 static int Open( vlc_object_t *p_this )
97 {
98     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
99     aout_filter_sys_t *p_sys;
100     goom_thread_t     *p_thread;
101     vlc_value_t       width, height;
102
103     if ( p_filter->input.i_format != VLC_FOURCC('f','l','3','2' )
104          || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
105     {
106         msg_Warn( p_filter, "Bad input or output format" );
107         return VLC_EGENERIC;
108     }
109     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
110     {
111         msg_Warn( p_filter, "input and output formats are not similar" );
112         return VLC_EGENERIC;
113     }
114
115     p_filter->pf_do_work = DoWork;
116     p_filter->b_in_place = 1;
117
118     /* Allocate structure */
119     p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );
120
121     /* Create goom thread */
122     p_sys->p_thread = p_thread =
123         vlc_object_create( p_filter, sizeof( goom_thread_t ) );
124     vlc_object_attach( p_thread, p_this );
125
126     var_Create( p_thread, "goom-width", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
127     var_Get( p_thread, "goom-width", &width );
128     var_Create( p_thread, "goom-height", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
129     var_Get( p_thread, "goom-height", &height );
130
131     p_thread->p_vout =
132         vout_Request( p_filter, NULL, width.i_int, height.i_int,
133                       VLC_FOURCC('R','V','3','2'),
134                       VOUT_ASPECT_FACTOR * width.i_int/height.i_int );
135     if( p_thread->p_vout == NULL )
136     {
137         msg_Err( p_filter, "no suitable vout module" );
138         vlc_object_detach( p_thread );
139         vlc_object_destroy( p_thread );
140         free( p_sys );
141         return VLC_EGENERIC;
142     }
143     vlc_mutex_init( p_filter, &p_thread->lock );
144     vlc_cond_init( p_filter, &p_thread->wait );
145
146     p_thread->i_samples = 0;
147     memset( &p_thread->samples, 0, 512*2*2 );
148     p_thread->psz_title = TitleGet( VLC_OBJECT( p_filter ) );
149
150     if( vlc_thread_create( p_thread, "Goom Update Thread", Thread,
151                            VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
152     {
153         msg_Err( p_filter, "cannot lauch goom thread" );
154         vout_Destroy( p_thread->p_vout );
155         vlc_mutex_destroy( &p_thread->lock );
156         vlc_cond_destroy( &p_thread->wait );
157         if( p_thread->psz_title ) free( p_thread->psz_title );
158         vlc_object_detach( p_thread );
159         vlc_object_destroy( p_thread );
160         free( p_sys );
161         return VLC_EGENERIC;
162     }
163
164     return VLC_SUCCESS;
165 }
166
167 /*****************************************************************************
168  * Play: play a sound samples buffer
169  *****************************************************************************
170  * This function writes a buffer of i_length bytes in the socket
171  *****************************************************************************/
172 static inline int16_t FloatToInt16( float f )
173 {
174     if( f >= 1.0 )
175         return 32767;
176     else if( f < -1.0 )
177         return -32768;
178     else
179         return (int16_t)( f * 32768.0 );
180 }
181
182 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
183                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
184 {
185     aout_filter_sys_t *p_sys = p_filter->p_sys;
186
187     int16_t *p_isample;
188     float   *p_fsample = (float*)p_in_buf->p_buffer;
189     int     i_samples = 0;
190     int     i_channels = aout_FormatNbChannels( &p_filter->input );
191
192     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
193     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes;
194
195     /* copy samples */
196     vlc_mutex_lock( &p_sys->p_thread->lock );
197     p_sys->p_thread->i_pts = p_in_buf->start_date;
198     if( p_sys->p_thread->i_samples >= 512 )
199     {
200         p_sys->p_thread->i_samples = 0;
201     }
202     p_isample = &p_sys->p_thread->samples[0][p_sys->p_thread->i_samples];
203
204     i_samples = __MIN( 512 - p_sys->p_thread->i_samples,
205                        (int)p_out_buf->i_nb_samples );
206     p_sys->p_thread->i_samples += i_samples;
207
208     while( i_samples > 0 )
209     {
210         p_isample[0] = FloatToInt16( p_fsample[0] );
211         if( i_channels > 1 )
212         {
213             p_isample[512] = FloatToInt16( p_fsample[1] );
214         }
215
216         p_isample++;
217         p_fsample += i_channels;
218
219         i_samples--;
220     }
221     if( p_sys->p_thread->i_samples == 512 )
222     {
223         vlc_cond_signal( &p_sys->p_thread->wait );
224     }
225     vlc_mutex_unlock( &p_sys->p_thread->lock );
226
227 }
228 /*****************************************************************************
229  * Thread:
230  *****************************************************************************/
231 static void Thread( vlc_object_t *p_this )
232 {
233     goom_thread_t *p_thread = (goom_thread_t*)p_this;
234     vlc_value_t width, height;
235
236     var_Get( p_this, "goom-width", &width );
237     var_Get( p_this, "goom-height", &height );
238
239     goom_init( width.i_int, height.i_int, 0 );
240     goom_set_font( NULL, NULL, NULL );
241
242     while( !p_thread->b_die )
243     {
244         mtime_t   i_pts;
245         int16_t   data[2][512];
246         uint32_t  *plane;
247         picture_t *p_pic;
248
249         /* goom_update is damn slow, so just copy data and release the lock */
250         vlc_mutex_lock( &p_thread->lock );
251         vlc_cond_wait( &p_thread->wait, &p_thread->lock );
252         i_pts = p_thread->i_pts;
253         memcpy( data, p_thread->samples, 512 * 2 * 2 );
254         vlc_mutex_unlock( &p_thread->lock );
255
256         plane = goom_update( data, 0, 0.0, p_thread->psz_title, NULL );
257
258         if( p_thread->psz_title )
259         {
260             free( p_thread->psz_title );
261             p_thread->psz_title = NULL;
262         }
263         while( !( p_pic = vout_CreatePicture( p_thread->p_vout, 0, 0, 0 ) ) &&
264                !p_thread->b_die )
265         {
266             msleep( VOUT_OUTMEM_SLEEP );
267         }
268
269         if( p_pic == NULL )
270         {
271             break;
272         }
273
274         memcpy( p_pic->p[0].p_pixels, plane, width.i_int * height.i_int * 4 );
275         vout_DatePicture( p_thread->p_vout, p_pic, i_pts );
276         vout_DisplayPicture( p_thread->p_vout, p_pic );
277
278     }
279
280     goom_close();
281 }
282
283
284 /*****************************************************************************
285  * Close: close the plugin
286  *****************************************************************************/
287 static void Close( vlc_object_t *p_this )
288 {
289     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
290     aout_filter_sys_t *p_sys = p_filter->p_sys;
291
292     /* Stop Goom Thread */
293     p_sys->p_thread->b_die = VLC_TRUE;
294
295     vlc_mutex_lock( &p_sys->p_thread->lock );
296     vlc_cond_signal( &p_sys->p_thread->wait );
297     vlc_mutex_unlock( &p_sys->p_thread->lock );
298
299     vlc_thread_join( p_sys->p_thread );
300
301     /* Free data */
302     vout_Request( p_filter, p_sys->p_thread->p_vout, 0, 0, 0, 0 );
303     vlc_mutex_destroy( &p_sys->p_thread->lock );
304     vlc_cond_destroy( &p_sys->p_thread->wait );
305     vlc_object_detach( p_sys->p_thread );
306     vlc_object_destroy( p_sys->p_thread );
307
308     free( p_sys );
309 }
310
311
312 static char *TitleGet( vlc_object_t *p_this )
313 {
314     char *psz_title = NULL;
315     input_thread_t *p_input =
316         vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
317
318     if( p_input )
319     {
320         char *psz = strrchr( p_input->psz_source, '/' );
321
322         if( psz )
323         {
324             psz++;
325         }
326         else
327         {
328             psz = p_input->psz_source;
329         }
330         if( psz && *psz )
331         {
332             psz_title = strdup( psz );
333         }
334         vlc_object_release( p_input );
335     }
336
337     return psz_title;
338 }