]> git.sesse.net Git - vlc/blob - src/audio_output/dec.c
Use calloc instead of malloc+memset.
[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                               const audio_replay_gain_t *p_replay_gain,
48                               const aout_request_vout_t *p_request_vout )
49 {
50     aout_input_t * p_input;
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         return NULL;
58     }
59     if( p_format->i_channels <= 0 )
60     {
61         msg_Err( p_aout, "no audio channels" );
62         return NULL;
63     }
64
65     if( p_format->i_rate > 192000 )
66     {
67         msg_Err( p_aout, "excessive audio sample frequency (%u)",
68                  p_format->i_rate );
69         return NULL;
70     }
71     if( p_format->i_rate < 4000 )
72     {
73         msg_Err( p_aout, "too low audio sample frequency (%u)",
74                  p_format->i_rate );
75         return NULL;
76     }
77
78     /* We can only be called by the decoder, so no need to lock
79      * p_input->lock. */
80     aout_lock_mixer( p_aout );
81
82     if ( p_aout->i_nb_inputs >= AOUT_MAX_INPUTS )
83     {
84         msg_Err( p_aout, "too many inputs already (%d)", p_aout->i_nb_inputs );
85         goto error;
86     }
87
88     p_input = calloc( 1, sizeof(aout_input_t));
89     if( !p_input )
90         goto error;
91
92     vlc_mutex_init( &p_input->lock );
93
94     p_input->b_changed = false;
95     p_input->b_error = true;
96     p_input->b_paused = false;
97     p_input->i_pause_date = 0;
98
99     aout_FormatPrepare( p_format );
100
101     memcpy( &p_input->input, p_format,
102             sizeof(audio_sample_format_t) );
103     if( p_replay_gain )
104         p_input->replay_gain = *p_replay_gain;
105
106     aout_lock_input_fifos( p_aout );
107     p_aout->pp_inputs[p_aout->i_nb_inputs] = p_input;
108     p_aout->i_nb_inputs++;
109
110     if ( p_aout->mixer.b_error )
111     {
112         int i;
113
114         var_Destroy( p_aout, "audio-device" );
115         var_Destroy( p_aout, "audio-channels" );
116
117         /* Recreate the output using the new format. */
118         if ( aout_OutputNew( p_aout, p_format ) < 0 )
119         {
120             for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
121             {
122                 aout_lock_input( p_aout, p_aout->pp_inputs[i] );
123                 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
124                 aout_unlock_input( p_aout, p_aout->pp_inputs[i] );
125             }
126             aout_unlock_input_fifos( p_aout );
127             aout_unlock_mixer( p_aout );
128             return p_input;
129         }
130
131         /* Create other input streams. */
132         for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
133         {
134             aout_input_t *p_input = p_aout->pp_inputs[i];
135
136             aout_lock_input( p_aout, p_input );
137             aout_InputDelete( p_aout, p_input );
138             aout_InputNew( p_aout, p_input, &p_input->request_vout );
139             aout_unlock_input( p_aout, p_input );
140         }
141     }
142     else
143     {
144         aout_MixerDelete( p_aout );
145     }
146
147     if ( aout_MixerNew( p_aout ) == -1 )
148     {
149         aout_OutputDelete( p_aout );
150         aout_unlock_input_fifos( p_aout );
151         goto error;
152     }
153
154     aout_InputNew( p_aout, p_input, p_request_vout );
155     aout_unlock_input_fifos( p_aout );
156
157     aout_unlock_mixer( p_aout );
158
159     return p_input;
160
161 error:
162     aout_unlock_mixer( p_aout );
163     return NULL;
164 }
165
166 aout_input_t * __aout_DecNew( vlc_object_t * p_this,
167                               aout_instance_t ** pp_aout,
168                               audio_sample_format_t * p_format,
169                               const audio_replay_gain_t *p_replay_gain,
170                               const aout_request_vout_t *p_request_video )
171 {
172     aout_instance_t *p_aout = *pp_aout;
173     if ( p_aout == NULL )
174     {
175         msg_Dbg( p_this, "no aout present, spawning one" );
176         p_aout = aout_New( p_this );
177
178         /* Everything failed, I'm a loser, I just wanna die */
179         if( p_aout == NULL )
180             return NULL;
181
182         vlc_object_attach( p_aout, p_this );
183         *pp_aout = p_aout;
184     }
185
186     return DecNew( p_this, p_aout, p_format, p_replay_gain, p_request_video );
187 }
188
189 /*****************************************************************************
190  * aout_DecDelete : delete a decoder
191  *****************************************************************************/
192 int aout_DecDelete( aout_instance_t * p_aout, aout_input_t * p_input )
193 {
194     int i_input;
195
196     /* This function can only be called by the decoder itself, so no need
197      * to lock p_input->lock. */
198     aout_lock_mixer( p_aout );
199
200     for ( i_input = 0; i_input < p_aout->i_nb_inputs; i_input++ )
201     {
202         if ( p_aout->pp_inputs[i_input] == p_input )
203         {
204             break;
205         }
206     }
207
208     if ( i_input == p_aout->i_nb_inputs )
209     {
210         msg_Err( p_aout, "cannot find an input to delete" );
211         aout_unlock_mixer( p_aout );
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     aout_unlock_mixer( p_aout );
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     aout_lock_input( NULL, p_input );
259
260     if ( p_input->b_error )
261     {
262         aout_unlock_input( NULL, p_input );
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 = false;
276
277     aout_unlock_input( NULL, p_input );
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     assert( i_input_rate >= INPUT_RATE_DEFAULT / AOUT_MAX_INPUT_RATE &&
304             i_input_rate <= INPUT_RATE_DEFAULT * AOUT_MAX_INPUT_RATE );
305
306     assert( p_buffer->start_date > 0 );
307
308     p_buffer->end_date = p_buffer->start_date
309                             + (mtime_t)p_buffer->i_nb_samples * 1000000
310                                 / p_input->input.i_rate;
311
312     aout_lock_input( p_aout, p_input );
313
314     if( p_input->b_error )
315     {
316         aout_unlock_input( p_aout, p_input );
317         aout_BufferFree( p_buffer );
318         return -1;
319     }
320
321     if( p_input->b_changed )
322     {
323         /* Maybe the allocation size has changed. Re-allocate a buffer. */
324         aout_buffer_t * p_new_buffer;
325         mtime_t duration = (1000000 * (mtime_t)p_buffer->i_nb_samples)
326                             / p_input->input.i_rate;
327
328         aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_new_buffer );
329         vlc_memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
330                     p_buffer->i_nb_bytes );
331         p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
332         p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
333         p_new_buffer->start_date = p_buffer->start_date;
334         p_new_buffer->end_date = p_buffer->end_date;
335         aout_BufferFree( p_buffer );
336         p_buffer = p_new_buffer;
337         p_input->b_changed = false;
338     }
339
340     int i_ret = aout_InputPlay( p_aout, p_input, p_buffer, i_input_rate );
341
342     aout_unlock_input( p_aout, p_input );
343
344     if( i_ret == -1 )
345         return -1;
346
347     /* Run the mixer if it is able to run. */
348     aout_lock_mixer( p_aout );
349
350     aout_MixerRun( p_aout );
351
352     aout_unlock_mixer( p_aout );
353
354     return 0;
355 }
356
357 int aout_DecGetResetLost( aout_instance_t *p_aout, aout_input_t *p_input )
358 {
359     aout_lock_input( p_aout, p_input );
360     int i_value = p_input->i_buffer_lost;
361     p_input->i_buffer_lost = 0;
362     aout_unlock_input( p_aout, p_input );
363
364     return i_value;
365 }
366
367 void aout_DecChangePause( aout_instance_t *p_aout, aout_input_t *p_input, bool b_paused, mtime_t i_date )
368 {
369     mtime_t i_duration = 0;
370     aout_lock_input( p_aout, p_input );
371     assert( !p_input->b_paused || !b_paused );
372     if( p_input->b_paused )
373     {
374         i_duration = i_date - p_input->i_pause_date;
375     }
376     p_input->b_paused = b_paused;
377     p_input->i_pause_date = i_date;
378     aout_unlock_input( p_aout, p_input );
379
380     if( i_duration != 0 )
381     {
382         aout_lock_mixer( p_aout );
383         for( aout_buffer_t *p = p_input->fifo.p_first; p != NULL; p = p->p_next )
384         {
385             p->start_date += i_duration;
386             p->end_date += i_duration;
387         }
388         aout_unlock_mixer( p_aout );
389     }
390 }
391
392 void aout_DecFlush( aout_instance_t *p_aout, aout_input_t *p_input )
393 {
394     aout_lock_input_fifos( p_aout );
395
396     aout_FifoSet( p_aout, &p_input->fifo, 0 );
397     p_input->p_first_byte_to_mix = NULL;
398
399     aout_unlock_input_fifos( p_aout );
400 }
401