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