]> git.sesse.net Git - vlc/blob - src/audio_output/dec.c
aout: decouple mixer from audio output
[vlc] / src / audio_output / dec.c
1 /*****************************************************************************
2  * dec.c : audio output API towards decoders
3  *****************************************************************************
4  * Copyright (C) 2002-2007 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 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <assert.h>
32
33 #include <vlc_common.h>
34
35 #include <vlc_aout.h>
36 #include <vlc_input.h>
37
38 #include "aout_internal.h"
39
40 #undef aout_DecNew
41 /**
42  * Creates an audio output
43  */
44 aout_input_t *aout_DecNew( audio_output_t *p_aout,
45                            audio_sample_format_t *p_format,
46                            const audio_replay_gain_t *p_replay_gain,
47                            const aout_request_vout_t *p_request_vout )
48 {
49     /* Sanitize audio format */
50     if( p_format->i_channels > 32 )
51     {
52         msg_Err( p_aout, "too many audio channels (%u)",
53                  p_format->i_channels );
54         return NULL;
55     }
56     if( p_format->i_channels <= 0 )
57     {
58         msg_Err( p_aout, "no audio channels" );
59         return NULL;
60     }
61     if( p_format->i_channels != aout_FormatNbChannels( p_format ) )
62     {
63         msg_Err( p_aout, "incompatible audio channels count with layout mask" );
64         return NULL;
65     }
66
67     if( p_format->i_rate > 192000 )
68     {
69         msg_Err( p_aout, "excessive audio sample frequency (%u)",
70                  p_format->i_rate );
71         return NULL;
72     }
73     if( p_format->i_rate < 4000 )
74     {
75         msg_Err( p_aout, "too low audio sample frequency (%u)",
76                  p_format->i_rate );
77         return NULL;
78     }
79
80     aout_input_t *p_input = calloc( 1, sizeof(aout_input_t));
81     if( !p_input )
82         return NULL;
83
84     p_input->b_error = true;
85     p_input->b_paused = false;
86     p_input->i_pause_date = 0;
87
88     aout_FormatPrepare( p_format );
89
90     memcpy( &p_input->input, p_format,
91             sizeof(audio_sample_format_t) );
92     if( p_replay_gain )
93         p_input->replay_gain = *p_replay_gain;
94
95     /* We can only be called by the decoder, so no need to lock
96      * p_input->lock. */
97     aout_lock( p_aout );
98     assert( p_aout->p_input == NULL );
99     p_aout->p_input = p_input;
100
101     var_Destroy( p_aout, "audio-device" );
102     var_Destroy( p_aout, "audio-channels" );
103
104     /* Recreate the output using the new format. */
105     if( aout_OutputNew( p_aout, p_format ) < 0 )
106 #warning Input without output and mixer = bad idea.
107         goto out;
108
109     assert( p_aout->mixer == NULL );
110     p_aout->mixer = aout_MixerNew( p_aout, &p_aout->mixer_format );
111     if( p_aout->mixer == NULL )
112     {
113         aout_OutputDelete( p_aout );
114 #warning Memory leak.
115         p_input = NULL;
116         goto out;
117     }
118
119     aout_InputNew( p_aout, p_input, p_request_vout );
120 out:
121     aout_unlock( p_aout );
122     return p_input;
123 }
124
125 /*****************************************************************************
126  * aout_DecDelete : delete a decoder
127  *****************************************************************************/
128 void aout_DecDelete( audio_output_t * p_aout, aout_input_t * p_input )
129 {
130     aout_lock( p_aout );
131     /* Remove the input. */
132     assert( p_input == p_aout->p_input ); /* buggy decoder? */
133     p_aout->p_input = NULL;
134     aout_InputDelete( p_aout, p_input );
135
136     aout_OutputDelete( p_aout );
137     aout_MixerDelete( p_aout->mixer );
138     p_aout->mixer = NULL;
139     var_Destroy( p_aout, "audio-device" );
140     var_Destroy( p_aout, "audio-channels" );
141
142     aout_unlock( p_aout );
143     free( p_input );
144 }
145
146
147 /*
148  * Buffer management
149  */
150
151 /*****************************************************************************
152  * aout_DecNewBuffer : ask for a new empty buffer
153  *****************************************************************************/
154 aout_buffer_t * aout_DecNewBuffer( aout_input_t * p_input,
155                                    size_t i_nb_samples )
156 {
157     size_t length = i_nb_samples * p_input->input.i_bytes_per_frame
158                                  / p_input->input.i_frame_length;
159     block_t *block = block_Alloc( length );
160     if( likely(block != NULL) )
161     {
162         block->i_nb_samples = i_nb_samples;
163         block->i_pts = block->i_length = 0;
164     }
165     return block;
166 }
167
168 /*****************************************************************************
169  * aout_DecDeleteBuffer : destroy an undecoded buffer
170  *****************************************************************************/
171 void aout_DecDeleteBuffer( audio_output_t * p_aout, aout_input_t * p_input,
172                            aout_buffer_t * p_buffer )
173 {
174     (void)p_aout; (void)p_input;
175     aout_BufferFree( p_buffer );
176 }
177
178 /*****************************************************************************
179  * aout_DecPlay : filter & mix the decoded buffer
180  *****************************************************************************/
181 int aout_DecPlay( audio_output_t * p_aout, aout_input_t * p_input,
182                   aout_buffer_t * p_buffer, int i_input_rate )
183 {
184     assert( i_input_rate >= INPUT_RATE_DEFAULT / AOUT_MAX_INPUT_RATE &&
185             i_input_rate <= INPUT_RATE_DEFAULT * AOUT_MAX_INPUT_RATE );
186     assert( p_buffer->i_pts > 0 );
187
188     p_buffer->i_length = (mtime_t)p_buffer->i_nb_samples * 1000000
189                                 / p_input->input.i_rate;
190
191     aout_lock( p_aout );
192     if( p_input->b_error )
193     {
194         aout_unlock( p_aout );
195         aout_BufferFree( p_buffer );
196         return -1;
197     }
198
199     aout_InputCheckAndRestart( p_aout, p_input );
200     aout_InputPlay( p_aout, p_input, p_buffer, i_input_rate );
201
202     const float amp = p_aout->mixer_multiplier * p_input->multiplier;
203     while( (p_buffer = aout_OutputSlice( p_aout, &p_input->fifo ) ) != NULL )
204     {
205         aout_MixerRun( p_aout->mixer, p_buffer, amp );
206         aout_OutputPlay( p_aout, p_buffer );
207     }
208     aout_unlock( p_aout );
209     return 0;
210 }
211
212 int aout_DecGetResetLost( audio_output_t *p_aout, aout_input_t *p_input )
213 {
214     int val;
215
216     aout_lock( p_aout );
217     val = p_input->i_buffer_lost;
218     p_input->i_buffer_lost = 0;
219     aout_unlock( p_aout );
220
221     return val;
222 }
223
224 void aout_DecChangePause( audio_output_t *p_aout, aout_input_t *p_input, bool b_paused, mtime_t i_date )
225 {
226     mtime_t i_duration = 0;
227
228     aout_lock( p_aout );
229     assert( p_aout->p_input == p_input );
230     assert( !p_input->b_paused || !b_paused );
231     if( p_input->b_paused )
232     {
233         i_duration = i_date - p_input->i_pause_date;
234     }
235     p_input->b_paused = b_paused;
236     p_input->i_pause_date = i_date;
237
238     if( i_duration != 0 )
239     {
240         aout_FifoMoveDates( &p_input->fifo, i_duration );
241         aout_FifoMoveDates( &p_aout->fifo, i_duration );
242     }
243     aout_OutputPause( p_aout, b_paused, i_date );
244     aout_unlock( p_aout );
245 }
246
247 void aout_DecFlush( audio_output_t *p_aout, aout_input_t *p_input )
248 {
249     aout_lock( p_aout );
250     aout_FifoReset( &p_input->fifo );
251     aout_FifoReset( &p_aout->fifo );
252     aout_OutputFlush( p_aout, false );
253     aout_unlock( p_aout );
254 }
255
256 bool aout_DecIsEmpty( audio_output_t * p_aout, aout_input_t * p_input )
257 {
258     mtime_t end_date;
259
260     aout_lock( p_aout );
261     end_date = aout_FifoNextStart( &p_input->fifo );
262     aout_unlock( p_aout );
263     return end_date == VLC_TS_INVALID || end_date <= mdate();
264 }