]> git.sesse.net Git - vlc/blob - modules/visualization/scope/scope.c
asf: fixed some headers errors.
[vlc] / modules / visualization / scope / scope.c
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 $
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 <vlc/vlc.h>
32 #include <vlc/aout.h>
33 #include <vlc/vout.h>
34 #include "aout_internal.h"
35
36 #define SCOPE_WIDTH 320
37 #define SCOPE_HEIGHT 240
38 #define SCOPE_ASPECT (VOUT_ASPECT_FACTOR*SCOPE_WIDTH/SCOPE_HEIGHT)
39
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
47 {
48     aout_fifo_t *p_aout_fifo;
49     vout_thread_t *p_vout;
50 } aout_filter_sys_t;
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static int  Open         ( vlc_object_t * );             
56 static void Close        ( vlc_object_t * );                   
57
58 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
59                         aout_buffer_t * );
60
61 /*****************************************************************************
62  * Module descriptor
63  *****************************************************************************/
64 vlc_module_begin();
65     set_description( _("scope effect") ); 
66     set_capability( "audio filter", 0 );
67     set_callbacks( Open, Close );
68     add_shortcut( "scope" );
69 vlc_module_end();
70
71 /*****************************************************************************
72  * Open: open a scope effect plugin
73  *****************************************************************************/
74 static int Open( vlc_object_t *p_this )
75 {
76     aout_filter_t *p_aout = (aout_filter_t *)p_this;
77     aout_filter_t * p_filter = (aout_filter_t *)p_this;
78
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 )
81     {
82         msg_Warn( p_filter, "Bad input or output format" );
83         return -1;
84     }
85
86     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
87     {
88         msg_Warn( p_filter, "input and output formats are not similar" );
89         return -1;
90     }
91
92     p_filter->pf_do_work = DoWork;
93     p_filter->b_in_place = 1;
94
95     /* Allocate structure */
96     p_aout->p_sys = malloc( sizeof( aout_filter_sys_t ) );
97     if( p_aout->p_sys == NULL )
98     {
99         msg_Err( p_aout, "out of memory" );
100         return -1;
101     }
102
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 );
107
108     if( p_aout->p_sys->p_vout == NULL )
109     {
110         msg_Err( p_aout, "no suitable vout module" );
111         free( p_aout->p_sys );
112         return -1;
113     }
114
115     return( 0 );
116 }
117
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 )
125 {
126     picture_t *p_outpic;
127     int i_index, i_image;
128     byte_t *p_buffer = p_in_buf->p_buffer;
129     uint8_t  *ppp_area[2][3];
130     float *p_sample;
131
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++ )
135     {
136         /* Don't stay here forever */
137         if( mdate() >= p_in_buf->end_date - 10000 )
138         {
139             break;
140         }
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 ) )
143                   == NULL )
144         {
145             if( p_aout->b_die )
146             {
147                 return;
148             }
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 */
152         }
153
154         /* Blank the picture */
155         for( i_index = 0 ; i_index < p_outpic->i_planes ; i_index++ )
156         {
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 );
159         }
160
161         /* We only support 2 channels for now */
162         for( i_index = 0 ; i_index < 2 ; i_index++ )
163         {
164             int j;
165             for( j = 0 ; j < 3 ; j++ )
166             {
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;
170             }
171         }
172
173         for( i_index = 0, p_sample = (float*)p_buffer;
174              i_index < SCOPE_WIDTH;
175              i_index++ )
176         {
177             int i;
178             int i_tmp_value;
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;
184             p_sample++;
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;
188             p_sample++;
189             i_right_value = i_tmp_value / 256 + 128;
190             for( i = 0 ; i < 2 ; i++ )
191             {
192                 /* Left channel */
193                 *(ppp_area[0][0]
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;
197                 *(ppp_area[0][1]
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;
201
202                 /* Right channel */
203                 *(ppp_area[1][0]
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;
207                 *(ppp_area[1][2]
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;
211             }
212         }
213
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 );
218
219         p_buffer += SCOPE_WIDTH * 8;
220     }
221 }
222
223 /*****************************************************************************
224  * Close: close the plugin
225  *****************************************************************************/
226 static void Close( vlc_object_t *p_this )
227 {
228     aout_filter_t * p_filter = (aout_filter_t *)p_this;
229
230     /* Kill video output */
231     vout_Destroy( p_filter->p_sys->p_vout );
232
233     free( p_filter->p_sys );
234 }
235