]> git.sesse.net Git - vlc/blob - src/audio_output/dec.c
cd09fac9a2ecf7a13f124a2344a8133ef9f06aa5
[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 <assert.h>
32
33 #include <vlc_common.h>
34
35 #include <vlc_aout.h>
36 #include <vlc_input.h>
37
38 #include "aout_internal.h"
39
40 #undef aout_DecNew
41 /**
42  * Creates an audio output
43  */
44 aout_input_t *aout_DecNew( audio_output_t *p_aout,
45                            audio_sample_format_t *p_format,
46                            const audio_replay_gain_t *p_replay_gain,
47                            const aout_request_vout_t *p_request_vout )
48 {
49     /* Sanitize audio format */
50     if( p_format->i_channels > 32 )
51     {
52         msg_Err( p_aout, "too many audio channels (%u)",
53                  p_format->i_channels );
54         return NULL;
55     }
56     if( p_format->i_channels <= 0 )
57     {
58         msg_Err( p_aout, "no audio channels" );
59         return NULL;
60     }
61     if( p_format->i_channels != aout_FormatNbChannels( p_format ) )
62     {
63         msg_Err( p_aout, "incompatible audio channels count with layout mask" );
64         return NULL;
65     }
66
67     if( p_format->i_rate > 192000 )
68     {
69         msg_Err( p_aout, "excessive audio sample frequency (%u)",
70                  p_format->i_rate );
71         return NULL;
72     }
73     if( p_format->i_rate < 4000 )
74     {
75         msg_Err( p_aout, "too low audio sample frequency (%u)",
76                  p_format->i_rate );
77         return NULL;
78     }
79
80     aout_input_t *p_input = calloc( 1, sizeof(aout_input_t));
81     if( !p_input )
82         return NULL;
83
84     p_input->b_error = true;
85
86     aout_FormatPrepare( p_format );
87
88     memcpy( &p_input->input, p_format,
89             sizeof(audio_sample_format_t) );
90     if( p_replay_gain )
91         p_input->replay_gain = *p_replay_gain;
92
93     /* We can only be called by the decoder, so no need to lock
94      * p_input->lock. */
95     aout_owner_t *owner = aout_owner(p_aout);
96     aout_lock( p_aout );
97     assert (owner->input == NULL);
98
99     var_Destroy( p_aout, "audio-device" );
100     var_Destroy( p_aout, "audio-channels" );
101
102     /* Recreate the output using the new format. */
103     if( aout_OutputNew( p_aout, p_format ) < 0 )
104         goto error;
105
106     assert (owner->volume.mixer == NULL);
107     owner->volume.mixer = aout_MixerNew (p_aout, owner->mixer_format.i_format);
108     if (owner->volume.mixer == NULL)
109     {
110         aout_OutputDelete( p_aout );
111         goto error;
112     }
113
114     owner->input = p_input;
115     aout_InputNew( p_aout, p_input, p_request_vout );
116     aout_unlock( p_aout );
117     return p_input;
118 error:
119     aout_unlock( p_aout );
120     free( p_input );
121     return NULL;
122 }
123
124 /*****************************************************************************
125  * aout_DecDelete : delete a decoder
126  *****************************************************************************/
127 void aout_DecDelete( audio_output_t * p_aout, aout_input_t * p_input )
128 {
129     aout_owner_t *owner = aout_owner (p_aout);
130     struct audio_mixer *mixer;
131
132     aout_lock( p_aout );
133     /* Remove the input. */
134     assert (owner->input == p_input); /* buggy decoder? */
135     owner->input = NULL;
136     aout_InputDelete( p_aout, p_input );
137
138     aout_OutputDelete( p_aout );
139     mixer = owner->volume.mixer;
140     owner->volume.mixer = NULL;
141     var_Destroy( p_aout, "audio-device" );
142     var_Destroy( p_aout, "audio-channels" );
143
144     aout_unlock( p_aout );
145
146     aout_MixerDelete (mixer);
147     free( p_input );
148 }
149
150
151 /*
152  * Buffer management
153  */
154
155 /*****************************************************************************
156  * aout_DecNewBuffer : ask for a new empty buffer
157  *****************************************************************************/
158 aout_buffer_t * aout_DecNewBuffer( aout_input_t * p_input,
159                                    size_t i_nb_samples )
160 {
161     size_t length = i_nb_samples * p_input->input.i_bytes_per_frame
162                                  / p_input->input.i_frame_length;
163     block_t *block = block_Alloc( length );
164     if( likely(block != NULL) )
165     {
166         block->i_nb_samples = i_nb_samples;
167         block->i_pts = block->i_length = 0;
168     }
169     return block;
170 }
171
172 /*****************************************************************************
173  * aout_DecDeleteBuffer : destroy an undecoded buffer
174  *****************************************************************************/
175 void aout_DecDeleteBuffer( audio_output_t * p_aout, aout_input_t * p_input,
176                            aout_buffer_t * p_buffer )
177 {
178     (void)p_aout; (void)p_input;
179     aout_BufferFree( p_buffer );
180 }
181
182 /*****************************************************************************
183  * aout_DecPlay : filter & mix the decoded buffer
184  *****************************************************************************/
185 int aout_DecPlay( audio_output_t * p_aout, aout_input_t * p_input,
186                   aout_buffer_t * p_buffer, int i_input_rate )
187 {
188     aout_owner_t *owner = aout_owner (p_aout);
189     assert( i_input_rate >= INPUT_RATE_DEFAULT / AOUT_MAX_INPUT_RATE &&
190             i_input_rate <= INPUT_RATE_DEFAULT * AOUT_MAX_INPUT_RATE );
191     assert( p_buffer->i_pts > 0 );
192
193     p_buffer->i_length = (mtime_t)p_buffer->i_nb_samples * 1000000
194                                 / p_input->input.i_rate;
195
196     aout_lock( p_aout );
197     if( p_input->b_error )
198     {
199         aout_unlock( p_aout );
200         aout_BufferFree( p_buffer );
201         return -1;
202     }
203
204     /* Input */
205     aout_InputCheckAndRestart( p_aout, p_input );
206     p_buffer = aout_InputPlay( p_aout, p_input, p_buffer, i_input_rate );
207
208     if( p_buffer != NULL )
209     {
210         /* Mixer */
211         float amp = owner->volume.multiplier * p_input->multiplier;
212         aout_MixerRun (owner->volume.mixer, p_buffer, amp);
213
214         /* Output */
215         aout_OutputPlay( p_aout, p_buffer );
216     }
217
218     aout_unlock( p_aout );
219     return 0;
220 }
221
222 int aout_DecGetResetLost( audio_output_t *p_aout, aout_input_t *p_input )
223 {
224     int val;
225
226     aout_lock( p_aout );
227     val = p_input->i_buffer_lost;
228     p_input->i_buffer_lost = 0;
229     aout_unlock( p_aout );
230
231     return val;
232 }
233
234 void aout_DecChangePause( audio_output_t *p_aout, aout_input_t *p_input, bool b_paused, mtime_t i_date )
235 {
236     aout_owner_t *owner = aout_owner (p_aout);
237
238     aout_lock( p_aout );
239     assert (owner->input == p_input);
240
241     /* XXX: Should the input date be offset by the pause duration instead? */
242     date_Set (&p_input->date, VLC_TS_INVALID);
243     aout_OutputPause( p_aout, b_paused, i_date );
244     aout_unlock( p_aout );
245 }
246
247 void aout_DecFlush( audio_output_t *p_aout, aout_input_t *p_input )
248 {
249     aout_lock( p_aout );
250     date_Set (&p_input->date, VLC_TS_INVALID);
251     aout_OutputFlush( p_aout, false );
252     aout_unlock( p_aout );
253 }
254
255 bool aout_DecIsEmpty( audio_output_t * p_aout, aout_input_t * p_input )
256 {
257     mtime_t end_date;
258
259     aout_lock( p_aout );
260     /* FIXME: tell output to drain */
261     end_date = date_Get (&p_input->date);
262     aout_unlock( p_aout );
263     return end_date == VLC_TS_INVALID || end_date <= mdate();
264 }