1 /*****************************************************************************
2 * scope.c : Scope effect module
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: scope.c,v 1.4 2003/08/19 18:51:03 sigmunau Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
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() */
34 #include "aout_internal.h"
36 #define SCOPE_WIDTH 320
37 #define SCOPE_HEIGHT 240
38 #define SCOPE_ASPECT (VOUT_ASPECT_FACTOR*SCOPE_WIDTH/SCOPE_HEIGHT)
40 /*****************************************************************************
41 * aout_sys_t: scope audio output method descriptor
42 *****************************************************************************
43 * This structure is part of the audio output thread descriptor.
44 * It describes some scope specific variables.
45 *****************************************************************************/
46 typedef struct aout_filter_sys_t
48 aout_fifo_t *p_aout_fifo;
49 vout_thread_t *p_vout;
52 /*****************************************************************************
54 *****************************************************************************/
55 static int Open ( vlc_object_t * );
56 static void Close ( vlc_object_t * );
58 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
61 /*****************************************************************************
63 *****************************************************************************/
65 set_description( _("scope effect") );
66 set_capability( "audio filter", 0 );
67 set_callbacks( Open, Close );
68 add_shortcut( "scope" );
71 /*****************************************************************************
72 * Open: open a scope effect plugin
73 *****************************************************************************/
74 static int Open( vlc_object_t *p_this )
76 aout_filter_t *p_aout = (aout_filter_t *)p_this;
77 aout_filter_t * p_filter = (aout_filter_t *)p_this;
79 if ( p_filter->input.i_format != VLC_FOURCC('f','l','3','2') //AOUT_FMT_U16_NE
80 || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )//AOUT_FMT_U16_NE )
82 msg_Warn( p_filter, "Bad input or output format" );
86 if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
88 msg_Warn( p_filter, "input and output formats are not similar" );
92 p_filter->pf_do_work = DoWork;
93 p_filter->b_in_place = 1;
95 /* Allocate structure */
96 p_aout->p_sys = malloc( sizeof( aout_filter_sys_t ) );
97 if( p_aout->p_sys == NULL )
99 msg_Err( p_aout, "out of memory" );
103 /* Open video output */
104 p_aout->p_sys->p_vout =
105 vout_Create( p_aout, SCOPE_WIDTH, SCOPE_HEIGHT,
106 VLC_FOURCC('I','4','2','0'), SCOPE_ASPECT );
108 if( p_aout->p_sys->p_vout == NULL )
110 msg_Err( p_aout, "no suitable vout module" );
111 free( p_aout->p_sys );
118 /*****************************************************************************
119 * Play: play a sound samples buffer
120 *****************************************************************************
121 * This function writes a buffer of i_length bytes in the socket
122 *****************************************************************************/
123 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
124 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
127 int i_index, i_image;
128 byte_t *p_buffer = p_in_buf->p_buffer;
129 uint8_t *ppp_area[2][3];
132 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
133 p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes;
134 for( i_image = 0; (i_image + 1) * SCOPE_WIDTH < p_in_buf->i_nb_samples ; i_image++ )
136 /* Don't stay here forever */
137 if( mdate() >= p_in_buf->end_date - 10000 )
141 /* This is a new frame. Get a structure from the video_output. */
142 while( ( p_outpic = vout_CreatePicture( p_filter->p_sys->p_vout, 0, 0, 0 ) )
149 msleep( 1 );/* Not sleeping here makes us use 100% cpu,
150 * sleeping too much absolutly kills audio
151 * quality. 1 seems to be a good value */
154 /* Blank the picture */
155 for( i_index = 0 ; i_index < p_outpic->i_planes ; i_index++ )
157 memset( p_outpic->p[i_index].p_pixels, i_index ? 0x80 : 0x00,
158 p_outpic->p[i_index].i_lines * p_outpic->p[i_index].i_pitch );
161 /* We only support 2 channels for now */
162 for( i_index = 0 ; i_index < 2 ; i_index++ )
165 for( j = 0 ; j < 3 ; j++ )
167 ppp_area[i_index][j] =
168 p_outpic->p[j].p_pixels + 3 * i_index * p_outpic->p[j].i_lines
169 / p_filter->input.i_original_channels * p_outpic->p[j].i_pitch;
173 for( i_index = 0, p_sample = (float*)p_buffer;
174 i_index < SCOPE_WIDTH;
179 uint8_t i_right_value, i_left_value;
180 if ( *p_sample >= 1.0 ) i_tmp_value = 32767;
181 else if ( *p_sample < -1.0 ) i_tmp_value = -32768;
182 else i_tmp_value = *p_sample * 32768.0;
183 i_left_value = i_tmp_value / 256 + 128;
185 if ( *p_sample >= 1.0 ) i_tmp_value = 32767;
186 else if ( *p_sample < -1.0 ) i_tmp_value = -32768;
187 else i_tmp_value = *p_sample * 32768.0;
189 i_right_value = i_tmp_value / 256 + 128;
190 for( i = 0 ; i < 2 ; i++ )
194 + p_outpic->p[0].i_pitch * i_index / SCOPE_WIDTH
195 + p_outpic->p[0].i_lines * i_left_value / 512
196 * p_outpic->p[0].i_pitch) = 0xbf;
198 + p_outpic->p[1].i_pitch * i_index / SCOPE_WIDTH
199 + p_outpic->p[1].i_lines * i_left_value / 512
200 * p_outpic->p[1].i_pitch) = 0xff;
204 + p_outpic->p[0].i_pitch * i_index / SCOPE_WIDTH
205 + p_outpic->p[0].i_lines * i_right_value / 512
206 * p_outpic->p[0].i_pitch) = 0x9f;
208 + p_outpic->p[2].i_pitch * i_index / SCOPE_WIDTH
209 + p_outpic->p[2].i_lines * i_right_value / 512
210 * p_outpic->p[2].i_pitch) = 0xdd;
214 /* Display the picture - FIXME: find a better date :-) */
215 vout_DatePicture( p_filter->p_sys->p_vout, p_outpic,
216 p_in_buf->start_date + i_image * 20000 );
217 vout_DisplayPicture( p_filter->p_sys->p_vout, p_outpic );
219 p_buffer += SCOPE_WIDTH * 8;
223 /*****************************************************************************
224 * Close: close the plugin
225 *****************************************************************************/
226 static void Close( vlc_object_t *p_this )
228 aout_filter_t * p_filter = (aout_filter_t *)p_this;
230 /* Kill video output */
231 vout_Destroy( p_filter->p_sys->p_vout );
233 free( p_filter->p_sys );