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.2 2003/08/23 22:49:50 fenrir Exp $
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 #define GOOM_WIDTH 160
40 #define GOOM_HEIGHT 120
41 #define GOOM_ASPECT (VOUT_ASPECT_FACTOR*GOOM_WIDTH/GOOM_HEIGHT)
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close ( vlc_object_t * );
50 set_description( _("goom effect") );
51 set_capability( "audio filter", 0 );
52 set_callbacks( Open, Close );
53 add_shortcut( "goom" );
57 /*****************************************************************************
59 *****************************************************************************/
64 vout_thread_t *p_vout;
73 int16_t samples[2][512];
76 typedef struct aout_filter_sys_t
78 goom_thread_t *p_thread;
82 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
85 static void Thread ( vlc_object_t * );
87 static char *TitleGet( vlc_object_t * );
89 /*****************************************************************************
90 * Open: open a scope effect plugin
91 *****************************************************************************/
92 static int Open( vlc_object_t *p_this )
94 aout_filter_t *p_filter = (aout_filter_t *)p_this;
95 aout_filter_sys_t *p_sys;
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') )
100 msg_Warn( p_filter, "Bad input or output format" );
103 if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
105 msg_Warn( p_filter, "input and output formats are not similar" );
109 p_filter->pf_do_work = DoWork;
110 p_filter->b_in_place = 1;
112 /* Allocate structure */
113 p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );
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'),
122 if( p_sys->p_thread->p_vout == NULL )
124 msg_Err( p_filter, "no suitable vout module" );
125 vlc_object_destroy( p_sys->p_thread );
126 free( p_filter->p_sys );
129 vlc_mutex_init( p_filter, &p_sys->p_thread->lock );
130 vlc_cond_init( p_filter, &p_sys->p_thread->wait );
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 ) );
136 if( vlc_thread_create( p_sys->p_thread, "Goom Update Thread", Thread,
137 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
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 )
145 free( p_sys->p_thread->psz_title );
147 vlc_object_destroy( p_sys->p_thread );
148 free( p_filter->p_sys );
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 )
167 return (int16_t)( f * 32768.0 );
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 )
173 aout_filter_sys_t *p_sys = p_filter->p_sys;
176 float *p_fsample = (float*)p_in_buf->p_buffer;
178 int i_channels = aout_FormatNbChannels( &p_filter->input );
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;
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 )
189 p_sys->p_thread->i_samples = 0;
191 p_isample = &p_sys->p_thread->samples[0][p_sys->p_thread->i_samples];
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;
197 while( i_samples > 0 )
199 p_isample[0] = FloatToInt16( p_fsample[0] );
202 p_isample[512] = FloatToInt16( p_fsample[1] );
206 p_fsample += i_channels;
210 if( p_sys->p_thread->i_samples == 512 )
212 vlc_cond_signal( &p_sys->p_thread->wait );
214 vlc_mutex_unlock( &p_sys->p_thread->lock );
217 /*****************************************************************************
219 *****************************************************************************/
220 static void Thread( vlc_object_t *p_this )
222 goom_thread_t *p_thread = (goom_thread_t*)p_this;
224 goom_init( GOOM_WIDTH, GOOM_HEIGHT, 0 );
225 goom_set_font( NULL, NULL, NULL );
227 while( !p_thread->b_die )
230 int16_t data[2][512];
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 );
241 plane = goom_update( data, 0, 0.0, p_thread->psz_title, NULL );
243 if( p_thread->psz_title )
245 free( p_thread->psz_title );
246 p_thread->psz_title = NULL;
248 while( ( p_pic = vout_CreatePicture( p_thread->p_vout, 0, 0, 0 ) ) == NULL &&
251 msleep( VOUT_OUTMEM_SLEEP );
254 if( p_thread->b_die || p_pic == NULL )
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 );
269 /*****************************************************************************
270 * Close: close the plugin
271 *****************************************************************************/
272 static void Close( vlc_object_t *p_this )
274 aout_filter_t *p_filter = (aout_filter_t *)p_this;
275 aout_filter_sys_t *p_sys = p_filter->p_sys;
277 /* Stop Goom Thread */
278 p_sys->p_thread->b_die = VLC_TRUE;
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 );
284 vlc_thread_join( p_sys->p_thread );
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 );
296 static char *TitleGet( vlc_object_t *p_this )
298 input_thread_t *p_input = vlc_object_find( p_this,
299 VLC_OBJECT_INPUT, FIND_ANYWHERE);
300 char *psz_title = NULL;
304 char *psz = strrchr( p_input->psz_source, '/' );
312 psz = p_input->psz_source;
316 psz_title = strdup( psz );
318 vlc_object_release( p_input );