]> git.sesse.net Git - vlc/blob - plugins/fx/scope.c
* ./extras/MacOSX_dvdioctl: removed outdated files.
[vlc] / plugins / fx / scope.c
1 /*****************************************************************************
2  * scope.c : Scope effect module
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: scope.c,v 1.5 2002/03/18 19:14:52 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
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 <videolan/vlc.h>
32
33 #include "video.h"
34 #include "video_output.h"
35
36 #include "audio_output.h"                                   /* aout_thread_t */
37
38 #define SCOPE_WIDTH 320
39 #define SCOPE_HEIGHT 240
40 #define SCOPE_ASPECT (VOUT_ASPECT_FACTOR*SCOPE_WIDTH/SCOPE_HEIGHT)
41
42 /*****************************************************************************
43  * Capabilities defined in the other files.
44  *****************************************************************************/
45 static void aout_getfunctions( function_list_t * p_function_list );
46
47 /*****************************************************************************
48  * aout_sys_t: scope audio output method descriptor
49  *****************************************************************************
50  * This structure is part of the audio output thread descriptor.
51  * It describes some scope specific variables.
52  *****************************************************************************/
53 typedef struct aout_sys_s
54 {
55     struct aout_thread_s aout;
56     struct aout_fifo_s *p_aout_fifo;
57
58     struct vout_thread_s *p_vout;
59
60 } aout_sys_t;
61
62 /*****************************************************************************
63  * Build configuration tree.
64  *****************************************************************************/
65 MODULE_CONFIG_START
66 MODULE_CONFIG_STOP
67
68 MODULE_INIT_START
69     SET_DESCRIPTION( "scope effect module" )
70     ADD_CAPABILITY( AOUT, 0 )
71     ADD_SHORTCUT( "scope" )
72 MODULE_INIT_STOP
73
74 MODULE_ACTIVATE_START
75     aout_getfunctions( &p_module->p_functions->aout );
76 MODULE_ACTIVATE_STOP
77
78 MODULE_DEACTIVATE_START
79 MODULE_DEACTIVATE_STOP
80
81 /*****************************************************************************
82  * Local prototypes.
83  *****************************************************************************/
84 static int     aout_Open        ( aout_thread_t *p_aout );
85 static int     aout_SetFormat   ( aout_thread_t *p_aout );
86 static int     aout_GetBufInfo  ( aout_thread_t *p_aout, int i_buffer_info );
87 static void    aout_Play        ( aout_thread_t *p_aout,
88                                   byte_t *buffer, int i_size );
89 static void    aout_Close       ( aout_thread_t *p_aout );
90
91 /*****************************************************************************
92  * Functions exported as capabilities. They are declared as static so that
93  * we don't pollute the namespace too much.
94  *****************************************************************************/
95 static void aout_getfunctions( function_list_t * p_function_list )
96 {
97     p_function_list->functions.aout.pf_open = aout_Open;
98     p_function_list->functions.aout.pf_setformat = aout_SetFormat;
99     p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
100     p_function_list->functions.aout.pf_play = aout_Play;
101     p_function_list->functions.aout.pf_close = aout_Close;
102 }
103
104 /*****************************************************************************
105  * aout_Open: open a scope effect plugin
106  *****************************************************************************/
107 static int aout_Open( aout_thread_t *p_aout )
108 {
109     char *psz_method;
110
111     /* Allocate structure */
112     p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
113     if( p_aout->p_sys == NULL )
114     {
115         intf_ErrMsg("error: %s", strerror(ENOMEM) );
116         return -1;
117     }
118
119     psz_method = config_GetPszVariable( "aout" );
120     if( psz_method )
121     {
122         if( !*psz_method )
123         {
124             free( psz_method );
125             return -1;
126         }
127     }
128     else
129     {
130         return -1;
131     }
132
133     /* Open video output */
134     p_aout->p_sys->p_vout =
135         vout_CreateThread( NULL, SCOPE_WIDTH, SCOPE_HEIGHT,
136                            FOURCC_I420, SCOPE_ASPECT );
137
138     if( p_aout->p_sys->p_vout == NULL )
139     {
140         intf_ErrMsg( "aout scope error: no suitable vout module" );
141         free( p_aout->p_sys );
142         return -1;
143     }
144
145     /* Open audio output  */
146     p_aout->p_sys->aout.i_format   = p_aout->i_format;
147     p_aout->p_sys->aout.i_rate     = p_aout->i_rate;
148     p_aout->p_sys->aout.i_channels = p_aout->i_channels;
149
150     p_aout->p_sys->aout.p_module = module_Need( MODULE_CAPABILITY_AOUT, "",
151                                     (void *)&p_aout->p_sys->aout );
152     if( p_aout->p_sys->aout.p_module == NULL )
153     {
154         intf_ErrMsg( "aout scope error: no suitable aout module" );
155         vout_DestroyThread( p_aout->p_sys->p_vout, NULL );
156         free( p_aout->p_sys );
157         return -1;
158     }
159
160 #define aout_functions p_aout->p_sys->aout.p_module->p_functions->aout.functions.aout
161     p_aout->p_sys->aout.pf_open       = aout_functions.pf_open;
162     p_aout->p_sys->aout.pf_setformat  = aout_functions.pf_setformat;
163     p_aout->p_sys->aout.pf_getbufinfo = aout_functions.pf_getbufinfo;
164     p_aout->p_sys->aout.pf_play       = aout_functions.pf_play;
165     p_aout->p_sys->aout.pf_close      = aout_functions.pf_close;
166 #undef aout_functions
167     
168     return( 0 );
169 }
170
171 /*****************************************************************************
172  * aout_SetFormat: set the output format
173  *****************************************************************************/
174 static int aout_SetFormat( aout_thread_t *p_aout )
175 {
176     int i_ret;
177
178     /* Force the output method */
179     p_aout->p_sys->aout.i_format = p_aout->i_format;
180     p_aout->p_sys->aout.i_channels = p_aout->i_channels;
181     p_aout->p_sys->aout.i_rate = p_aout->i_rate;
182
183     /*
184      * Initialize audio device
185      */
186     i_ret = p_aout->p_sys->aout.pf_setformat( &p_aout->p_sys->aout );
187
188     if( i_ret )
189     {
190         return i_ret;
191     }
192
193     if( p_aout->p_sys->aout.i_format != p_aout->i_format
194          || p_aout->p_sys->aout.i_channels != p_aout->i_channels )
195     {
196         intf_ErrMsg( "aout error: plugin isn't cooperative" );
197         return 0;
198     }
199
200     p_aout->i_channels = p_aout->p_sys->aout.i_channels;
201     p_aout->i_format = p_aout->p_sys->aout.i_format;
202     p_aout->i_rate = p_aout->p_sys->aout.i_rate;
203
204     return 0;
205 }
206
207 /*****************************************************************************
208  * aout_GetBufInfo: buffer status query
209  *****************************************************************************/
210 static int aout_GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
211 {
212     return p_aout->p_sys->aout.pf_getbufinfo( &p_aout->p_sys->aout,
213                                               i_buffer_limit );
214 }
215
216 /*****************************************************************************
217  * aout_Play: play a sound samples buffer
218  *****************************************************************************
219  * This function writes a buffer of i_length bytes in the socket
220  *****************************************************************************/
221 static void aout_Play( aout_thread_t *p_aout, byte_t *p_buffer, int i_size )
222 {
223     picture_t *p_outpic;
224     int i_index, i_image;
225     u8  *ppp_area[2][3];
226     u16 *p_sample;
227
228     /* Play the real sound */
229     p_aout->p_sys->aout.pf_play( &p_aout->p_sys->aout, p_buffer, i_size );
230
231     for( i_image = 0; (i_image + 1) * SCOPE_WIDTH * 8 < i_size ; i_image++ )
232     {
233         /* Don't stay here forever */
234         if( mdate() >= p_aout->date - 10000 )
235         {
236             break;
237         }
238
239         /* This is a new frame. Get a structure from the video_output. */
240         while( ( p_outpic = vout_CreatePicture( p_aout->p_sys->p_vout, 0, 0, 0 ) )
241                   == NULL )
242         {
243             if( p_aout->b_die )
244             {
245                 return;
246             }
247             msleep( VOUT_OUTMEM_SLEEP );
248         }
249
250         /* Blank the picture */
251         for( i_index = 0 ; i_index < p_outpic->i_planes ; i_index++ )
252         {
253             memset( p_outpic->p[i_index].p_pixels, i_index ? 0x80 : 0x00,
254                     p_outpic->p[i_index].i_lines * p_outpic->p[i_index].i_pitch );
255         }
256
257         /* We only support 2 channels for now */
258         for( i_index = 0 ; i_index < 2 ; i_index++ )
259         {
260             int j;
261             for( j = 0 ; j < 3 ; j++ )
262             {
263                 ppp_area[i_index][j] =
264                     p_outpic->p[j].p_pixels + i_index * p_outpic->p[j].i_lines
265                                 / p_aout->i_channels * p_outpic->p[j].i_pitch;
266             }
267         }
268
269         for( i_index = 0, p_sample = (u16*)p_buffer;
270              i_index < SCOPE_WIDTH;
271              i_index++ )
272         {
273             int i;
274             u8 i_value;
275
276             for( i = 0 ; i < 2 ; i++ )
277             {
278                 /* Left channel */
279                 i_value = *p_sample++ / 256 + 128;
280                 *(ppp_area[0][0]
281                    + p_outpic->p[0].i_pitch * i_index / SCOPE_WIDTH
282                    + p_outpic->p[0].i_lines * i_value / 512
283                        * p_outpic->p[0].i_pitch) = 0xbf;
284                 *(ppp_area[0][1]
285                    + p_outpic->p[1].i_pitch * i_index / SCOPE_WIDTH
286                    + p_outpic->p[1].i_lines * i_value / 512
287                       * p_outpic->p[1].i_pitch) = 0xff;
288
289                 /* Right channel */
290                 i_value = *p_sample++ / 256 + 128;
291                 *(ppp_area[1][0]
292                    + p_outpic->p[0].i_pitch * i_index / SCOPE_WIDTH
293                    + p_outpic->p[0].i_lines * i_value / 512
294                       * p_outpic->p[0].i_pitch) = 0x9f;
295                 *(ppp_area[1][2]
296                    + p_outpic->p[2].i_pitch * i_index / SCOPE_WIDTH
297                    + p_outpic->p[2].i_lines * i_value / 512
298                       * p_outpic->p[2].i_pitch) = 0xdd;
299             }
300         }
301
302         /* Display the picture - FIXME: find a better date :-) */
303         vout_DatePicture( p_aout->p_sys->p_vout, p_outpic,
304                           p_aout->date + i_image * 20000 );
305         vout_DisplayPicture( p_aout->p_sys->p_vout, p_outpic );
306
307         p_buffer += SCOPE_WIDTH * 4;
308     }
309 }
310
311 /*****************************************************************************
312  * aout_Close: close the Esound socket
313  *****************************************************************************/
314 static void aout_Close( aout_thread_t *p_aout )
315 {
316     p_aout->p_sys->aout.pf_close( &p_aout->p_sys->aout );
317     module_Unneed( p_aout->p_sys->aout.p_module );
318     vout_DestroyThread( p_aout->p_sys->p_vout, NULL );
319     free( p_aout->p_sys );
320 }
321