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