* Major API change of the audio output. New aout_Dec* functions.
[vlc.git] / include / aout_internal.h
1 /*****************************************************************************
2  * aout_internal.h : internal defines for audio output
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: aout_internal.h,v 1.21 2002/09/26 22:40:18 massiot Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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 #define aout_BufferAlloc( p_alloc, i_nb_usec, p_previous_buffer,            \
38                           p_new_buffer )                                    \
39     if ( (p_alloc)->i_alloc_type == AOUT_ALLOC_NONE )                       \
40     {                                                                       \
41         (p_new_buffer) = p_previous_buffer;                                 \
42     }                                                                       \
43     else                                                                    \
44     {                                                                       \
45         int i_alloc_size;                                                   \
46         i_alloc_size = (u64)(p_alloc)->i_bytes_per_sec                      \
47                                             * (i_nb_usec) / 1000000 + 1;    \
48         if ( (p_alloc)->i_alloc_type == AOUT_ALLOC_STACK )                  \
49         {                                                                   \
50             (p_new_buffer) = alloca( i_alloc_size + sizeof(aout_buffer_t) );\
51         }                                                                   \
52         else                                                                \
53         {                                                                   \
54             (p_new_buffer) = malloc( i_alloc_size + sizeof(aout_buffer_t) );\
55         }                                                                   \
56         if ( p_new_buffer != NULL )                                         \
57         {                                                                   \
58             (p_new_buffer)->i_alloc_type = (p_alloc)->i_alloc_type;         \
59             (p_new_buffer)->i_size = i_alloc_size;                          \
60             (p_new_buffer)->p_buffer = (byte_t *)(p_new_buffer)             \
61                                          + sizeof(aout_buffer_t);           \
62             if ( (p_previous_buffer) != NULL )                              \
63             {                                                               \
64                 (p_new_buffer)->start_date =                                \
65                            ((aout_buffer_t *)p_previous_buffer)->start_date;\
66                 (p_new_buffer)->end_date =                                  \
67                            ((aout_buffer_t *)p_previous_buffer)->end_date;  \
68             }                                                               \
69         }                                                                   \
70         /* we'll keep that for a while --Meuuh */                           \
71         /* else printf("%s:%d\n", __FILE__, __LINE__); */                   \
72     }
73
74 #define aout_BufferFree( p_buffer )                                         \
75     if ( (p_buffer)->i_alloc_type == AOUT_ALLOC_HEAP )                      \
76     {                                                                       \
77         free( p_buffer );                                                   \
78     }
79
80 /*****************************************************************************
81  * aout_fifo_t : audio output buffer FIFO
82  *****************************************************************************/
83 struct aout_fifo_t
84 {
85     aout_buffer_t *         p_first;
86     aout_buffer_t **        pp_last;
87     audio_date_t            end_date;
88 };
89
90 /*****************************************************************************
91  * aout_filter_t : audio output filter
92  *****************************************************************************/
93 typedef struct aout_filter_t
94 {
95     VLC_COMMON_MEMBERS
96
97     audio_sample_format_t   input;
98     audio_sample_format_t   output;
99     aout_alloc_t            output_alloc;
100
101     module_t *              p_module;
102     struct aout_filter_sys_t * p_sys;
103     void                 (* pf_do_work)( struct aout_instance_t *,
104                                          struct aout_filter_t *,
105                                          struct aout_buffer_t *,
106                                          struct aout_buffer_t * );
107     vlc_bool_t              b_in_place;
108 } aout_filter_t;
109
110 /*****************************************************************************
111  * aout_mixer_t : audio output mixer
112  *****************************************************************************/
113 typedef struct aout_mixer_t
114 {
115     audio_sample_format_t   mixer;
116     aout_alloc_t            output_alloc;
117
118     module_t *              p_module;
119     struct aout_mixer_sys_t * p_sys;
120     void                 (* pf_do_work)( struct aout_instance_t *,
121                                          struct aout_buffer_t * );
122
123     /* If b_error == 1, there is no mixer nor audio output pipeline. */
124     vlc_bool_t              b_error;
125     /* Multiplier used to raise or lower the volume of the sound in
126      * software. Beware, this creates sound distortion and should be avoided
127      * as much as possible. This isn't available for non-float32 mixer. */
128     float                   f_multiplier;
129 } aout_mixer_t;
130
131 /*****************************************************************************
132  * aout_input_t : input stream for the audio output
133  *****************************************************************************/
134 struct aout_input_t
135 {
136     /* When this lock is taken, the pipeline cannot be changed by a
137      * third-party. */
138     vlc_mutex_t             lock;
139
140     audio_sample_format_t   input;
141     aout_alloc_t            input_alloc;
142
143     /* pre-filters */
144     aout_filter_t *         pp_filters[AOUT_MAX_FILTERS];
145     int                     i_nb_filters;
146
147     aout_fifo_t             fifo;
148
149     /* Mixer information */
150     byte_t *                p_first_byte_to_mix;
151
152     /* If b_error == 1, there is no input pipeline. */
153     vlc_bool_t              b_error;
154     /* Did we just change the output format ? (expect buffer inconsistencies) */
155     vlc_bool_t              b_changed;
156 };
157
158 /*****************************************************************************
159  * aout_output_t : output stream for the audio output
160  *****************************************************************************/
161 typedef struct aout_output_t
162 {
163     audio_sample_format_t   output;
164
165     /* post-filters */
166     aout_filter_t *         pp_filters[AOUT_MAX_FILTERS];
167     int                     i_nb_filters;
168
169     aout_fifo_t             fifo;
170
171     struct module_t *       p_module;
172     struct aout_sys_t *     p_sys;
173     void                 (* pf_play)( aout_instance_t * );
174     int                  (* pf_volume_get )( aout_instance_t *, audio_volume_t * );
175     int                  (* pf_volume_set )( aout_instance_t *, audio_volume_t );
176     int                  (* pf_volume_infos )( aout_instance_t *, audio_volume_t * );
177     int                     i_nb_samples;
178
179     /* Current volume for the output - it's just a placeholder, the plug-in
180      * may or may not use it. */
181     audio_volume_t          i_volume;
182 } aout_output_t;
183
184 /*****************************************************************************
185  * aout_instance_t : audio output thread descriptor
186  *****************************************************************************/
187 struct aout_instance_t
188 {
189     VLC_COMMON_MEMBERS
190
191     /* Locks : please note that if you need several of these locks, it is
192      * mandatory (to avoid deadlocks) to take them in the following order :
193      * mixer_lock, p_input->lock, output_fifo_lock, input_fifos_lock.
194      * --Meuuh */
195     /* When input_fifos_lock is taken, none of the p_input->fifo structures
196      * can be read or modified by a third-party thread. */
197     vlc_mutex_t             input_fifos_lock;
198     /* When mixer_lock is taken, all decoder threads willing to mix a
199      * buffer must wait until it is released. The output pipeline cannot
200      * be modified. No input stream can be added or removed. */
201     vlc_mutex_t             mixer_lock;
202     /* When output_fifo_lock is taken, the p_aout->output.fifo structure
203      * cannot be read or written  by a third-party thread. */
204     vlc_mutex_t             output_fifo_lock;
205
206     /* Input streams & pre-filters */
207     aout_input_t *          pp_inputs[AOUT_MAX_INPUTS];
208     int                     i_nb_inputs;
209
210     /* Mixer */
211     aout_mixer_t            mixer;
212
213     /* Output plug-in */
214     aout_output_t           output;
215 };
216
217 /*****************************************************************************
218  * Prototypes
219  *****************************************************************************/
220 /* From input.c : */
221 int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input );
222 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input );
223 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
224                     aout_buffer_t * p_buffer );
225
226 /* From filters.c : */
227 int aout_FiltersCreatePipeline( aout_instance_t * p_aout,
228                                 aout_filter_t ** pp_filters,
229                                 int * pi_nb_filters,
230                                 const audio_sample_format_t * p_input_format,
231                                 const audio_sample_format_t * p_output_format );
232 void aout_FiltersDestroyPipeline( aout_instance_t * p_aout,
233                                   aout_filter_t ** pp_filters,
234                                   int i_nb_filters );
235 void aout_FiltersHintBuffers( aout_instance_t * p_aout,
236                               aout_filter_t ** pp_filters,
237                               int i_nb_filters, aout_alloc_t * p_first_alloc );
238 void aout_FiltersPlay( aout_instance_t * p_aout,
239                        aout_filter_t ** pp_filters,
240                        int i_nb_filters, aout_buffer_t ** pp_input_buffer );
241
242 /* From mixer.c : */
243 int aout_MixerNew( aout_instance_t * p_aout );
244 int aout_MixerDelete( aout_instance_t * p_aout );
245 void aout_MixerRun( aout_instance_t * p_aout );
246 int aout_MixerMultiplierSet( aout_instance_t * p_aout, float f_multiplier );
247 int aout_MixerMultiplierGet( aout_instance_t * p_aout, float * pf_multiplier );
248
249 /* From output.c : */
250 int aout_OutputNew( aout_instance_t * p_aout,
251                     audio_sample_format_t * p_format );
252 void aout_OutputPlay( aout_instance_t * p_aout, aout_buffer_t * p_buffer );
253 void aout_OutputDelete( aout_instance_t * p_aout );
254 VLC_EXPORT( aout_buffer_t *, aout_OutputNextBuffer, ( aout_instance_t *, mtime_t, vlc_bool_t ) );
255
256 /* From common.c : */
257 VLC_EXPORT( int, aout_FormatNbChannels, ( audio_sample_format_t * p_format ) );
258 void aout_FormatPrepare( audio_sample_format_t * p_format );
259 void aout_FifoInit( aout_instance_t *, aout_fifo_t *, u32 );
260 mtime_t aout_FifoNextStart( aout_instance_t *, aout_fifo_t * );
261 void aout_FifoPush( aout_instance_t *, aout_fifo_t *, aout_buffer_t * );
262 void aout_FifoSet( aout_instance_t *, aout_fifo_t *, mtime_t );
263 void aout_FifoMoveDates( aout_instance_t *, aout_fifo_t *, mtime_t );
264 VLC_EXPORT( aout_buffer_t *, aout_FifoPop, ( aout_instance_t * p_aout, aout_fifo_t * p_fifo ) );
265 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo );
266
267 /* From intf.c :*/
268 VLC_EXPORT( void, aout_VolumeSoftInit, ( aout_instance_t * ) );
269 int aout_VolumeSoftGet( aout_instance_t *, audio_volume_t * );
270 int aout_VolumeSoftSet( aout_instance_t *, audio_volume_t );
271 int aout_VolumeSoftInfos( aout_instance_t *, audio_volume_t * );
272 VLC_EXPORT( void, aout_VolumeNoneInit, ( aout_instance_t * ) );
273 int aout_VolumeNoneGet( aout_instance_t *, audio_volume_t * );
274 int aout_VolumeNoneSet( aout_instance_t *, audio_volume_t );
275 int aout_VolumeNoneInfos( aout_instance_t *, audio_volume_t * );
276