]> git.sesse.net Git - vlc/blob - src/audio_output/dec.c
Add vlclua_dir_list_free to free list created by vlclua_dir_list and use it.
[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         return NULL;
61     }
62     if( p_format->i_channels <= 0 )
63     {
64         msg_Err( p_aout, "no audio channels" );
65         return NULL;
66     }
67
68     if( p_format->i_rate > 192000 )
69     {
70         msg_Err( p_aout, "excessive audio sample frequency (%u)",
71                  p_format->i_rate );
72         return NULL;
73     }
74     if( p_format->i_rate < 4000 )
75     {
76         msg_Err( p_aout, "too low audio sample frequency (%u)",
77                  p_format->i_rate );
78         return NULL;
79     }
80
81     /* We can only be called by the decoder, so no need to lock
82      * p_input->lock. */
83     vlc_mutex_lock( &p_aout->mixer_lock );
84
85     if ( p_aout->i_nb_inputs >= AOUT_MAX_INPUTS )
86     {
87         msg_Err( p_aout, "too many inputs already (%d)", p_aout->i_nb_inputs );
88         goto error;
89     }
90
91     p_input = malloc(sizeof(aout_input_t));
92     if ( p_input == NULL )
93         goto error;
94     memset( p_input, 0, sizeof(aout_input_t) );
95
96     vlc_mutex_init( &p_input->lock );
97
98     p_input->b_changed = 0;
99     p_input->b_error = 1;
100
101     aout_FormatPrepare( p_format );
102
103     memcpy( &p_input->input, p_format,
104             sizeof(audio_sample_format_t) );
105     if( p_replay_gain )
106         p_input->replay_gain = *p_replay_gain;
107
108     p_aout->pp_inputs[p_aout->i_nb_inputs] = p_input;
109     p_aout->i_nb_inputs++;
110
111     if ( p_aout->mixer.b_error )
112     {
113         int i;
114
115         var_Destroy( p_aout, "audio-device" );
116         var_Destroy( p_aout, "audio-channels" );
117
118         /* Recreate the output using the new format. */
119         if ( aout_OutputNew( p_aout, p_format ) < 0 )
120         {
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                 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
126             }
127             vlc_mutex_unlock( &p_aout->mixer_lock );
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             vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
135             aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
136             aout_InputNew( p_aout, p_aout->pp_inputs[i] );
137             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
138         }
139     }
140     else
141     {
142         aout_MixerDelete( p_aout );
143     }
144
145     if ( aout_MixerNew( p_aout ) == -1 )
146     {
147         aout_OutputDelete( p_aout );
148         goto error;
149     }
150
151     aout_InputNew( p_aout, p_input );
152
153     vlc_mutex_unlock( &p_aout->mixer_lock );
154     p_input->i_desync = var_CreateGetInteger( p_this, "audio-desync" ) * 1000;
155
156     p_input_thread = (input_thread_t *)vlc_object_find( p_this,
157                                            VLC_OBJECT_INPUT, FIND_PARENT );
158     if( p_input_thread )
159     {
160         p_input->i_pts_delay = p_input_thread->i_pts_delay;
161         p_input->i_pts_delay += p_input->i_desync;
162         p_input->p_input_thread = p_input_thread;
163         vlc_object_release( p_input_thread );
164     }
165     else
166     {
167         p_input->i_pts_delay = DEFAULT_PTS_DELAY;
168         p_input->i_pts_delay += p_input->i_desync;
169         p_input->p_input_thread = NULL;
170     }
171
172     return p_input;
173
174 error:
175     vlc_mutex_unlock( &p_aout->mixer_lock );
176     return NULL;
177 }
178
179 aout_input_t * __aout_DecNew( vlc_object_t * p_this,
180                               aout_instance_t ** pp_aout,
181                               audio_sample_format_t * p_format,
182                               audio_replay_gain_t *p_replay_gain )
183 {
184     aout_instance_t *p_aout = *pp_aout;
185     if ( p_aout == NULL )
186     {
187         msg_Dbg( p_this, "no aout present, spawning one" );
188         p_aout = aout_New( p_this );
189
190         /* Everything failed, I'm a loser, I just wanna die */
191         if( p_aout == NULL )
192             return NULL;
193
194         vlc_object_attach( p_aout, p_this );
195         *pp_aout = p_aout;
196     }
197
198     return DecNew( p_this, p_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_input_t * p_input,
264                                    size_t i_nb_samples )
265 {
266     aout_buffer_t * p_buffer;
267     mtime_t duration;
268
269     vlc_mutex_lock( &p_input->lock );
270
271     if ( p_input->b_error )
272     {
273         vlc_mutex_unlock( &p_input->lock );
274         return NULL;
275     }
276
277     duration = (1000000 * (mtime_t)i_nb_samples) / p_input->input.i_rate;
278
279     /* This necessarily allocates in the heap. */
280     aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_buffer );
281     if( p_buffer != NULL )
282         p_buffer->i_nb_bytes = i_nb_samples * p_input->input.i_bytes_per_frame
283                                   / p_input->input.i_frame_length;
284
285     /* Suppose the decoder doesn't have more than one buffered buffer */
286     p_input->b_changed = 0;
287
288     vlc_mutex_unlock( &p_input->lock );
289
290     if( p_buffer == NULL )
291         return NULL;
292
293     p_buffer->i_nb_samples = i_nb_samples;
294     p_buffer->start_date = p_buffer->end_date = 0;
295     return p_buffer;
296 }
297
298 /*****************************************************************************
299  * aout_DecDeleteBuffer : destroy an undecoded buffer
300  *****************************************************************************/
301 void aout_DecDeleteBuffer( aout_instance_t * p_aout, aout_input_t * p_input,
302                            aout_buffer_t * p_buffer )
303 {
304     (void)p_aout; (void)p_input;
305     aout_BufferFree( p_buffer );
306 }
307
308 /*****************************************************************************
309  * aout_DecPlay : filter & mix the decoded buffer
310  *****************************************************************************/
311 int aout_DecPlay( aout_instance_t * p_aout, aout_input_t * p_input,
312                   aout_buffer_t * p_buffer, int i_input_rate )
313 {
314     if ( p_buffer->start_date == 0 )
315     {
316         msg_Warn( p_aout, "non-dated buffer received" );
317         aout_BufferFree( p_buffer );
318         return -1;
319     }
320
321 #ifndef FIXME
322     /* This hack for #transcode{acodec=...}:display to work -- Courmisch */
323     if( i_input_rate == 0 )
324         i_input_rate = INPUT_RATE_DEFAULT;
325 #endif
326     if( i_input_rate > INPUT_RATE_DEFAULT * AOUT_MAX_INPUT_RATE ||
327         i_input_rate < INPUT_RATE_DEFAULT / AOUT_MAX_INPUT_RATE )
328     {
329         aout_BufferFree( p_buffer );
330         return 0;
331     }
332
333     /* Apply the desynchronisation requested by the user */
334     p_buffer->start_date += p_input->i_desync;
335     p_buffer->end_date += p_input->i_desync;
336
337     if ( p_buffer->start_date > mdate() + p_input->i_pts_delay +
338          AOUT_MAX_ADVANCE_TIME )
339     {
340         msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
341                   p_buffer->start_date - mdate());
342         if( p_input->p_input_thread )
343         {
344             vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
345             stats_UpdateInteger( p_aout,
346                            p_input->p_input_thread->p->counters.p_lost_abuffers,
347                            1, NULL );
348             vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
349         }
350         aout_BufferFree( p_buffer );
351         return -1;
352     }
353
354     p_buffer->end_date = p_buffer->start_date
355                             + (mtime_t)p_buffer->i_nb_samples * 1000000
356                                 / p_input->input.i_rate;
357
358     vlc_mutex_lock( &p_input->lock );
359
360     if ( p_input->b_error )
361     {
362         vlc_mutex_unlock( &p_input->lock );
363         aout_BufferFree( p_buffer );
364         return -1;
365     }
366
367     if ( p_input->b_changed )
368     {
369         /* Maybe the allocation size has changed. Re-allocate a buffer. */
370         aout_buffer_t * p_new_buffer;
371         mtime_t duration = (1000000 * (mtime_t)p_buffer->i_nb_samples)
372                             / p_input->input.i_rate;
373
374         aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_new_buffer );
375         vlc_memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
376                     p_buffer->i_nb_bytes );
377         p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
378         p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
379         p_new_buffer->start_date = p_buffer->start_date;
380         p_new_buffer->end_date = p_buffer->end_date;
381         aout_BufferFree( p_buffer );
382         p_buffer = p_new_buffer;
383         p_input->b_changed = 0;
384     }
385
386     /* If the buffer is too early, wait a while. */
387     mwait( p_buffer->start_date - AOUT_MAX_PREPARE_TIME );
388
389     if ( aout_InputPlay( p_aout, p_input, p_buffer, i_input_rate ) == -1 )
390     {
391         vlc_mutex_unlock( &p_input->lock );
392         return -1;
393     }
394
395     vlc_mutex_unlock( &p_input->lock );
396
397     /* Run the mixer if it is able to run. */
398     vlc_mutex_lock( &p_aout->mixer_lock );
399     aout_MixerRun( p_aout );
400     if( p_input->p_input_thread )
401     {
402         vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
403         stats_UpdateInteger( p_aout,
404                              p_input->p_input_thread->p->counters.p_played_abuffers,
405                              1, NULL );
406         vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
407     }
408     vlc_mutex_unlock( &p_aout->mixer_lock );
409
410     return 0;
411 }