]> git.sesse.net Git - vlc/blob - src/audio_output/common.c
Major change of the channels management. p_format->i_channels disappeares
[vlc] / src / audio_output / common.c
1 /*****************************************************************************
2  * common.c : audio output management of common data structures
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: common.c,v 1.9 2002/11/14 22:38:48 massiot Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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 #include "audio_output.h"
33 #include "aout_internal.h"
34
35
36 /*
37  * Instances management (internal and external)
38  */
39
40 /*****************************************************************************
41  * aout_New: initialize aout structure
42  *****************************************************************************/
43 aout_instance_t * __aout_New( vlc_object_t * p_parent )
44 {
45     aout_instance_t * p_aout;
46
47     /* Allocate descriptor. */
48     p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
49     if( p_aout == NULL )
50     {
51         return NULL;
52     }
53
54     /* Initialize members. */
55     vlc_mutex_init( p_parent, &p_aout->input_fifos_lock );
56     vlc_mutex_init( p_parent, &p_aout->mixer_lock );
57     vlc_mutex_init( p_parent, &p_aout->output_fifo_lock );
58     p_aout->i_nb_inputs = 0;
59     p_aout->mixer.f_multiplier = 1.0;
60     p_aout->mixer.b_error = 1;
61     p_aout->output.b_starving = 1;
62
63     vlc_object_attach( p_aout, p_parent->p_vlc );
64
65     var_Create( p_aout, "physical-channels", VLC_VAR_INTEGER );
66     var_AddCallback( p_aout, "physical-channels", aout_ChannelsRestart,
67                      NULL );
68     var_Create( p_aout, "original-channels", VLC_VAR_INTEGER );
69     var_AddCallback( p_aout, "original-channels", aout_ChannelsRestart,
70                      NULL );
71
72     return p_aout;
73 }
74
75 /*****************************************************************************
76  * aout_Delete: destroy aout structure
77  *****************************************************************************/
78 void aout_Delete( aout_instance_t * p_aout )
79 {
80     var_Destroy( p_aout, "channels" );
81
82     vlc_mutex_destroy( &p_aout->input_fifos_lock );
83     vlc_mutex_destroy( &p_aout->mixer_lock );
84     vlc_mutex_destroy( &p_aout->output_fifo_lock );
85
86     /* Free structure. */
87     vlc_object_destroy( p_aout );
88 }
89
90
91 /*
92  * Formats management (internal and external)
93  */
94
95 /*****************************************************************************
96  * aout_FormatNbChannels : return the number of channels
97  *****************************************************************************/
98 int aout_FormatNbChannels( const audio_sample_format_t * p_format )
99 {
100     static const u32 pi_channels[] =
101         { AOUT_CHAN_CENTER, AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
102           AOUT_CHAN_REARCENTER, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
103           AOUT_CHAN_LFE };
104     int i_nb = 0, i;
105
106     for ( i = 0; i < sizeof(pi_channels)/sizeof(u32); i++ )
107     {
108         if ( p_format->i_physical_channels & pi_channels[i] ) i_nb++;
109     }
110
111     return i_nb;
112 }
113
114 /*****************************************************************************
115  * aout_FormatPrepare : compute the number of bytes per frame & frame length
116  *****************************************************************************/
117 void aout_FormatPrepare( audio_sample_format_t * p_format )
118 {
119     int i_result;
120
121     switch ( p_format->i_format )
122     {
123     case VLC_FOURCC('u','8',' ',' '):
124     case VLC_FOURCC('s','8',' ',' '):
125         i_result = 1;
126         break;
127
128     case VLC_FOURCC('u','1','6','l'):
129     case VLC_FOURCC('s','1','6','l'):
130     case VLC_FOURCC('u','1','6','b'):
131     case VLC_FOURCC('s','1','6','b'):
132         i_result = 2;
133         break;
134
135     case VLC_FOURCC('f','l','3','2'):
136     case VLC_FOURCC('f','i','3','2'):
137         i_result = 4;
138         break;
139
140     case VLC_FOURCC('s','p','d','i'):
141     case VLC_FOURCC('a','5','2',' '):
142     case VLC_FOURCC('d','t','s',' '):
143     default:
144         /* For these formats the caller has to indicate the parameters
145          * by hand. */
146         return;
147     }
148
149     p_format->i_bytes_per_frame = i_result * aout_FormatNbChannels( p_format );
150     p_format->i_frame_length = 1;
151 }
152
153 /*****************************************************************************
154  * aout_FormatPrintChannels : print a channel in a human-readable form
155  *****************************************************************************/
156 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
157 {
158     switch ( p_format->i_physical_channels & AOUT_CHAN_PHYSMASK )
159     {
160     case AOUT_CHAN_CENTER:
161         if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
162               || (p_format->i_original_channels
163                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
164             return "Mono";
165         else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
166             return "Left";
167         return "Right";
168     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
169         if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
170             return "Dolby";
171         else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
172             return "Dual-mono";
173         else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
174             return "Stereo/Left";
175         else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
176             return "Stereo/Right";
177         return "Stereo";
178     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
179         return "3F";
180     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
181         return "2F1R";
182     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
183           | AOUT_CHAN_REARCENTER:
184         return "3F1R";
185     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
186           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
187         return "2F2R";
188     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
189           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
190         return "3F2R";
191
192     case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
193         if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
194               || (p_format->i_original_channels
195                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
196             return "Mono/LFE";
197         else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
198             return "Left/LFE";
199         return "Right/LFE";
200     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
201         if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
202             return "Dolby/LFE";
203         else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
204             return "Dual-mono/LFE";
205         else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
206             return "Stereo/Left/LFE";
207         else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
208             return "Stereo/Right/LFE";
209          return "Stereo/LFE";
210     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
211         return "3F/LFE";
212     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
213           | AOUT_CHAN_LFE:
214         return "2F1R/LFE";
215     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
216           | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
217         return "3F1R/LFE";
218     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
219           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
220         return "2F2R/LFE";
221     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
222           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
223         return "3F2R/LFE";
224     }
225
226     return "ERROR";
227 }
228
229 /*****************************************************************************
230  * aout_FormatPrint : print a format in a human-readable form
231  *****************************************************************************/
232 void aout_FormatPrint( aout_instance_t * p_aout, const char * psz_text,
233                        const audio_sample_format_t * p_format )
234 {
235     msg_Dbg( p_aout, "%s format='%4.4s' rate=%d channels=%s", psz_text,
236              (char *)&p_format->i_format, p_format->i_rate,
237              aout_FormatPrintChannels( p_format ) );
238 }
239
240 /*****************************************************************************
241  * aout_FormatsPrint : print two formats in a human-readable form
242  *****************************************************************************/
243 void aout_FormatsPrint( aout_instance_t * p_aout, const char * psz_text,
244                         const audio_sample_format_t * p_format1,
245                         const audio_sample_format_t * p_format2 )
246 {
247     msg_Dbg( p_aout, "%s format='%4.4s'->'%4.4s' rate=%d->%d channels=%s->%s",
248              psz_text,
249              (char *)&p_format1->i_format, (char *)&p_format2->i_format,
250              p_format1->i_rate, p_format2->i_rate,
251              aout_FormatPrintChannels( p_format1 ),
252              aout_FormatPrintChannels( p_format2 ) );
253 }
254
255
256 /*
257  * FIFO management (internal) - please understand that solving race conditions
258  * is _your_ job, ie. in the audio output you should own the mixer lock
259  * before calling any of these functions.
260  */
261
262 /*****************************************************************************
263  * aout_FifoInit : initialize the members of a FIFO
264  *****************************************************************************/
265 void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
266                     u32 i_rate )
267 {
268     p_fifo->p_first = NULL;
269     p_fifo->pp_last = &p_fifo->p_first;
270     aout_DateInit( &p_fifo->end_date, i_rate );
271 }
272
273 /*****************************************************************************
274  * aout_FifoPush : push a packet into the FIFO
275  *****************************************************************************/
276 void aout_FifoPush( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
277                     aout_buffer_t * p_buffer )
278 {
279     *p_fifo->pp_last = p_buffer;
280     p_fifo->pp_last = &p_buffer->p_next;
281     *p_fifo->pp_last = NULL;
282     /* Enforce the continuity of the stream. */
283     if ( aout_DateGet( &p_fifo->end_date ) )
284     {
285         p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
286         p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
287                                                  p_buffer->i_nb_samples ); 
288     }
289     else
290     {
291         aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
292     }
293 }
294
295 /*****************************************************************************
296  * aout_FifoSet : set end_date and trash all buffers (because they aren't
297  * properly dated)
298  *****************************************************************************/
299 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
300                    mtime_t date )
301 {
302     aout_buffer_t * p_buffer;
303
304     aout_DateSet( &p_fifo->end_date, date );
305     p_buffer = p_fifo->p_first;
306     while ( p_buffer != NULL )
307     {
308         aout_buffer_t * p_next = p_buffer->p_next;
309         aout_BufferFree( p_buffer );
310         p_buffer = p_next;
311     }
312     p_fifo->p_first = NULL;
313     p_fifo->pp_last = &p_fifo->p_first;
314 }
315
316 /*****************************************************************************
317  * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
318  *****************************************************************************/
319 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
320                          mtime_t difference )
321 {
322     aout_buffer_t * p_buffer;
323
324     aout_DateMove( &p_fifo->end_date, difference );
325     p_buffer = p_fifo->p_first;
326     while ( p_buffer != NULL )
327     {
328         p_buffer->start_date += difference;
329         p_buffer->end_date += difference;
330         p_buffer = p_buffer->p_next;
331     }
332 }
333
334 /*****************************************************************************
335  * aout_FifoNextStart : return the current end_date
336  *****************************************************************************/
337 mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
338 {
339     return aout_DateGet( &p_fifo->end_date );
340 }
341
342 /*****************************************************************************
343  * aout_FifoFirstDate : return the playing date of the first buffer in the
344  * FIFO
345  *****************************************************************************/
346 mtime_t aout_FifoFirstDate( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
347 {
348     return p_fifo->p_first ?  p_fifo->p_first->start_date : 0;
349 }
350
351 /*****************************************************************************
352  * aout_FifoPop : get the next buffer out of the FIFO
353  *****************************************************************************/
354 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
355 {
356     aout_buffer_t * p_buffer;
357     p_buffer = p_fifo->p_first;
358     if ( p_buffer == NULL ) return NULL;
359     p_fifo->p_first = p_buffer->p_next;
360     if ( p_fifo->p_first == NULL )
361     {
362         p_fifo->pp_last = &p_fifo->p_first;
363     }
364
365     return p_buffer;
366 }
367
368 /*****************************************************************************
369  * aout_FifoDestroy : destroy a FIFO and its buffers
370  *****************************************************************************/
371 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
372 {
373     aout_buffer_t * p_buffer;
374
375     p_buffer = p_fifo->p_first;
376     while ( p_buffer != NULL )
377     {
378         aout_buffer_t * p_next = p_buffer->p_next;
379         aout_BufferFree( p_buffer );
380         p_buffer = p_next;
381     }
382 }
383
384
385 /*
386  * Date management (internal and external)
387  */
388
389 /*****************************************************************************
390  * aout_DateInit : set the divider of an audio_date_t
391  *****************************************************************************/
392 void aout_DateInit( audio_date_t * p_date, u32 i_divider )
393 {
394     p_date->date = 0;
395     p_date->i_divider = i_divider;
396     p_date->i_remainder = 0;
397 }
398
399 /*****************************************************************************
400  * aout_DateSet : set the date of an audio_date_t
401  *****************************************************************************/
402 void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
403 {
404     p_date->date = new_date;
405     p_date->i_remainder = 0;
406 }
407
408 /*****************************************************************************
409  * aout_DateMove : move forwards or backwards the date of an audio_date_t
410  *****************************************************************************/
411 void aout_DateMove( audio_date_t * p_date, mtime_t difference )
412 {
413     p_date->date += difference;
414 }
415
416 /*****************************************************************************
417  * aout_DateGet : get the date of an audio_date_t
418  *****************************************************************************/
419 mtime_t aout_DateGet( const audio_date_t * p_date )
420 {
421     return p_date->date;
422 }
423
424 /*****************************************************************************
425  * aout_DateIncrement : increment the date and return the result, taking
426  * into account rounding errors
427  *****************************************************************************/
428 mtime_t aout_DateIncrement( audio_date_t * p_date, u32 i_nb_samples )
429 {
430     mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
431     p_date->date += i_dividend / p_date->i_divider;
432     p_date->i_remainder += (int)(i_dividend % p_date->i_divider);
433     if ( p_date->i_remainder >= p_date->i_divider )
434     {
435         /* This is Bresenham algorithm. */
436         p_date->date++;
437         p_date->i_remainder -= p_date->i_divider;
438     }
439     return p_date->date;
440 }
441