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