]> git.sesse.net Git - vlc/blob - include/aout_internal.h
* Fixed another bunch of memory leaks.
[vlc] / include / aout_internal.h
1 /*****************************************************************************
2  * aout_internal.h : internal defines for audio output
3  *****************************************************************************
4  * Copyright (C) 2002 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * aout_alloc_t : allocation of memory in the audio output
26  *****************************************************************************/
27 typedef struct aout_alloc_t
28 {
29     int                     i_alloc_type;
30     int                     i_bytes_per_sec;
31 } aout_alloc_t;
32
33 #define AOUT_ALLOC_NONE     0
34 #define AOUT_ALLOC_STACK    1
35 #define AOUT_ALLOC_HEAP     2
36
37 #if defined( __APPLE__ ) || defined( SYS_BSD )
38 #undef HAVE_ALLOCA
39 #endif
40
41 #ifdef HAVE_ALLOCA
42 #   define ALLOCA_TEST( p_alloc, p_new_buffer )                             \
43         if ( (p_alloc)->i_alloc_type == AOUT_ALLOC_STACK )                  \
44         {                                                                   \
45             (p_new_buffer) = alloca( i_alloc_size + sizeof(aout_buffer_t) );\
46             i_alloc_type = AOUT_ALLOC_STACK;                                \
47         }                                                                   \
48         else
49 #else
50 #   define ALLOCA_TEST( p_alloc, p_new_buffer )
51 #endif
52
53 #define aout_BufferAlloc( p_alloc, i_nb_usec, p_previous_buffer,            \
54                           p_new_buffer )                                    \
55     if ( (p_alloc)->i_alloc_type == AOUT_ALLOC_NONE )                       \
56     {                                                                       \
57         (p_new_buffer) = p_previous_buffer;                                 \
58     }                                                                       \
59     else                                                                    \
60     {                                                                       \
61         int i_alloc_size, i_alloc_type;                                     \
62         i_alloc_size = (int)( (uint64_t)(p_alloc)->i_bytes_per_sec          \
63                                             * (i_nb_usec) / 1000000 + 1 );  \
64         ALLOCA_TEST( p_alloc, p_new_buffer )                                \
65         {                                                                   \
66             (p_new_buffer) = malloc( i_alloc_size + sizeof(aout_buffer_t) );\
67             i_alloc_type = AOUT_ALLOC_HEAP;                                 \
68         }                                                                   \
69         if ( p_new_buffer != NULL )                                         \
70         {                                                                   \
71             (p_new_buffer)->i_alloc_type = i_alloc_type;                    \
72             (p_new_buffer)->i_size = i_alloc_size;                          \
73             (p_new_buffer)->p_buffer = (byte_t *)(p_new_buffer)             \
74                                          + sizeof(aout_buffer_t);           \
75             if ( (p_previous_buffer) != NULL )                              \
76             {                                                               \
77                 (p_new_buffer)->start_date =                                \
78                            ((aout_buffer_t *)p_previous_buffer)->start_date;\
79                 (p_new_buffer)->end_date =                                  \
80                            ((aout_buffer_t *)p_previous_buffer)->end_date;  \
81             }                                                               \
82         }                                                                   \
83         /* we'll keep that for a while --Meuuh */                           \
84         /* else printf("%s:%d\n", __FILE__, __LINE__); */                   \
85     }
86
87 #define aout_BufferFree( p_buffer )                                         \
88     if( p_buffer != NULL && (p_buffer)->i_alloc_type == AOUT_ALLOC_HEAP )   \
89     {                                                                       \
90         free( p_buffer );                                                   \
91     }                                                                       \
92     p_buffer = NULL;
93
94 /*****************************************************************************
95  * aout_fifo_t : audio output buffer FIFO
96  *****************************************************************************/
97 struct aout_fifo_t
98 {
99     aout_buffer_t *         p_first;
100     aout_buffer_t **        pp_last;
101     audio_date_t            end_date;
102 };
103
104 /*****************************************************************************
105  * aout_filter_t : audio output filter
106  *****************************************************************************/
107 struct aout_filter_t
108 {
109     VLC_COMMON_MEMBERS
110
111     audio_sample_format_t   input;
112     audio_sample_format_t   output;
113     aout_alloc_t            output_alloc;
114
115     module_t *              p_module;
116     struct aout_filter_sys_t * p_sys;
117     void                 (* pf_do_work)( struct aout_instance_t *,
118                                          struct aout_filter_t *,
119                                          struct aout_buffer_t *,
120                                          struct aout_buffer_t * );
121     vlc_bool_t              b_in_place;
122     vlc_bool_t              b_continuity;
123 };
124
125 /*****************************************************************************
126  * aout_mixer_t : audio output mixer
127  *****************************************************************************/
128 typedef struct aout_mixer_t
129 {
130     audio_sample_format_t   mixer;
131     aout_alloc_t            output_alloc;
132
133     module_t *              p_module;
134     struct aout_mixer_sys_t * p_sys;
135     void                 (* pf_do_work)( struct aout_instance_t *,
136                                          struct aout_buffer_t * );
137
138     /* If b_error == 1, there is no mixer. */
139     vlc_bool_t              b_error;
140     /* Multiplier used to raise or lower the volume of the sound in
141      * software. Beware, this creates sound distortion and should be avoided
142      * as much as possible. This isn't available for non-float32 mixer. */
143     float                   f_multiplier;
144 } aout_mixer_t;
145
146 /*****************************************************************************
147  * aout_input_t : input stream for the audio output
148  *****************************************************************************/
149 #define AOUT_RESAMPLING_NONE     0
150 #define AOUT_RESAMPLING_UP       1
151 #define AOUT_RESAMPLING_DOWN     2
152 struct aout_input_t
153 {
154     /* When this lock is taken, the pipeline cannot be changed by a
155      * third-party. */
156     vlc_mutex_t             lock;
157
158     /* The input thread that spawned this input */
159     input_thread_t         *p_input_thread;
160
161     audio_sample_format_t   input;
162     aout_alloc_t            input_alloc;
163
164     /* pre-filters */
165     aout_filter_t *         pp_filters[AOUT_MAX_FILTERS];
166     int                     i_nb_filters;
167
168     /* resamplers */
169     aout_filter_t *         pp_resamplers[AOUT_MAX_FILTERS];
170     int                     i_nb_resamplers;
171     int                     i_resampling_type;
172     mtime_t                 i_resamp_start_date;
173     int                     i_resamp_start_drift;
174
175     aout_fifo_t             fifo;
176
177     /* Mixer information */
178     byte_t *                p_first_byte_to_mix;
179
180     /* If b_restart == 1, the input pipeline will be re-created. */
181     vlc_bool_t              b_restart;
182
183     /* If b_error == 1, there is no input pipeline. */
184     vlc_bool_t              b_error;
185
186     /* Did we just change the output format? (expect buffer inconsistencies) */
187     vlc_bool_t              b_changed;
188
189     /* internal caching delay from input */
190     int                     i_pts_delay;
191     /* desynchronisation delay request by the user */
192     int                     i_desync;
193
194 };
195
196 /*****************************************************************************
197  * aout_output_t : output stream for the audio output
198  *****************************************************************************/
199 typedef struct aout_output_t
200 {
201     audio_sample_format_t   output;
202     /* Indicates whether the audio output is currently starving, to avoid
203      * printing a 1,000 "output is starving" messages. */
204     vlc_bool_t              b_starving;
205
206     /* post-filters */
207     aout_filter_t *         pp_filters[AOUT_MAX_FILTERS];
208     int                     i_nb_filters;
209
210     aout_fifo_t             fifo;
211
212     struct module_t *       p_module;
213     struct aout_sys_t *     p_sys;
214     void                 (* pf_play)( aout_instance_t * );
215     int                  (* pf_volume_get )( aout_instance_t *, audio_volume_t * );
216     int                  (* pf_volume_set )( aout_instance_t *, audio_volume_t );
217     int                  (* pf_volume_infos )( aout_instance_t *, audio_volume_t * );
218     int                     i_nb_samples;
219
220     /* Current volume for the output - it's just a placeholder, the plug-in
221      * may or may not use it. */
222     audio_volume_t          i_volume;
223
224     /* If b_error == 1, there is no audio output pipeline. */
225     vlc_bool_t              b_error;
226 } aout_output_t;
227
228 /*****************************************************************************
229  * aout_instance_t : audio output thread descriptor
230  *****************************************************************************/
231 struct aout_instance_t
232 {
233     VLC_COMMON_MEMBERS
234
235     /* Locks : please note that if you need several of these locks, it is
236      * mandatory (to avoid deadlocks) to take them in the following order :
237      * mixer_lock, p_input->lock, output_fifo_lock, input_fifos_lock.
238      * --Meuuh */
239     /* When input_fifos_lock is taken, none of the p_input->fifo structures
240      * can be read or modified by a third-party thread. */
241     vlc_mutex_t             input_fifos_lock;
242     /* When mixer_lock is taken, all decoder threads willing to mix a
243      * buffer must wait until it is released. The output pipeline cannot
244      * be modified. No input stream can be added or removed. */
245     vlc_mutex_t             mixer_lock;
246     /* When output_fifo_lock is taken, the p_aout->output.fifo structure
247      * cannot be read or written  by a third-party thread. */
248     vlc_mutex_t             output_fifo_lock;
249
250     /* Input streams & pre-filters */
251     aout_input_t *          pp_inputs[AOUT_MAX_INPUTS];
252     int                     i_nb_inputs;
253
254     /* Mixer */
255     aout_mixer_t            mixer;
256
257     /* Output plug-in */
258     aout_output_t           output;
259 };
260
261 /*****************************************************************************
262  * Prototypes
263  *****************************************************************************/
264 /* From input.c : */
265 int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input );
266 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input );
267 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
268                     aout_buffer_t * p_buffer );
269
270 /* From filters.c : */
271 VLC_EXPORT( int, aout_FiltersCreatePipeline, ( aout_instance_t * p_aout, aout_filter_t ** pp_filters, int * pi_nb_filters, const audio_sample_format_t * p_input_format, const audio_sample_format_t * p_output_format ) );
272 VLC_EXPORT( void, aout_FiltersDestroyPipeline, ( aout_instance_t * p_aout, aout_filter_t ** pp_filters, int i_nb_filters ) );
273 VLC_EXPORT( void, aout_FiltersPlay, ( aout_instance_t * p_aout, aout_filter_t ** pp_filters, int i_nb_filters, aout_buffer_t ** pp_input_buffer ) );
274 void aout_FiltersHintBuffers( aout_instance_t * p_aout, aout_filter_t ** pp_filters, int i_nb_filters, aout_alloc_t * p_first_alloc );
275
276 /* From mixer.c : */
277 int aout_MixerNew( aout_instance_t * p_aout );
278 void aout_MixerDelete( aout_instance_t * p_aout );
279 void aout_MixerRun( aout_instance_t * p_aout );
280 int aout_MixerMultiplierSet( aout_instance_t * p_aout, float f_multiplier );
281 int aout_MixerMultiplierGet( aout_instance_t * p_aout, float * pf_multiplier );
282
283 /* From output.c : */
284 int aout_OutputNew( aout_instance_t * p_aout,
285                     audio_sample_format_t * p_format );
286 void aout_OutputPlay( aout_instance_t * p_aout, aout_buffer_t * p_buffer );
287 void aout_OutputDelete( aout_instance_t * p_aout );
288 VLC_EXPORT( aout_buffer_t *, aout_OutputNextBuffer, ( aout_instance_t *, mtime_t, vlc_bool_t ) );
289
290 /* From common.c : */
291 VLC_EXPORT( unsigned int, aout_FormatNbChannels, ( const audio_sample_format_t * p_format ) );
292 VLC_EXPORT( void, aout_FormatPrepare, ( audio_sample_format_t * p_format ) );
293 VLC_EXPORT( void, aout_FormatPrint, ( aout_instance_t * p_aout, const char * psz_text, const audio_sample_format_t * p_format ) );
294 VLC_EXPORT( void, aout_FormatsPrint, ( aout_instance_t * p_aout, const char * psz_text, const audio_sample_format_t * p_format1, const audio_sample_format_t * p_format2 ) );
295 VLC_EXPORT( const char *, aout_FormatPrintChannels, ( const audio_sample_format_t * ) );
296 void aout_FifoInit( aout_instance_t *, aout_fifo_t *, uint32_t );
297 mtime_t aout_FifoNextStart( aout_instance_t *, aout_fifo_t * );
298 void aout_FifoPush( aout_instance_t *, aout_fifo_t *, aout_buffer_t * );
299 void aout_FifoSet( aout_instance_t *, aout_fifo_t *, mtime_t );
300 void aout_FifoMoveDates( aout_instance_t *, aout_fifo_t *, mtime_t );
301 VLC_EXPORT( aout_buffer_t *, aout_FifoPop, ( aout_instance_t * p_aout, aout_fifo_t * p_fifo ) );
302 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo );
303 VLC_EXPORT( mtime_t, aout_FifoFirstDate, ( aout_instance_t *, aout_fifo_t * ) );
304
305 /* From intf.c :*/
306 VLC_EXPORT( void, aout_VolumeSoftInit, ( aout_instance_t * ) );
307 int aout_VolumeSoftGet( aout_instance_t *, audio_volume_t * );
308 int aout_VolumeSoftSet( aout_instance_t *, audio_volume_t );
309 int aout_VolumeSoftInfos( aout_instance_t *, audio_volume_t * );
310 VLC_EXPORT( void, aout_VolumeNoneInit, ( aout_instance_t * ) );
311 int aout_VolumeNoneGet( aout_instance_t *, audio_volume_t * );
312 int aout_VolumeNoneSet( aout_instance_t *, audio_volume_t );
313 int aout_VolumeNoneInfos( aout_instance_t *, audio_volume_t * );
314