]> git.sesse.net Git - vlc/blob - include/vlc_aout.h
Rework aout_buffer_t layout
[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     /* i_size is the real size of the buffer (used for debug ONLY), i_nb_bytes
133      * is the number of significative bytes in it. */
134     size_t                  i_size, i_nb_bytes;
135     unsigned int            i_nb_samples;
136     bool                    b_alloc;
137     bool                    b_discontinuity; /* Set on discontinuity (for non pcm stream) */
138     mtime_t                 start_date, end_date;
139
140     struct aout_buffer_t *  p_next;
141 };
142
143 static inline void aout_BufferFree( aout_buffer_t *buffer )
144 {
145     if( buffer && buffer->b_alloc )
146         free( buffer );
147 }
148
149 /* Size of a frame for S/PDIF output. */
150 #define AOUT_SPDIF_SIZE 6144
151
152 /* Number of samples in an A/52 frame. */
153 #define A52_FRAME_NB 1536
154
155 /* Max input rate factor (1/4 -> 4) */
156 #define AOUT_MAX_INPUT_RATE (4)
157
158 /** allocation of memory in the audio output */
159 typedef struct aout_alloc_t
160 {
161     bool                    b_alloc;
162     int                     i_bytes_per_sec;
163 } aout_alloc_t;
164
165 /** audio output buffer FIFO */
166 struct aout_fifo_t
167 {
168     aout_buffer_t *         p_first;
169     aout_buffer_t **        pp_last;
170     date_t                  end_date;
171 };
172
173 /* FIXME to remove once aout.h is cleaned a bit more */
174 #include <vlc_aout_mixer.h>
175
176 /* */
177 typedef struct
178 {
179     vout_thread_t  *(*pf_request_vout)( void *,
180                                         vout_thread_t *, video_format_t *, bool b_recycle );
181     void *p_private;
182 } aout_request_vout_t;
183
184 /** audio output filter */
185 typedef struct aout_filter_owner_sys_t aout_filter_owner_sys_t;
186 typedef struct aout_filter_sys_t aout_filter_sys_t;
187 struct aout_filter_t
188 {
189     VLC_COMMON_MEMBERS
190
191     audio_sample_format_t   input;
192     audio_sample_format_t   output;
193     aout_alloc_t            output_alloc;
194
195     module_t *              p_module;
196     aout_filter_sys_t       *p_sys;
197
198     bool                    b_in_place;
199     bool                    b_continuity;
200
201     void                    (*pf_do_work)( aout_instance_t *, aout_filter_t *,
202                                            aout_buffer_t *, aout_buffer_t * );
203
204     /* Owner fieldS
205      * XXX You MUST not use them directly */
206
207     /* Vout callback
208      * XXX use aout_filter_RequestVout */
209     aout_request_vout_t request_vout;
210
211     /* Private structure for the owner of the filter */
212     aout_filter_owner_sys_t *p_owner;
213 };
214
215 #define AOUT_RESAMPLING_NONE     0
216 #define AOUT_RESAMPLING_UP       1
217 #define AOUT_RESAMPLING_DOWN     2
218
219 /** an output stream for the audio output */
220 typedef struct aout_output_t
221 {
222     audio_sample_format_t   output;
223     /* Indicates whether the audio output is currently starving, to avoid
224      * printing a 1,000 "output is starving" messages. */
225     bool              b_starving;
226
227     /* post-filters */
228     aout_filter_t *         pp_filters[AOUT_MAX_FILTERS];
229     int                     i_nb_filters;
230
231     aout_fifo_t             fifo;
232
233     struct module_t *       p_module;
234     struct aout_sys_t *     p_sys;
235     void                 (* pf_play)( aout_instance_t * );
236     int                  (* pf_volume_get )( aout_instance_t *, audio_volume_t * );
237     int                  (* pf_volume_set )( aout_instance_t *, audio_volume_t );
238     int                     i_nb_samples;
239
240     /* Current volume for the output - it's just a placeholder, the plug-in
241      * may or may not use it. */
242     audio_volume_t          i_volume;
243
244     /* If b_error == 1, there is no audio output pipeline. */
245     bool              b_error;
246 } aout_output_t;
247
248 /** audio output thread descriptor */
249 struct aout_instance_t
250 {
251     VLC_COMMON_MEMBERS
252
253     /* Locks : please note that if you need several of these locks, it is
254      * mandatory (to avoid deadlocks) to take them in the following order :
255      * mixer_lock, p_input->lock, output_fifo_lock, input_fifos_lock.
256      * --Meuuh */
257     /* When input_fifos_lock is taken, none of the p_input->fifo structures
258      * can be read or modified by a third-party thread. */
259     vlc_mutex_t             input_fifos_lock;
260     /* When mixer_lock is taken, all decoder threads willing to mix a
261      * buffer must wait until it is released. The output pipeline cannot
262      * be modified. No input stream can be added or removed. */
263     vlc_mutex_t             mixer_lock;
264     /* When output_fifo_lock is taken, the p_aout->output.fifo structure
265      * cannot be read or written  by a third-party thread. */
266     vlc_mutex_t             output_fifo_lock;
267
268     /* Input streams & pre-filters */
269     aout_input_t *          pp_inputs[AOUT_MAX_INPUTS];
270     int                     i_nb_inputs;
271
272     /* Mixer */
273     audio_sample_format_t   mixer_format;
274     aout_alloc_t            mixer_allocation;
275     float                   mixer_multiplier;
276     aout_mixer_t            *p_mixer;
277
278     /* Output plug-in */
279     aout_output_t           output;
280 };
281
282 /**
283  * It describes the audio channel order VLC except.
284  */
285 static const uint32_t pi_vlc_chan_order_wg4[] =
286 {
287     AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
288     AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
289     AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_REARCENTER,
290     AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0
291 };
292
293 /*****************************************************************************
294  * Prototypes
295  *****************************************************************************/
296
297 VLC_EXPORT( aout_buffer_t *, aout_OutputNextBuffer, ( aout_instance_t *, mtime_t, bool ) LIBVLC_USED );
298
299 /**
300  * This function computes the reordering needed to go from pi_chan_order_in to
301  * pi_chan_order_out.
302  * If pi_chan_order_in or pi_chan_order_out is NULL, it will assume that vlc
303  * internal (WG4) order is requested.
304  */
305 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 ) );
306 VLC_EXPORT( void, aout_ChannelReorder, ( uint8_t *, int, int, const int *, int ) );
307
308 /**
309  * This fonction will compute the extraction parameter into pi_selection to go
310  * from i_channels with their type given by pi_order_src[] into the order
311  * describe by pi_order_dst.
312  * It will also set :
313  * - *pi_channels as the number of channels that will be extracted which is
314  * lower (in case of non understood channels type) or equal to i_channels.
315  * - the layout of the channels (*pi_layout).
316  *
317  * It will return true if channel extraction is really needed, in which case
318  * aout_ChannelExtract must be used
319  *
320  * XXX It must be used when the source may have channel type not understood
321  * by VLC. In this case the channel type pi_order_src[] must be set to 0.
322  * XXX It must also be used if multiple channels have the same type.
323  */
324 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 ) );
325
326 /**
327  * Do the actual channels extraction using the parameters created by
328  * aout_CheckChannelExtraction.
329  *
330  * XXX this function does not work in place (p_dst and p_src must not overlap).
331  * XXX Only 8, 16, 24, 32, 64 bits per sample are supported.
332  */
333 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 ) );
334
335 /* */
336 VLC_EXPORT( unsigned int, aout_FormatNbChannels, ( const audio_sample_format_t * p_format ) LIBVLC_USED );
337 VLC_EXPORT( unsigned int, aout_BitsPerSample, ( vlc_fourcc_t i_format ) LIBVLC_USED );
338 VLC_EXPORT( void, aout_FormatPrepare, ( audio_sample_format_t * p_format ) );
339 VLC_EXPORT( void, aout_FormatPrint, ( aout_instance_t * p_aout, const char * psz_text, const audio_sample_format_t * p_format ) );
340 VLC_EXPORT( const char *, aout_FormatPrintChannels, ( const audio_sample_format_t * ) LIBVLC_USED );
341
342 VLC_EXPORT( mtime_t, aout_FifoFirstDate, ( aout_instance_t *, aout_fifo_t * ) LIBVLC_USED );
343 VLC_EXPORT( aout_buffer_t *, aout_FifoPop, ( aout_instance_t * p_aout, aout_fifo_t * p_fifo ) LIBVLC_USED );
344
345 /* From intf.c : */
346 VLC_EXPORT( void, aout_VolumeSoftInit, ( aout_instance_t * ) );
347 VLC_EXPORT( void, aout_VolumeNoneInit, ( aout_instance_t * ) );
348 #define aout_VolumeGet(a, b) __aout_VolumeGet(VLC_OBJECT(a), b)
349 VLC_EXPORT( int, __aout_VolumeGet, ( vlc_object_t *, audio_volume_t * ) );
350 #define aout_VolumeSet(a, b) __aout_VolumeSet(VLC_OBJECT(a), b)
351 VLC_EXPORT( int, __aout_VolumeSet, ( vlc_object_t *, audio_volume_t ) );
352 #define aout_VolumeUp(a, b, c) __aout_VolumeUp(VLC_OBJECT(a), b, c)
353 VLC_EXPORT( int, __aout_VolumeUp, ( vlc_object_t *, int, audio_volume_t * ) );
354 #define aout_VolumeDown(a, b, c) __aout_VolumeDown(VLC_OBJECT(a), b, c)
355 VLC_EXPORT( int, __aout_VolumeDown, ( vlc_object_t *, int, audio_volume_t * ) );
356 #define aout_ToggleMute(a, b) __aout_ToggleMute(VLC_OBJECT(a), b)
357 VLC_EXPORT( int, __aout_ToggleMute, ( vlc_object_t *, audio_volume_t * ) );
358 VLC_EXPORT( int, aout_FindAndRestart, ( vlc_object_t *, const char *, vlc_value_t, vlc_value_t, void * ) );
359 VLC_EXPORT( int, aout_ChannelsRestart, ( vlc_object_t *, const char *, vlc_value_t, vlc_value_t, void * ) );
360
361 VLC_EXPORT( void, aout_EnableFilter, (vlc_object_t *, const char *, bool ));
362 #define aout_EnableFilter( o, n, b ) \
363         aout_EnableFilter( VLC_OBJECT(o), n, b )
364
365 /* */
366 VLC_EXPORT( vout_thread_t *, aout_filter_RequestVout, ( aout_filter_t *, vout_thread_t *p_vout, video_format_t *p_fmt ) );
367
368 # ifdef __cplusplus
369 }
370 # endif
371
372 #endif /* _VLC_AOUT_H */