]> git.sesse.net Git - vlc/blob - include/vlc_aout.h
aout_VolumeMute -> aout_ToggleMute
[vlc] / include / vlc_aout.h
1 /*****************************************************************************
2  * audio_output.h : audio output interface
3  *****************************************************************************
4  * Copyright (C) 2002-2005 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 #ifndef VLC_AOUT_H
25 #define VLC_AOUT_H 1
26
27 /**
28  * \file
29  * This file defines functions, structures and macros for audio output object
30  */
31
32 # ifdef __cplusplus
33 extern "C" {
34 # endif
35
36 #include "vlc_es.h"
37
38 #define AOUT_FMTS_IDENTICAL( p_first, p_second ) (                          \
39     ((p_first)->i_format == (p_second)->i_format)                           \
40       && ((p_first)->i_rate == (p_second)->i_rate)                          \
41       && ((p_first)->i_physical_channels == (p_second)->i_physical_channels)\
42       && ((p_first)->i_original_channels == (p_second)->i_original_channels) )
43
44 /* Check if i_rate == i_rate and i_channels == i_channels */
45 #define AOUT_FMTS_SIMILAR( p_first, p_second ) (                            \
46     ((p_first)->i_rate == (p_second)->i_rate)                               \
47       && ((p_first)->i_physical_channels == (p_second)->i_physical_channels)\
48       && ((p_first)->i_original_channels == (p_second)->i_original_channels) )
49
50 #define VLC_CODEC_SPDIFL VLC_FOURCC('s','p','d','i')
51 #define VLC_CODEC_SPDIFB VLC_FOURCC('s','p','d','b')
52
53 #define AOUT_FMT_NON_LINEAR( p_format )                 \
54     ( ((p_format)->i_format == VLC_CODEC_SPDIFL)       \
55        || ((p_format)->i_format == VLC_CODEC_SPDIFB)   \
56        || ((p_format)->i_format == VLC_CODEC_A52)       \
57        || ((p_format)->i_format == VLC_CODEC_DTS) )
58
59 /* This is heavily borrowed from libmad, by Robert Leslie <rob@mars.org> */
60 /*
61  * Fixed-point format: 0xABBBBBBB
62  * A == whole part      (sign + 3 bits)
63  * B == fractional part (28 bits)
64  *
65  * Values are signed two's complement, so the effective range is:
66  * 0x80000000 to 0x7fffffff
67  *       -8.0 to +7.9999999962747097015380859375
68  *
69  * The smallest representable value is:
70  * 0x00000001 == 0.0000000037252902984619140625 (i.e. about 3.725e-9)
71  *
72  * 28 bits of fractional accuracy represent about
73  * 8.6 digits of decimal accuracy.
74  *
75  * Fixed-point numbers can be added or subtracted as normal
76  * integers, but multiplication requires shifting the 64-bit result
77  * from 56 fractional bits back to 28 (and rounding.)
78  */
79 typedef int32_t vlc_fixed_t;
80 #define FIXED32_FRACBITS 28
81 #define FIXED32_MIN ((vlc_fixed_t) -0x80000000L)
82 #define FIXED32_MAX ((vlc_fixed_t) +0x7fffffffL)
83 #define FIXED32_ONE ((vlc_fixed_t) 0x10000000)
84
85 /*
86  * Channels descriptions
87  */
88
89 /* Values available for physical and original channels */
90 #define AOUT_CHAN_CENTER            0x1
91 #define AOUT_CHAN_LEFT              0x2
92 #define AOUT_CHAN_RIGHT             0x4
93 #define AOUT_CHAN_REARCENTER        0x10
94 #define AOUT_CHAN_REARLEFT          0x20
95 #define AOUT_CHAN_REARRIGHT         0x40
96 #define AOUT_CHAN_MIDDLELEFT        0x100
97 #define AOUT_CHAN_MIDDLERIGHT       0x200
98 #define AOUT_CHAN_LFE               0x1000
99
100 /* Values available for original channels only */
101 #define AOUT_CHAN_DOLBYSTEREO       0x10000
102 #define AOUT_CHAN_DUALMONO          0x20000
103 #define AOUT_CHAN_REVERSESTEREO     0x40000
104
105 #define AOUT_CHAN_PHYSMASK          0xFFFF
106 #define AOUT_CHAN_MAX               9
107
108 /* Values used for the audio-device and audio-channels object variables */
109 #define AOUT_VAR_MONO               1
110 #define AOUT_VAR_STEREO             2
111 #define AOUT_VAR_2F2R               4
112 #define AOUT_VAR_3F2R               5
113 #define AOUT_VAR_5_1                6
114 #define AOUT_VAR_6_1                7
115 #define AOUT_VAR_7_1                8
116 #define AOUT_VAR_SPDIF              10
117
118 #define AOUT_VAR_CHAN_STEREO        1
119 #define AOUT_VAR_CHAN_RSTEREO       2
120 #define AOUT_VAR_CHAN_LEFT          3
121 #define AOUT_VAR_CHAN_RIGHT         4
122 #define AOUT_VAR_CHAN_DOLBYS        5
123
124 /*****************************************************************************
125  * Main audio output structures
126  *****************************************************************************/
127
128 /** audio output buffer */
129 struct aout_buffer_t
130 {
131     uint8_t *               p_buffer;
132     int                     i_alloc_type;
133     /* i_size is the real size of the buffer (used for debug ONLY), i_nb_bytes
134      * is the number of significative bytes in it. */
135     size_t                  i_size, i_nb_bytes;
136     unsigned int            i_nb_samples;
137     mtime_t                 start_date, end_date;
138     bool                    b_discontinuity; /* Set on discontinuity (for non pcm stream) */
139
140     struct aout_buffer_t *  p_next;
141
142     /** Private data (aout_buffer_t will disappear soon so no need for an
143      * aout_buffer_sys_t type) */
144     void * p_sys;
145
146     /** This way the release can be overloaded */
147     void (*pf_release)( aout_buffer_t * );
148 };
149
150 #define aout_BufferFree( p_buffer ) do {                                    \
151     if( p_buffer != NULL && (p_buffer)->i_alloc_type == AOUT_ALLOC_HEAP )   \
152     {                                                                       \
153         free( p_buffer );                                                   \
154     }                                                                       \
155     p_buffer = NULL; } while(0)
156
157 /* Size of a frame for S/PDIF output. */
158 #define AOUT_SPDIF_SIZE 6144
159
160 /* Number of samples in an A/52 frame. */
161 #define A52_FRAME_NB 1536
162
163 /* Max input rate factor (1/4 -> 4) */
164 #define AOUT_MAX_INPUT_RATE (4)
165
166 /** date incrementation helper structure without long-term
167  * rounding errors
168  */
169 struct audio_date_t
170 {
171     mtime_t  date;
172     uint32_t i_divider;
173     uint32_t i_remainder;
174 };
175
176 /** allocation of memory in the audio output */
177 typedef struct aout_alloc_t
178 {
179     int                     i_alloc_type;
180     int                     i_bytes_per_sec;
181 } aout_alloc_t;
182
183 #define AOUT_ALLOC_NONE     0
184 #define AOUT_ALLOC_STACK    1
185 #define AOUT_ALLOC_HEAP     2
186
187 /** audio output mixer */
188 typedef struct aout_mixer_t
189 {
190     audio_sample_format_t   mixer;
191     aout_alloc_t            output_alloc;
192
193     module_t *              p_module;
194     struct aout_mixer_sys_t * p_sys;
195     void                 (* pf_do_work)( struct aout_instance_t *,
196                                          struct aout_buffer_t * );
197
198     /** If b_error == 1, there is no mixer. */
199     bool              b_error;
200     /** Multiplier used to raise or lower the volume of the sound in
201      * software. Beware, this creates sound distortion and should be avoided
202      * as much as possible. This isn't available for non-float32 mixer. */
203     float                   f_multiplier;
204 } aout_mixer_t;
205
206 /** audio output buffer FIFO */
207 struct aout_fifo_t
208 {
209     aout_buffer_t *         p_first;
210     aout_buffer_t **        pp_last;
211     audio_date_t            end_date;
212 };
213
214 /* */
215 typedef struct
216 {
217     vout_thread_t  *(*pf_request_vout)( void *,
218                                         vout_thread_t *, video_format_t *, bool b_recycle );
219     void *p_private;
220 } aout_request_vout_t;
221
222 /** audio output filter */
223 typedef struct aout_filter_owner_sys_t aout_filter_owner_sys_t;
224 typedef struct aout_filter_sys_t aout_filter_sys_t;
225 struct aout_filter_t
226 {
227     VLC_COMMON_MEMBERS
228
229     audio_sample_format_t   input;
230     audio_sample_format_t   output;
231     aout_alloc_t            output_alloc;
232
233     module_t *              p_module;
234     aout_filter_sys_t       *p_sys;
235
236     bool                    b_in_place;
237     bool                    b_continuity;
238
239     void                    (*pf_do_work)( aout_instance_t *, aout_filter_t *,
240                                            aout_buffer_t *, aout_buffer_t * );
241
242     /* Owner fieldS
243      * XXX You MUST not use them directly */
244
245     /* Vout callback
246      * XXX use aout_filter_RequestVout */
247     aout_request_vout_t request_vout;
248
249     /* Private structure for the owner of the filter */
250     aout_filter_owner_sys_t *p_owner;
251 };
252
253 #define AOUT_RESAMPLING_NONE     0
254 #define AOUT_RESAMPLING_UP       1
255 #define AOUT_RESAMPLING_DOWN     2
256 /** an input stream for the audio output */
257 struct aout_input_t
258 {
259     /* When this lock is taken, the pipeline cannot be changed by a
260      * third-party. */
261     vlc_mutex_t             lock;
262
263     audio_sample_format_t   input;
264     aout_alloc_t            input_alloc;
265
266     /* pre-filters */
267     aout_filter_t *         pp_filters[AOUT_MAX_FILTERS];
268     int                     i_nb_filters;
269
270     aout_filter_t *         p_playback_rate_filter;
271
272     /* resamplers */
273     aout_filter_t *         pp_resamplers[AOUT_MAX_FILTERS];
274     int                     i_nb_resamplers;
275     int                     i_resampling_type;
276     mtime_t                 i_resamp_start_date;
277     int                     i_resamp_start_drift;
278
279     aout_fifo_t             fifo;
280
281     /* Mixer information */
282     uint8_t *               p_first_byte_to_mix;
283     audio_replay_gain_t     replay_gain;
284     float                   f_multiplier;
285
286     /* If b_restart == 1, the input pipeline will be re-created. */
287     bool              b_restart;
288
289     /* If b_error == 1, there is no input pipeline. */
290     bool              b_error;
291
292     /* Did we just change the output format? (expect buffer inconsistencies) */
293     bool              b_changed;
294
295     /* last rate from input */
296     int               i_last_input_rate;
297
298     /* */
299     int               i_buffer_lost;
300
301     /* */
302     bool              b_paused;
303     mtime_t           i_pause_date;
304
305     /* */
306     bool                b_recycle_vout;
307     aout_request_vout_t request_vout;
308  };
309
310 /** an output stream for the audio output */
311 typedef struct aout_output_t
312 {
313     audio_sample_format_t   output;
314     /* Indicates whether the audio output is currently starving, to avoid
315      * printing a 1,000 "output is starving" messages. */
316     bool              b_starving;
317
318     /* post-filters */
319     aout_filter_t *         pp_filters[AOUT_MAX_FILTERS];
320     int                     i_nb_filters;
321
322     aout_fifo_t             fifo;
323
324     struct module_t *       p_module;
325     struct aout_sys_t *     p_sys;
326     void                 (* pf_play)( aout_instance_t * );
327     int                  (* pf_volume_get )( aout_instance_t *, audio_volume_t * );
328     int                  (* pf_volume_set )( aout_instance_t *, audio_volume_t );
329     int                  (* pf_volume_infos )( aout_instance_t *, audio_volume_t * );
330     int                     i_nb_samples;
331
332     /* Current volume for the output - it's just a placeholder, the plug-in
333      * may or may not use it. */
334     audio_volume_t          i_volume;
335
336     /* If b_error == 1, there is no audio output pipeline. */
337     bool              b_error;
338 } aout_output_t;
339
340 /** audio output thread descriptor */
341 struct aout_instance_t
342 {
343     VLC_COMMON_MEMBERS
344
345     /* Locks : please note that if you need several of these locks, it is
346      * mandatory (to avoid deadlocks) to take them in the following order :
347      * mixer_lock, p_input->lock, output_fifo_lock, input_fifos_lock.
348      * --Meuuh */
349     /* When input_fifos_lock is taken, none of the p_input->fifo structures
350      * can be read or modified by a third-party thread. */
351     vlc_mutex_t             input_fifos_lock;
352     /* When mixer_lock is taken, all decoder threads willing to mix a
353      * buffer must wait until it is released. The output pipeline cannot
354      * be modified. No input stream can be added or removed. */
355     vlc_mutex_t             mixer_lock;
356     /* When output_fifo_lock is taken, the p_aout->output.fifo structure
357      * cannot be read or written  by a third-party thread. */
358     vlc_mutex_t             output_fifo_lock;
359
360     /* Input streams & pre-filters */
361     aout_input_t *          pp_inputs[AOUT_MAX_INPUTS];
362     int                     i_nb_inputs;
363
364     /* Mixer */
365     aout_mixer_t            mixer;
366
367     /* Output plug-in */
368     aout_output_t           output;
369 };
370
371 /**
372  * It describes the audio channel order VLC except.
373  */
374 static const uint32_t pi_vlc_chan_order_wg4[] =
375 {
376     AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
377     AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
378     AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_REARCENTER,
379     AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0
380 };
381
382 /*****************************************************************************
383  * Prototypes
384  *****************************************************************************/
385
386 /* From common.c : */
387 VLC_EXPORT( void, aout_DateInit, ( audio_date_t *, uint32_t ) );
388 VLC_EXPORT( void, aout_DateSet, ( audio_date_t *, mtime_t ) );
389 VLC_EXPORT( void, aout_DateMove, ( audio_date_t *, mtime_t ) );
390 VLC_EXPORT( mtime_t, aout_DateGet, ( const audio_date_t * ) LIBVLC_USED);
391 VLC_EXPORT( mtime_t, aout_DateIncrement, ( audio_date_t *, uint32_t ) );
392
393 VLC_EXPORT( aout_buffer_t *, aout_OutputNextBuffer, ( aout_instance_t *, mtime_t, bool ) LIBVLC_USED );
394
395 /**
396  * This function computes the reordering needed to go from pi_chan_order_in to
397  * pi_chan_order_out.
398  * If pi_chan_order_in or pi_chan_order_out is NULL, it will assume that vlc
399  * internal (WG4) order is requested.
400  */
401 VLC_EXPORT( int, aout_CheckChannelReorder, ( const uint32_t *pi_chan_order_in, const uint32_t *pi_chan_order_out, uint32_t i_channel_mask, int i_channels, int *pi_chan_table ) );
402 VLC_EXPORT( void, aout_ChannelReorder, ( uint8_t *, int, int, const int *, int ) );
403
404 /**
405  * This fonction will compute the extraction parameter into pi_selection to go
406  * from i_channels with their type given by pi_order_src[] into the order
407  * describe by pi_order_dst.
408  * It will also set :
409  * - *pi_channels as the number of channels that will be extracted which is
410  * lower (in case of non understood channels type) or equal to i_channels.
411  * - the layout of the channels (*pi_layout).
412  *
413  * It will return true if channel extraction is really needed, in which case
414  * aout_ChannelExtract must be used
415  *
416  * XXX It must be used when the source may have channel type not understood
417  * by VLC. In this case the channel type pi_order_src[] must be set to 0.
418  * XXX It must also be used if multiple channels have the same type.
419  */
420 VLC_EXPORT( bool, aout_CheckChannelExtraction, ( int *pi_selection, uint32_t *pi_layout, int *pi_channels, const uint32_t pi_order_dst[AOUT_CHAN_MAX], const uint32_t *pi_order_src, int i_channels ) );
421
422 /**
423  * Do the actual channels extraction using the parameters created by
424  * aout_CheckChannelExtraction.
425  *
426  * XXX this function does not work in place (p_dst and p_src must not overlap).
427  * XXX Only 8, 16, 24, 32, 64 bits per sample are supported.
428  */
429 VLC_EXPORT( void, aout_ChannelExtract, ( void *p_dst, int i_dst_channels, const void *p_src, int i_src_channels, int i_sample_count, const int *pi_selection, int i_bits_per_sample ) );
430
431 /* */
432 VLC_EXPORT( unsigned int, aout_FormatNbChannels, ( const audio_sample_format_t * p_format ) LIBVLC_USED );
433 VLC_EXPORT( unsigned int, aout_BitsPerSample, ( vlc_fourcc_t i_format ) LIBVLC_USED );
434 VLC_EXPORT( void, aout_FormatPrepare, ( audio_sample_format_t * p_format ) );
435 VLC_EXPORT( void, aout_FormatPrint, ( aout_instance_t * p_aout, const char * psz_text, const audio_sample_format_t * p_format ) );
436 VLC_EXPORT( const char *, aout_FormatPrintChannels, ( const audio_sample_format_t * ) LIBVLC_USED );
437
438 VLC_EXPORT( mtime_t, aout_FifoFirstDate, ( aout_instance_t *, aout_fifo_t * ) LIBVLC_USED );
439 VLC_EXPORT( aout_buffer_t *, aout_FifoPop, ( aout_instance_t * p_aout, aout_fifo_t * p_fifo ) LIBVLC_USED );
440
441 /* From intf.c : */
442 VLC_EXPORT( void, aout_VolumeSoftInit, ( aout_instance_t * ) );
443 VLC_EXPORT( void, aout_VolumeNoneInit, ( aout_instance_t * ) );
444 #define aout_VolumeGet(a, b) __aout_VolumeGet(VLC_OBJECT(a), b)
445 VLC_EXPORT( int, __aout_VolumeGet, ( vlc_object_t *, audio_volume_t * ) );
446 #define aout_VolumeSet(a, b) __aout_VolumeSet(VLC_OBJECT(a), b)
447 VLC_EXPORT( int, __aout_VolumeSet, ( vlc_object_t *, audio_volume_t ) );
448 #define aout_VolumeInfos(a, b) __aout_VolumeInfos(VLC_OBJECT(a), b)
449 VLC_EXPORT( int, __aout_VolumeInfos, ( vlc_object_t *, audio_volume_t * ) );
450 #define aout_VolumeUp(a, b, c) __aout_VolumeUp(VLC_OBJECT(a), b, c)
451 VLC_EXPORT( int, __aout_VolumeUp, ( vlc_object_t *, int, audio_volume_t * ) );
452 #define aout_VolumeDown(a, b, c) __aout_VolumeDown(VLC_OBJECT(a), b, c)
453 VLC_EXPORT( int, __aout_VolumeDown, ( vlc_object_t *, int, audio_volume_t * ) );
454 #define aout_ToggleMute(a, b) __aout_ToggleMute(VLC_OBJECT(a), b)
455 VLC_EXPORT( int, __aout_ToggleMute, ( vlc_object_t *, audio_volume_t * ) );
456 VLC_EXPORT( int, aout_FindAndRestart, ( vlc_object_t *, const char *, vlc_value_t, vlc_value_t, void * ) );
457 VLC_EXPORT( int, aout_ChannelsRestart, ( vlc_object_t *, const char *, vlc_value_t, vlc_value_t, void * ) );
458
459 VLC_EXPORT( void, aout_EnableFilter, (vlc_object_t *, const char *, bool ));
460
461 #define aout_VisualNext(a) aout_VisualChange( VLC_OBJECT(a),1 )
462 #define aout_VisualPrev(a) aout_VisualChange( VLC_OBJECT(a),-1 )
463
464 VLC_EXPORT( char *, aout_VisualChange, (vlc_object_t *, int ) );
465
466 /* */
467 VLC_EXPORT( vout_thread_t *, aout_filter_RequestVout, ( aout_filter_t *, vout_thread_t *p_vout, video_format_t *p_fmt ) );
468
469 # ifdef __cplusplus
470 }
471 # endif
472
473 #endif /* _VLC_AOUT_H */