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