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