]> git.sesse.net Git - vlc/blob - src/audio_output/dec.c
50ac040de0d917c3637a2eaf3ae7ff77c56e38fb
[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( aout_instance_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     aout_input_t * p_input;
50
51     /* Sanitize audio format */
52     if( p_format->i_channels > 32 )
53     {
54         msg_Err( p_aout, "too many audio channels (%u)",
55                  p_format->i_channels );
56         return NULL;
57     }
58     if( p_format->i_channels <= 0 )
59     {
60         msg_Err( p_aout, "no audio channels" );
61         return NULL;
62     }
63     if( p_format->i_channels != aout_FormatNbChannels( p_format ) )
64     {
65         msg_Err( p_aout, "incompatible audio channels count with layout mask" );
66         return NULL;
67     }
68
69     if( p_format->i_rate > 192000 )
70     {
71         msg_Err( p_aout, "excessive audio sample frequency (%u)",
72                  p_format->i_rate );
73         return NULL;
74     }
75     if( p_format->i_rate < 4000 )
76     {
77         msg_Err( p_aout, "too low audio sample frequency (%u)",
78                  p_format->i_rate );
79         return NULL;
80     }
81
82     /* We can only be called by the decoder, so no need to lock
83      * p_input->lock. */
84     aout_lock_mixer( p_aout );
85     assert( p_aout->i_nb_inputs == 0 );
86
87     p_input = calloc( 1, sizeof(aout_input_t));
88     if( !p_input )
89         goto error;
90
91     vlc_mutex_init( &p_input->lock );
92
93     p_input->b_changed = false;
94     p_input->b_error = true;
95     p_input->b_paused = false;
96     p_input->i_pause_date = 0;
97
98     aout_FormatPrepare( p_format );
99
100     memcpy( &p_input->input, p_format,
101             sizeof(audio_sample_format_t) );
102     if( p_replay_gain )
103         p_input->replay_gain = *p_replay_gain;
104
105     aout_lock_input_fifos( p_aout );
106     p_aout->pp_inputs[p_aout->i_nb_inputs] = p_input;
107     p_aout->i_nb_inputs++;
108
109     if ( !p_aout->p_mixer )
110     {
111         int i;
112
113         var_Destroy( p_aout, "audio-device" );
114         var_Destroy( p_aout, "audio-channels" );
115
116         /* Recreate the output using the new format. */
117         if ( aout_OutputNew( p_aout, p_format ) < 0 )
118         {
119             for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
120             {
121                 aout_lock_input( p_aout, p_aout->pp_inputs[i] );
122                 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
123                 aout_unlock_input( p_aout, p_aout->pp_inputs[i] );
124             }
125             aout_unlock_input_fifos( p_aout );
126             aout_unlock_mixer( p_aout );
127             return p_input;
128         }
129
130         /* Create other input streams. */
131         for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
132         {
133             aout_input_t *p_input = p_aout->pp_inputs[i];
134
135             aout_lock_input( p_aout, p_input );
136             aout_InputDelete( p_aout, p_input );
137             aout_InputNew( p_aout, p_input, &p_input->request_vout );
138             aout_unlock_input( p_aout, p_input );
139         }
140     }
141     else
142     {
143         aout_MixerDelete( p_aout );
144     }
145
146     if ( aout_MixerNew( p_aout ) == -1 )
147     {
148         aout_OutputDelete( p_aout );
149         aout_unlock_input_fifos( p_aout );
150         goto error;
151     }
152
153     aout_InputNew( p_aout, p_input, p_request_vout );
154     aout_unlock_input_fifos( p_aout );
155
156     aout_unlock_mixer( p_aout );
157
158     return p_input;
159
160 error:
161     aout_unlock_mixer( p_aout );
162     return NULL;
163 }
164
165 /*****************************************************************************
166  * aout_DecDelete : delete a decoder
167  *****************************************************************************/
168 int aout_DecDelete( aout_instance_t * p_aout, aout_input_t * p_input )
169 {
170     int i_input;
171
172     /* This function can only be called by the decoder itself, so no need
173      * to lock p_input->lock. */
174     aout_lock_mixer( p_aout );
175
176     for ( i_input = 0; i_input < p_aout->i_nb_inputs; i_input++ )
177     {
178         if ( p_aout->pp_inputs[i_input] == p_input )
179         {
180             break;
181         }
182     }
183
184     if ( i_input == p_aout->i_nb_inputs )
185     {
186         msg_Err( p_aout, "cannot find an input to delete" );
187         aout_unlock_mixer( p_aout );
188         return -1;
189     }
190
191     /* Remove the input from the list. */
192     p_aout->i_nb_inputs--;
193     assert( p_aout->i_nb_inputs == 0 );
194
195     aout_InputDelete( p_aout, p_input );
196
197     vlc_mutex_destroy( &p_input->lock );
198     free( p_input );
199
200     if ( !p_aout->i_nb_inputs )
201     {
202         aout_OutputDelete( p_aout );
203         aout_MixerDelete( p_aout );
204         var_Destroy( p_aout, "audio-device" );
205         var_Destroy( p_aout, "audio-channels" );
206     }
207
208     aout_unlock_mixer( p_aout );
209
210     return 0;
211 }
212
213
214 /*
215  * Buffer management
216  */
217
218 /*****************************************************************************
219  * aout_DecNewBuffer : ask for a new empty buffer
220  *****************************************************************************/
221 aout_buffer_t * aout_DecNewBuffer( aout_input_t * p_input,
222                                    size_t i_nb_samples )
223 {
224     block_t *block;
225     size_t length;
226
227     aout_lock_input( NULL, p_input );
228
229     if ( p_input->b_error )
230     {
231         aout_unlock_input( NULL, p_input );
232         return NULL;
233     }
234
235     length = i_nb_samples * p_input->input.i_bytes_per_frame
236                           / p_input->input.i_frame_length;
237     block = block_Alloc( length );
238
239     /* Suppose the decoder doesn't have more than one buffered buffer */
240     p_input->b_changed = false;
241
242     aout_unlock_input( NULL, p_input );
243
244     if( likely(block != NULL) )
245     {
246         block->i_nb_samples = i_nb_samples;
247         block->i_pts = block->i_length = 0;
248     }
249     return block;
250 }
251
252 /*****************************************************************************
253  * aout_DecDeleteBuffer : destroy an undecoded buffer
254  *****************************************************************************/
255 void aout_DecDeleteBuffer( aout_instance_t * p_aout, aout_input_t * p_input,
256                            aout_buffer_t * p_buffer )
257 {
258     (void)p_aout; (void)p_input;
259     aout_BufferFree( p_buffer );
260 }
261
262 /*****************************************************************************
263  * aout_DecPlay : filter & mix the decoded buffer
264  *****************************************************************************/
265 int aout_DecPlay( aout_instance_t * p_aout, aout_input_t * p_input,
266                   aout_buffer_t * p_buffer, int i_input_rate )
267 {
268     assert( i_input_rate >= INPUT_RATE_DEFAULT / AOUT_MAX_INPUT_RATE &&
269             i_input_rate <= INPUT_RATE_DEFAULT * AOUT_MAX_INPUT_RATE );
270
271     assert( p_buffer->i_pts > 0 );
272
273     p_buffer->i_length = (mtime_t)p_buffer->i_nb_samples * 1000000
274                                 / p_input->input.i_rate;
275
276     aout_lock_mixer( p_aout );
277     aout_lock_input( p_aout, p_input );
278
279     if( p_input->b_error )
280     {
281         aout_unlock_input( p_aout, p_input );
282         aout_unlock_mixer( p_aout );
283
284         aout_BufferFree( p_buffer );
285         return -1;
286     }
287
288     if( p_input->b_changed )
289     {
290         /* Maybe the allocation size has changed. Re-allocate a buffer. */
291         aout_buffer_t * p_new_buffer;
292         mtime_t duration = (1000000 * (mtime_t)p_buffer->i_nb_samples)
293                             / p_input->input.i_rate;
294
295         p_new_buffer = aout_BufferAlloc( &p_input->input_alloc, duration, NULL);
296         vlc_memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
297                     p_buffer->i_buffer );
298         p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
299         p_new_buffer->i_buffer = p_buffer->i_buffer;
300         p_new_buffer->i_pts = p_buffer->i_pts;
301         p_new_buffer->i_length = p_buffer->i_length;
302         aout_BufferFree( p_buffer );
303         p_buffer = p_new_buffer;
304         p_input->b_changed = false;
305     }
306
307     aout_InputCheckAndRestart( p_aout, p_input );
308     aout_unlock_mixer( p_aout );
309
310     int i_ret = aout_InputPlay( p_aout, p_input, p_buffer, i_input_rate );
311
312     aout_unlock_input( p_aout, p_input );
313
314     if( i_ret == -1 )
315         return -1;
316
317     /* Run the mixer if it is able to run. */
318     aout_lock_mixer( p_aout );
319
320     aout_MixerRun( p_aout );
321
322     aout_unlock_mixer( p_aout );
323
324     return 0;
325 }
326
327 int aout_DecGetResetLost( aout_instance_t *p_aout, aout_input_t *p_input )
328 {
329     aout_lock_input( p_aout, p_input );
330     int i_value = p_input->i_buffer_lost;
331     p_input->i_buffer_lost = 0;
332     aout_unlock_input( p_aout, p_input );
333
334     return i_value;
335 }
336
337 void aout_DecChangePause( aout_instance_t *p_aout, aout_input_t *p_input, bool b_paused, mtime_t i_date )
338 {
339     mtime_t i_duration = 0;
340     aout_lock_input( p_aout, p_input );
341     assert( !p_input->b_paused || !b_paused );
342     if( p_input->b_paused )
343     {
344         i_duration = i_date - p_input->i_pause_date;
345     }
346     p_input->b_paused = b_paused;
347     p_input->i_pause_date = i_date;
348     aout_unlock_input( p_aout, p_input );
349
350     if( i_duration != 0 )
351     {
352         aout_lock_mixer( p_aout );
353         for( aout_buffer_t *p = p_input->mixer.fifo.p_first; p != NULL; p = p->p_next )
354         {
355             p->i_pts += i_duration;
356         }
357         aout_unlock_mixer( p_aout );
358     }
359 }
360
361 void aout_DecFlush( aout_instance_t *p_aout, aout_input_t *p_input )
362 {
363     aout_lock_input_fifos( p_aout );
364
365     aout_FifoSet( p_aout, &p_input->mixer.fifo, 0 );
366     p_input->mixer.begin = NULL;
367
368     aout_unlock_input_fifos( p_aout );
369 }
370