1 /*****************************************************************************
2 * goom.c: based on libgoom (see http://ios.free.fr/?page=projet&quoi=1)
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
7 * Authors: Laurent Aimar
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <string.h> /* strdup() */
32 #include <vlc/input.h>
35 #include "aout_internal.h"
37 #include "goom_core.h"
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Open ( vlc_object_t * );
43 static void Close ( vlc_object_t * );
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)")
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" );
61 /*****************************************************************************
63 *****************************************************************************/
68 vout_thread_t *p_vout;
77 int16_t samples[2][512];
80 typedef struct aout_filter_sys_t
82 goom_thread_t *p_thread;
86 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
89 static void Thread ( vlc_object_t * );
91 static char *TitleGet( vlc_object_t * );
93 /*****************************************************************************
94 * Open: open a scope effect plugin
95 *****************************************************************************/
96 static int Open( vlc_object_t *p_this )
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;
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') )
106 msg_Warn( p_filter, "Bad input or output format" );
109 if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
111 msg_Warn( p_filter, "input and output formats are not similar" );
115 p_filter->pf_do_work = DoWork;
116 p_filter->b_in_place = 1;
118 /* Allocate structure */
119 p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );
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 );
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 );
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 )
137 msg_Err( p_filter, "no suitable vout module" );
138 vlc_object_detach( p_thread );
139 vlc_object_destroy( p_thread );
143 vlc_mutex_init( p_filter, &p_thread->lock );
144 vlc_cond_init( p_filter, &p_thread->wait );
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 ) );
150 if( vlc_thread_create( p_thread, "Goom Update Thread", Thread,
151 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
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 );
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 )
179 return (int16_t)( f * 32768.0 );
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 )
185 aout_filter_sys_t *p_sys = p_filter->p_sys;
188 float *p_fsample = (float*)p_in_buf->p_buffer;
190 int i_channels = aout_FormatNbChannels( &p_filter->input );
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;
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 )
200 p_sys->p_thread->i_samples = 0;
202 p_isample = &p_sys->p_thread->samples[0][p_sys->p_thread->i_samples];
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;
208 while( i_samples > 0 )
210 p_isample[0] = FloatToInt16( p_fsample[0] );
213 p_isample[512] = FloatToInt16( p_fsample[1] );
217 p_fsample += i_channels;
221 if( p_sys->p_thread->i_samples == 512 )
223 vlc_cond_signal( &p_sys->p_thread->wait );
225 vlc_mutex_unlock( &p_sys->p_thread->lock );
228 /*****************************************************************************
230 *****************************************************************************/
231 static void Thread( vlc_object_t *p_this )
233 goom_thread_t *p_thread = (goom_thread_t*)p_this;
234 vlc_value_t width, height;
236 var_Get( p_this, "goom-width", &width );
237 var_Get( p_this, "goom-height", &height );
239 goom_init( width.i_int, height.i_int, 0 );
240 goom_set_font( NULL, NULL, NULL );
242 while( !p_thread->b_die )
245 int16_t data[2][512];
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 );
256 plane = goom_update( data, 0, 0.0, p_thread->psz_title, NULL );
258 if( p_thread->psz_title )
260 free( p_thread->psz_title );
261 p_thread->psz_title = NULL;
263 while( !( p_pic = vout_CreatePicture( p_thread->p_vout, 0, 0, 0 ) ) &&
266 msleep( VOUT_OUTMEM_SLEEP );
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 );
284 /*****************************************************************************
285 * Close: close the plugin
286 *****************************************************************************/
287 static void Close( vlc_object_t *p_this )
289 aout_filter_t *p_filter = (aout_filter_t *)p_this;
290 aout_filter_sys_t *p_sys = p_filter->p_sys;
292 /* Stop Goom Thread */
293 p_sys->p_thread->b_die = VLC_TRUE;
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 );
299 vlc_thread_join( p_sys->p_thread );
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 );
312 static char *TitleGet( vlc_object_t *p_this )
314 char *psz_title = NULL;
315 input_thread_t *p_input =
316 vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
320 char *psz = strrchr( p_input->psz_source, '/' );
328 psz = p_input->psz_source;
332 psz_title = strdup( psz );
334 vlc_object_release( p_input );