* goom: visual effects using goom. (Not yet enabled)
[vlc.git] / 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: goom.c,v 1.1 2003/08/23 17:23:45 fenrir Exp $
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/goom_core.h"
38
39 #define GOOM_WIDTH 160
40 #define GOOM_HEIGHT 120
41 #define GOOM_ASPECT (VOUT_ASPECT_FACTOR*GOOM_WIDTH/GOOM_HEIGHT)
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 static int  Open         ( vlc_object_t * );
47 static void Close        ( vlc_object_t * );
48
49 vlc_module_begin();
50     set_description( _("goom effect") );
51     set_capability( "audio filter", 0 );
52     set_callbacks( Open, Close );
53     add_shortcut( "goom" );
54 vlc_module_end();
55
56
57 /*****************************************************************************
58  * Local prototypes
59  *****************************************************************************/
60
61 typedef struct
62 {
63     VLC_COMMON_MEMBERS
64     vout_thread_t *p_vout;
65
66     char          *psz_title;
67
68     vlc_mutex_t   lock;
69     vlc_cond_t    wait;
70
71     mtime_t       i_pts;
72     int           i_samples;
73     int16_t       samples[2][512];
74 } goom_thread_t;
75
76 typedef struct aout_filter_sys_t
77 {
78     goom_thread_t *p_thread;
79
80 } aout_filter_sys_t;
81
82 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
83                         aout_buffer_t * );
84
85 static void Thread    ( vlc_object_t * );
86
87 static char *TitleGet( vlc_object_t * );
88
89 /*****************************************************************************
90  * Open: open a scope effect plugin
91  *****************************************************************************/
92 static int Open( vlc_object_t *p_this )
93 {
94     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
95     aout_filter_sys_t *p_sys;
96
97     if ( p_filter->input.i_format != VLC_FOURCC('f','l','3','2' )
98          || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
99     {
100         msg_Warn( p_filter, "Bad input or output format" );
101         return -1;
102     }
103     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
104     {
105         msg_Warn( p_filter, "input and output formats are not similar" );
106         return -1;
107     }
108
109     p_filter->pf_do_work = DoWork;
110     p_filter->b_in_place = 1;
111
112     /* Allocate structure */
113     p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );
114
115     /* Create goom thread */
116     p_sys->p_thread = vlc_object_create( p_filter,
117                                          sizeof( goom_thread_t ) );
118     p_sys->p_thread->p_vout = vout_Request( p_filter, NULL,
119                                            GOOM_WIDTH, GOOM_HEIGHT,
120                                            VLC_FOURCC('R','V','3','2'),
121                                            GOOM_ASPECT );
122     if( p_sys->p_thread->p_vout == NULL )
123     {
124         msg_Err( p_filter, "no suitable vout module" );
125         vlc_object_destroy( p_sys->p_thread );
126         free( p_filter->p_sys );
127         return VLC_EGENERIC;
128     }
129     vlc_mutex_init( p_filter, &p_sys->p_thread->lock );
130     vlc_cond_init( p_filter, &p_sys->p_thread->wait );
131
132     p_sys->p_thread->i_samples = 0;
133     memset( &p_sys->p_thread->samples, 0, 512*2*2 );
134     p_sys->p_thread->psz_title = TitleGet( VLC_OBJECT( p_filter ) );
135
136     if( vlc_thread_create( p_sys->p_thread, "Goom Update Thread", Thread,
137                            VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
138     {
139         msg_Err( p_filter, "cannot lauch goom thread" );
140         vout_Destroy( p_sys->p_thread->p_vout );
141         vlc_mutex_destroy( &p_sys->p_thread->lock );
142         vlc_cond_destroy( &p_sys->p_thread->wait );
143         if( p_sys->p_thread->psz_title )
144         {
145             free( p_sys->p_thread->psz_title );
146         }
147         vlc_object_destroy( p_sys->p_thread );
148         free( p_filter->p_sys );
149         return VLC_EGENERIC;
150     }
151
152     return( 0 );
153 }
154
155 /*****************************************************************************
156  * Play: play a sound samples buffer
157  *****************************************************************************
158  * This function writes a buffer of i_length bytes in the socket
159  *****************************************************************************/
160 static inline int16_t FloatToInt16( float f )
161 {
162     if( f >= 1.0 )
163         return 32767;
164     else if( f < -1.0 )
165         return -32768;
166     else
167         return (int16_t)( f * 32768.0 );
168 }
169
170 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
171                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
172 {
173     aout_filter_sys_t *p_sys = p_filter->p_sys;
174
175     int16_t *p_isample;
176     float   *p_fsample = (float*)p_in_buf->p_buffer;
177     int     i_samples = 0;
178     int     i_channels = aout_FormatNbChannels( &p_filter->input );
179
180
181     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
182     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes;
183
184     /* copy samples */
185     vlc_mutex_lock( &p_sys->p_thread->lock );
186     p_sys->p_thread->i_pts = p_in_buf->start_date;
187     if( p_sys->p_thread->i_samples >= 512 )
188     {
189         p_sys->p_thread->i_samples = 0;
190     }
191     p_isample = &p_sys->p_thread->samples[0][p_sys->p_thread->i_samples];
192
193     i_samples = __MIN( 512 - p_sys->p_thread->i_samples,
194                        (int)p_out_buf->i_nb_samples );
195     p_sys->p_thread->i_samples += i_samples;
196
197     while( i_samples > 0 )
198     {
199         p_isample[0] = FloatToInt16( p_fsample[0] );
200         if( i_channels > 1 )
201         {
202             p_isample[512] = FloatToInt16( p_fsample[1] );
203         }
204
205         p_isample++;
206         p_fsample += i_channels;
207
208         i_samples--;
209     }
210     if( p_sys->p_thread->i_samples == 512 )
211     {
212         vlc_cond_signal( &p_sys->p_thread->wait );
213     }
214     vlc_mutex_unlock( &p_sys->p_thread->lock );
215
216 }
217 /*****************************************************************************
218  * Thread:
219  *****************************************************************************/
220 static void Thread( vlc_object_t *p_this )
221 {
222     goom_thread_t *p_thread = (goom_thread_t*)p_this;
223
224     goom_init( GOOM_WIDTH, GOOM_HEIGHT, 0 );
225     goom_set_font( NULL, NULL, NULL );
226
227     while( !p_thread->b_die )
228     {
229         mtime_t   i_pts;
230         int16_t   data[2][512];
231         uint32_t  *plane;
232         picture_t *p_pic;
233
234         /* goom_update is damn slow, so just copy data and release the lock */
235         vlc_mutex_lock( &p_thread->lock );
236         vlc_cond_wait( &p_thread->wait, &p_thread->lock );
237         i_pts = p_thread->i_pts;
238         memcpy( data, p_thread->samples, 512 * 2 * 2 );
239         vlc_mutex_unlock( &p_thread->lock );
240
241         plane = goom_update( data, 0, 0.0, p_thread->psz_title, NULL );
242
243         if( p_thread->psz_title )
244         {
245             free( p_thread->psz_title );
246             p_thread->psz_title = NULL;
247         }
248         while( ( p_pic = vout_CreatePicture( p_thread->p_vout, 0, 0, 0 ) ) == NULL &&
249                !p_thread->b_die )
250         {
251             msleep( VOUT_OUTMEM_SLEEP );
252         }
253
254         if( p_thread->b_die || p_pic == NULL )
255         {
256             break;
257         }
258
259         memcpy( p_pic->p[0].p_pixels, plane, GOOM_WIDTH * GOOM_HEIGHT * 4 );
260         vout_DatePicture( p_thread->p_vout, p_pic, i_pts );
261         vout_DisplayPicture( p_thread->p_vout, p_pic );
262
263     }
264
265     goom_close();
266 }
267
268
269 /*****************************************************************************
270  * Close: close the plugin
271  *****************************************************************************/
272 static void Close( vlc_object_t *p_this )
273 {
274     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
275     aout_filter_sys_t *p_sys = p_filter->p_sys;
276
277     /* Stop Goom Thread */
278     p_sys->p_thread->b_die = VLC_TRUE;
279
280     vlc_mutex_lock( &p_sys->p_thread->lock );
281     vlc_cond_signal( &p_sys->p_thread->wait );
282     vlc_mutex_unlock( &p_sys->p_thread->lock );
283
284     vlc_thread_join( p_sys->p_thread );
285
286     /* Free data */
287     vout_Request( p_filter, p_sys->p_thread->p_vout, 0, 0, 0, 0 );
288     vlc_mutex_destroy( &p_sys->p_thread->lock );
289     vlc_cond_destroy( &p_sys->p_thread->wait );
290     vlc_object_destroy( p_sys->p_thread );
291
292     free( p_sys );
293 }
294
295
296 static char *TitleGet( vlc_object_t *p_this )
297 {
298     input_thread_t *p_input = vlc_object_find( p_this,
299                                                VLC_OBJECT_INPUT, FIND_ANYWHERE);
300     char           *psz_title = NULL;
301
302     if( p_input )
303     {
304         char *psz = strrchr( p_input->psz_source, '/' );
305
306         if( psz )
307         {
308             psz++;
309         }
310         else
311         {
312             psz = p_input->psz_source;
313         }
314         if( psz && *psz )
315         {
316             psz_title = strdup( psz );
317         }
318         vlc_object_release( p_input );
319     }
320
321     return psz_title;
322 }
323