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