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