]> git.sesse.net Git - vlc/blob - include/vlc_aout.h
7139d4328efe9e88b8da3479d00599ba4319e78f
[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       && AOUT_FMTS_SIMILAR(p_first, p_second) )
41
42 /* Check if i_rate == i_rate and i_channels == i_channels */
43 #define AOUT_FMTS_SIMILAR( p_first, p_second ) (                            \
44     ((p_first)->i_rate == (p_second)->i_rate)                               \
45       && ((p_first)->i_physical_channels == (p_second)->i_physical_channels)\
46       && ((p_first)->i_original_channels == (p_second)->i_original_channels) )
47
48 #define VLC_CODEC_SPDIFL VLC_FOURCC('s','p','d','i')
49 #define VLC_CODEC_SPDIFB VLC_FOURCC('s','p','d','b')
50
51 #define AOUT_FMT_NON_LINEAR( p_format )                 \
52     ( ((p_format)->i_format == VLC_CODEC_SPDIFL)       \
53        || ((p_format)->i_format == VLC_CODEC_SPDIFB)   \
54        || ((p_format)->i_format == VLC_CODEC_A52)       \
55        || ((p_format)->i_format == VLC_CODEC_DTS) )
56
57 /* This is heavily borrowed from libmad, by Robert Leslie <rob@mars.org> */
58 /*
59  * Fixed-point format: 0xABBBBBBB
60  * A == whole part      (sign + 3 bits)
61  * B == fractional part (28 bits)
62  *
63  * Values are signed two's complement, so the effective range is:
64  * 0x80000000 to 0x7fffffff
65  *       -8.0 to +7.9999999962747097015380859375
66  *
67  * The smallest representable value is:
68  * 0x00000001 == 0.0000000037252902984619140625 (i.e. about 3.725e-9)
69  *
70  * 28 bits of fractional accuracy represent about
71  * 8.6 digits of decimal accuracy.
72  *
73  * Fixed-point numbers can be added or subtracted as normal
74  * integers, but multiplication requires shifting the 64-bit result
75  * from 56 fractional bits back to 28 (and rounding.)
76  */
77 typedef int32_t vlc_fixed_t;
78 #define FIXED32_FRACBITS 28
79 #define FIXED32_MIN ((vlc_fixed_t) -0x80000000L)
80 #define FIXED32_MAX ((vlc_fixed_t) +0x7fffffffL)
81 #define FIXED32_ONE ((vlc_fixed_t) 0x10000000)
82
83 /*
84  * Channels descriptions
85  */
86
87 /* Values available for physical and original channels */
88 #define AOUT_CHAN_CENTER            0x1
89 #define AOUT_CHAN_LEFT              0x2
90 #define AOUT_CHAN_RIGHT             0x4
91 #define AOUT_CHAN_REARCENTER        0x10
92 #define AOUT_CHAN_REARLEFT          0x20
93 #define AOUT_CHAN_REARRIGHT         0x40
94 #define AOUT_CHAN_MIDDLELEFT        0x100
95 #define AOUT_CHAN_MIDDLERIGHT       0x200
96 #define AOUT_CHAN_LFE               0x1000
97
98 /* Values available for original channels only */
99 #define AOUT_CHAN_DOLBYSTEREO       0x10000
100 #define AOUT_CHAN_DUALMONO          0x20000
101 #define AOUT_CHAN_REVERSESTEREO     0x40000
102
103 #define AOUT_CHAN_PHYSMASK          0xFFFF
104 #define AOUT_CHAN_MAX               9
105
106 /* Values used for the audio-device and audio-channels object variables */
107 #define AOUT_VAR_MONO               1
108 #define AOUT_VAR_STEREO             2
109 #define AOUT_VAR_2F2R               4
110 #define AOUT_VAR_3F2R               5
111 #define AOUT_VAR_5_1                6
112 #define AOUT_VAR_6_1                7
113 #define AOUT_VAR_7_1                8
114 #define AOUT_VAR_SPDIF              10
115
116 #define AOUT_VAR_CHAN_STEREO        1
117 #define AOUT_VAR_CHAN_RSTEREO       2
118 #define AOUT_VAR_CHAN_LEFT          3
119 #define AOUT_VAR_CHAN_RIGHT         4
120 #define AOUT_VAR_CHAN_DOLBYS        5
121
122 /*****************************************************************************
123  * Main audio output structures
124  *****************************************************************************/
125
126 #define aout_BufferFree( buffer ) block_Release( buffer )
127
128 /* Size of a frame for S/PDIF output. */
129 #define AOUT_SPDIF_SIZE 6144
130
131 /* Number of samples in an A/52 frame. */
132 #define A52_FRAME_NB 1536
133
134 /* Max input rate factor (1/4 -> 4) */
135 #define AOUT_MAX_INPUT_RATE (4)
136
137 /** allocation of memory in the audio output */
138 typedef struct aout_alloc_t
139 {
140     bool                    b_alloc;
141     int                     i_bytes_per_sec;
142 } aout_alloc_t;
143
144 /** audio output buffer FIFO */
145 struct aout_fifo_t
146 {
147     aout_buffer_t *         p_first;
148     aout_buffer_t **        pp_last;
149     date_t                  end_date;
150 };
151
152 /* FIXME to remove once aout.h is cleaned a bit more */
153 #include <vlc_aout_mixer.h>
154 #include <vlc_block.h>
155
156 /** audio output filter */
157 typedef struct aout_filter_owner_sys_t aout_filter_owner_sys_t;
158 typedef struct aout_filter_sys_t aout_filter_sys_t;
159 struct aout_filter_t
160 {
161     VLC_COMMON_MEMBERS
162
163     module_t *              p_module;
164     aout_filter_sys_t       *p_sys;
165
166     es_format_t             fmt_in;
167     es_format_t             fmt_out;
168
169     aout_alloc_t            output_alloc;
170
171     bool                    b_in_place;
172
173     void                    (*pf_do_work)( aout_instance_t *, aout_filter_t *,
174                                            aout_buffer_t *, aout_buffer_t * );
175
176     /* Private structure for the owner of the filter */
177     aout_filter_owner_sys_t *p_owner;
178 };
179
180 #define AOUT_RESAMPLING_NONE     0
181 #define AOUT_RESAMPLING_UP       1
182 #define AOUT_RESAMPLING_DOWN     2
183
184 /** an output stream for the audio output */
185 typedef struct aout_output_t
186 {
187     audio_sample_format_t   output;
188     /* Indicates whether the audio output is currently starving, to avoid
189      * printing a 1,000 "output is starving" messages. */
190     bool              b_starving;
191
192     /* post-filters */
193     filter_t *              pp_filters[AOUT_MAX_FILTERS];
194     int                     i_nb_filters;
195
196     aout_fifo_t             fifo;
197
198     struct module_t *       p_module;
199     struct aout_sys_t *     p_sys;
200     void                 (* pf_play)( aout_instance_t * );
201     int                  (* pf_volume_set )( aout_instance_t *, audio_volume_t, bool );
202     int                     i_nb_samples;
203
204     /* If b_error == 1, there is no audio output pipeline. */
205     bool              b_error;
206 } aout_output_t;
207
208 /** audio output thread descriptor */
209 struct aout_instance_t
210 {
211     VLC_COMMON_MEMBERS
212
213     /* Locks : please note that if you need several of these locks, it is
214      * mandatory (to avoid deadlocks) to take them in the following order :
215      * mixer_lock, p_input->lock, output_fifo_lock, input_fifos_lock.
216      * --Meuuh */
217     /* When input_fifos_lock is taken, none of the p_input->fifo structures
218      * can be read or modified by a third-party thread. */
219     vlc_mutex_t             input_fifos_lock;
220     /* When mixer_lock is taken, all decoder threads willing to mix a
221      * buffer must wait until it is released. The output pipeline cannot
222      * be modified. No input stream can be added or removed. */
223     vlc_mutex_t             mixer_lock;
224     /* When output_fifo_lock is taken, the p_aout->output.fifo structure
225      * cannot be read or written  by a third-party thread. */
226     vlc_mutex_t             output_fifo_lock;
227     /* volume_vars_lock is taken */
228     vlc_mutex_t             volume_vars_lock;
229
230     /* Input streams & pre-filters */
231     aout_input_t *          pp_inputs[1];
232     int                     i_nb_inputs;
233
234     /* Mixer */
235     audio_sample_format_t   mixer_format;
236     aout_alloc_t            mixer_allocation;
237     float                   mixer_multiplier;
238     aout_mixer_t            *p_mixer;
239
240     /* Output plug-in */
241     aout_output_t           output;
242 };
243
244 /**
245  * It describes the audio channel order VLC expect.
246  */
247 static const uint32_t pi_vlc_chan_order_wg4[] =
248 {
249     AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
250     AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
251     AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_REARCENTER,
252     AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0
253 };
254
255 /*****************************************************************************
256  * Prototypes
257  *****************************************************************************/
258
259 VLC_API aout_buffer_t * aout_OutputNextBuffer( aout_instance_t *, mtime_t, bool ) VLC_USED;
260
261 /**
262  * This function computes the reordering needed to go from pi_chan_order_in to
263  * pi_chan_order_out.
264  * If pi_chan_order_in or pi_chan_order_out is NULL, it will assume that vlc
265  * internal (WG4) order is requested.
266  */
267 VLC_API 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 );
268 VLC_API void aout_ChannelReorder( uint8_t *, int, int, const int *, int );
269
270 /**
271  * This fonction will compute the extraction parameter into pi_selection to go
272  * from i_channels with their type given by pi_order_src[] into the order
273  * describe by pi_order_dst.
274  * It will also set :
275  * - *pi_channels as the number of channels that will be extracted which is
276  * lower (in case of non understood channels type) or equal to i_channels.
277  * - the layout of the channels (*pi_layout).
278  *
279  * It will return true if channel extraction is really needed, in which case
280  * aout_ChannelExtract must be used
281  *
282  * XXX It must be used when the source may have channel type not understood
283  * by VLC. In this case the channel type pi_order_src[] must be set to 0.
284  * XXX It must also be used if multiple channels have the same type.
285  */
286 VLC_API 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 );
287
288 /**
289  * Do the actual channels extraction using the parameters created by
290  * aout_CheckChannelExtraction.
291  *
292  * XXX this function does not work in place (p_dst and p_src must not overlap).
293  * XXX Only 8, 16, 24, 32, 64 bits per sample are supported.
294  */
295 VLC_API 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 );
296
297 /* */
298 static inline unsigned aout_FormatNbChannels(const audio_sample_format_t *fmt)
299 {
300     return popcount(fmt->i_physical_channels & AOUT_CHAN_PHYSMASK);
301 }
302
303 VLC_API unsigned int aout_BitsPerSample( vlc_fourcc_t i_format ) VLC_USED;
304 VLC_API void aout_FormatPrepare( audio_sample_format_t * p_format );
305 VLC_API void aout_FormatPrint( aout_instance_t * p_aout, const char * psz_text, const audio_sample_format_t * p_format );
306 VLC_API const char * aout_FormatPrintChannels( const audio_sample_format_t * ) VLC_USED;
307
308 VLC_API mtime_t aout_FifoFirstDate( aout_instance_t *, aout_fifo_t * ) VLC_USED;
309 VLC_API aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo ) VLC_USED;
310
311 /* From intf.c : */
312 VLC_API void aout_VolumeSoftInit( aout_instance_t * );
313 VLC_API void aout_VolumeNoneInit( aout_instance_t * );
314 VLC_API audio_volume_t aout_VolumeGet( vlc_object_t * );
315 #define aout_VolumeGet(a) aout_VolumeGet(VLC_OBJECT(a))
316 VLC_API int aout_VolumeSet( vlc_object_t *, audio_volume_t );
317 #define aout_VolumeSet(a, b) aout_VolumeSet(VLC_OBJECT(a), b)
318 VLC_API int aout_VolumeUp( vlc_object_t *, int, audio_volume_t * );
319 #define aout_VolumeUp(a, b, c) aout_VolumeUp(VLC_OBJECT(a), b, c)
320 VLC_API int aout_VolumeDown( vlc_object_t *, int, audio_volume_t * );
321 #define aout_VolumeDown(a, b, c) aout_VolumeDown(VLC_OBJECT(a), b, c)
322 VLC_API int aout_ToggleMute( vlc_object_t *, audio_volume_t * );
323 #define aout_ToggleMute(a, b) aout_ToggleMute(VLC_OBJECT(a), b)
324 VLC_API int aout_SetMute( vlc_object_t *, audio_volume_t *, bool );
325 VLC_API bool aout_IsMuted( vlc_object_t * );
326 VLC_API int aout_ChannelsRestart( vlc_object_t *, const char *, vlc_value_t, vlc_value_t, void * );
327
328 VLC_API void aout_EnableFilter(vlc_object_t *, const char *, bool );
329 #define aout_EnableFilter( o, n, b ) \
330         aout_EnableFilter( VLC_OBJECT(o), n, b )
331
332 /* */
333 VLC_API vout_thread_t * aout_filter_RequestVout( filter_t *, vout_thread_t *p_vout, video_format_t *p_fmt ) VLC_USED;
334
335 # ifdef __cplusplus
336 }
337 # endif
338
339 #endif /* _VLC_AOUT_H */