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