]> git.sesse.net Git - vlc/blob - src/audio_output/common.c
* src/audio_output/common.c: moved message around so that it compiles.
[vlc] / src / audio_output / common.c
1 /*****************************************************************************
2  * common.c : audio output management of common data structures
3  *****************************************************************************
4  * Copyright (C) 2002-2005 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 #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     vlc_value_t val;
47
48     /* Allocate descriptor. */
49     p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
50     if( p_aout == NULL )
51     {
52         return NULL;
53     }
54
55     /* Initialize members. */
56     vlc_mutex_init( p_parent, &p_aout->input_fifos_lock );
57     vlc_mutex_init( p_parent, &p_aout->mixer_lock );
58     vlc_mutex_init( p_parent, &p_aout->output_fifo_lock );
59     p_aout->i_nb_inputs = 0;
60     p_aout->mixer.f_multiplier = 1.0;
61     p_aout->mixer.b_error = 1;
62     p_aout->output.b_error = 1;
63     p_aout->output.b_starving = 1;
64
65     var_Create( p_aout, "intf-change", VLC_VAR_BOOL );
66     val.b_bool = VLC_TRUE;
67     var_Set( p_aout, "intf-change", val );
68
69     return p_aout;
70 }
71
72 /*****************************************************************************
73  * aout_Delete: destroy aout structure
74  *****************************************************************************/
75 void aout_Delete( aout_instance_t * p_aout )
76 {
77     var_Destroy( p_aout, "intf-change" );
78
79     vlc_mutex_destroy( &p_aout->input_fifos_lock );
80     vlc_mutex_destroy( &p_aout->mixer_lock );
81     vlc_mutex_destroy( &p_aout->output_fifo_lock );
82
83     /* Free structure. */
84     vlc_object_destroy( p_aout );
85 }
86
87
88 /*
89  * Formats management (internal and external)
90  */
91
92 /*****************************************************************************
93  * aout_FormatNbChannels : return the number of channels
94  *****************************************************************************/
95 unsigned int aout_FormatNbChannels( const audio_sample_format_t * p_format )
96 {
97     static const uint32_t pi_channels[] =
98         { AOUT_CHAN_CENTER, AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
99           AOUT_CHAN_REARCENTER, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
100           AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, AOUT_CHAN_LFE };
101     unsigned int i_nb = 0, i;
102
103     for ( i = 0; i < sizeof(pi_channels)/sizeof(uint32_t); i++ )
104     {
105         if ( p_format->i_physical_channels & pi_channels[i] ) i_nb++;
106     }
107
108     return i_nb;
109 }
110
111 /*****************************************************************************
112  * aout_FormatPrepare : compute the number of bytes per frame & frame length
113  *****************************************************************************/
114 void aout_FormatPrepare( audio_sample_format_t * p_format )
115 {
116     int i_result;
117
118     switch ( p_format->i_format )
119     {
120     case VLC_FOURCC('u','8',' ',' '):
121     case VLC_FOURCC('s','8',' ',' '):
122         i_result = 1;
123         break;
124
125     case VLC_FOURCC('u','1','6','l'):
126     case VLC_FOURCC('s','1','6','l'):
127     case VLC_FOURCC('u','1','6','b'):
128     case VLC_FOURCC('s','1','6','b'):
129         i_result = 2;
130         break;
131
132     case VLC_FOURCC('u','2','4','l'):
133     case VLC_FOURCC('s','2','4','l'):
134     case VLC_FOURCC('u','2','4','b'):
135     case VLC_FOURCC('s','2','4','b'):
136         i_result = 3;
137         break;
138
139     case VLC_FOURCC('f','l','3','2'):
140     case VLC_FOURCC('f','i','3','2'):
141         i_result = 4;
142         break;
143
144     case VLC_FOURCC('s','p','d','i'):
145     case VLC_FOURCC('s','p','d','b'): /* Big endian spdif output */
146     case VLC_FOURCC('a','5','2',' '):
147     case VLC_FOURCC('d','t','s',' '):
148     case VLC_FOURCC('m','p','g','a'):
149     case VLC_FOURCC('m','p','g','3'):
150     default:
151         /* For these formats the caller has to indicate the parameters
152          * by hand. */
153         return;
154     }
155
156     p_format->i_bytes_per_frame = i_result * aout_FormatNbChannels( p_format );
157     p_format->i_frame_length = 1;
158 }
159
160 /*****************************************************************************
161  * aout_FormatPrintChannels : print a channel in a human-readable form
162  *****************************************************************************/
163 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
164 {
165     switch ( p_format->i_physical_channels & AOUT_CHAN_PHYSMASK )
166     {
167     case AOUT_CHAN_CENTER:
168         if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
169               || (p_format->i_original_channels
170                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
171             return "Mono";
172         else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
173             return "Left";
174         return "Right";
175     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
176         if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
177         {
178             if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
179                 return "Dolby/Reverse";
180             return "Stereo/Reverse";
181         }
182         else
183         {
184             if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
185                 return "Dolby";
186             else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
187                 return "Dual-mono";
188             else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
189                 return "Stereo/Mono";
190             else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
191                 return "Stereo/Left";
192             else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
193                 return "Stereo/Right";
194             return "Stereo";
195         }
196     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
197         return "3F";
198     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
199         return "2F1R";
200     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
201           | AOUT_CHAN_REARCENTER:
202         return "3F1R";
203     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
204           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
205         return "2F2R";
206     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
207           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
208         return "3F2R";
209
210     case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
211         if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
212               || (p_format->i_original_channels
213                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
214             return "Mono/LFE";
215         else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
216             return "Left/LFE";
217         return "Right/LFE";
218     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
219         if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
220             return "Dolby/LFE";
221         else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
222             return "Dual-mono/LFE";
223         else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
224             return "Mono/LFE";
225         else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
226             return "Stereo/Left/LFE";
227         else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
228             return "Stereo/Right/LFE";
229          return "Stereo/LFE";
230     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
231         return "3F/LFE";
232     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
233           | AOUT_CHAN_LFE:
234         return "2F1R/LFE";
235     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
236           | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
237         return "3F1R/LFE";
238     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
239           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
240         return "2F2R/LFE";
241     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
242           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
243         return "3F2R/LFE";
244     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
245           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
246           | AOUT_CHAN_MIDDLERIGHT:
247         return "3F2M2R";
248     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
249           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
250           | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
251         return "3F2M2R/LFE";
252     }
253
254     return "ERROR";
255 }
256
257 /*****************************************************************************
258  * aout_FormatPrint : print a format in a human-readable form
259  *****************************************************************************/
260 void aout_FormatPrint( aout_instance_t * p_aout, const char * psz_text,
261                        const audio_sample_format_t * p_format )
262 {
263     msg_Dbg( p_aout, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
264              (char *)&p_format->i_format, p_format->i_rate,
265              aout_FormatPrintChannels( p_format ),
266              p_format->i_frame_length, p_format->i_bytes_per_frame );
267 }
268
269 /*****************************************************************************
270  * aout_FormatsPrint : print two formats in a human-readable form
271  *****************************************************************************/
272 void aout_FormatsPrint( aout_instance_t * p_aout, const char * psz_text,
273                         const audio_sample_format_t * p_format1,
274                         const audio_sample_format_t * p_format2 )
275 {
276     msg_Dbg( p_aout, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
277              psz_text,
278              (char *)&p_format1->i_format, (char *)&p_format2->i_format,
279              p_format1->i_rate, p_format2->i_rate,
280              aout_FormatPrintChannels( p_format1 ),
281              aout_FormatPrintChannels( p_format2 ) );
282 }
283
284
285 /*
286  * FIFO management (internal) - please understand that solving race conditions
287  * is _your_ job, ie. in the audio output you should own the mixer lock
288  * before calling any of these functions.
289  */
290
291 /*****************************************************************************
292  * aout_FifoInit : initialize the members of a FIFO
293  *****************************************************************************/
294 void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
295                     uint32_t i_rate )
296 {
297     if( i_rate == 0 )
298     {
299         msg_Err( p_aout, "initialising fifo with zero divider" );
300     }
301
302     p_fifo->p_first = NULL;
303     p_fifo->pp_last = &p_fifo->p_first;
304     aout_DateInit( &p_fifo->end_date, i_rate );
305 }
306
307 /*****************************************************************************
308  * aout_FifoPush : push a packet into the FIFO
309  *****************************************************************************/
310 void aout_FifoPush( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
311                     aout_buffer_t * p_buffer )
312 {
313     *p_fifo->pp_last = p_buffer;
314     p_fifo->pp_last = &p_buffer->p_next;
315     *p_fifo->pp_last = NULL;
316     /* Enforce the continuity of the stream. */
317     if ( aout_DateGet( &p_fifo->end_date ) )
318     {
319         p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
320         p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
321                                                  p_buffer->i_nb_samples );
322     }
323     else
324     {
325         aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
326     }
327 }
328
329 /*****************************************************************************
330  * aout_FifoSet : set end_date and trash all buffers (because they aren't
331  * properly dated)
332  *****************************************************************************/
333 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
334                    mtime_t date )
335 {
336     aout_buffer_t * p_buffer;
337
338     aout_DateSet( &p_fifo->end_date, date );
339     p_buffer = p_fifo->p_first;
340     while ( p_buffer != NULL )
341     {
342         aout_buffer_t * p_next = p_buffer->p_next;
343         aout_BufferFree( p_buffer );
344         p_buffer = p_next;
345     }
346     p_fifo->p_first = NULL;
347     p_fifo->pp_last = &p_fifo->p_first;
348 }
349
350 /*****************************************************************************
351  * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
352  *****************************************************************************/
353 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
354                          mtime_t difference )
355 {
356     aout_buffer_t * p_buffer;
357
358     aout_DateMove( &p_fifo->end_date, difference );
359     p_buffer = p_fifo->p_first;
360     while ( p_buffer != NULL )
361     {
362         p_buffer->start_date += difference;
363         p_buffer->end_date += difference;
364         p_buffer = p_buffer->p_next;
365     }
366 }
367
368 /*****************************************************************************
369  * aout_FifoNextStart : return the current end_date
370  *****************************************************************************/
371 mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
372 {
373     return aout_DateGet( &p_fifo->end_date );
374 }
375
376 /*****************************************************************************
377  * aout_FifoFirstDate : return the playing date of the first buffer in the
378  * FIFO
379  *****************************************************************************/
380 mtime_t aout_FifoFirstDate( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
381 {
382     return p_fifo->p_first ?  p_fifo->p_first->start_date : 0;
383 }
384
385 /*****************************************************************************
386  * aout_FifoPop : get the next buffer out of the FIFO
387  *****************************************************************************/
388 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
389 {
390     aout_buffer_t * p_buffer;
391
392     p_buffer = p_fifo->p_first;
393     if ( p_buffer == NULL ) return NULL;
394     p_fifo->p_first = p_buffer->p_next;
395     if ( p_fifo->p_first == NULL )
396     {
397         p_fifo->pp_last = &p_fifo->p_first;
398     }
399
400     return p_buffer;
401 }
402
403 /*****************************************************************************
404  * aout_FifoDestroy : destroy a FIFO and its buffers
405  *****************************************************************************/
406 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
407 {
408     aout_buffer_t * p_buffer;
409
410     p_buffer = p_fifo->p_first;
411     while ( p_buffer != NULL )
412     {
413         aout_buffer_t * p_next = p_buffer->p_next;
414         aout_BufferFree( p_buffer );
415         p_buffer = p_next;
416     }
417
418     p_fifo->p_first = NULL;
419     p_fifo->pp_last = &p_fifo->p_first;
420 }
421
422
423 /*
424  * Date management (internal and external)
425  */
426
427 /*****************************************************************************
428  * aout_DateInit : set the divider of an audio_date_t
429  *****************************************************************************/
430 void aout_DateInit( audio_date_t * p_date, uint32_t i_divider )
431 {
432     p_date->date = 0;
433     p_date->i_divider = i_divider;
434     p_date->i_remainder = 0;
435 }
436
437 /*****************************************************************************
438  * aout_DateSet : set the date of an audio_date_t
439  *****************************************************************************/
440 void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
441 {
442     p_date->date = new_date;
443     p_date->i_remainder = 0;
444 }
445
446 /*****************************************************************************
447  * aout_DateMove : move forwards or backwards the date of an audio_date_t
448  *****************************************************************************/
449 void aout_DateMove( audio_date_t * p_date, mtime_t difference )
450 {
451     p_date->date += difference;
452 }
453
454 /*****************************************************************************
455  * aout_DateGet : get the date of an audio_date_t
456  *****************************************************************************/
457 mtime_t aout_DateGet( const audio_date_t * p_date )
458 {
459     return p_date->date;
460 }
461
462 /*****************************************************************************
463  * aout_DateIncrement : increment the date and return the result, taking
464  * into account rounding errors
465  *****************************************************************************/
466 mtime_t aout_DateIncrement( audio_date_t * p_date, uint32_t i_nb_samples )
467 {
468     mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
469     p_date->date += i_dividend / p_date->i_divider;
470     p_date->i_remainder += (int)(i_dividend % p_date->i_divider);
471     if ( p_date->i_remainder >= p_date->i_divider )
472     {
473         /* This is Bresenham algorithm. */
474         p_date->date++;
475         p_date->i_remainder -= p_date->i_divider;
476     }
477     return p_date->date;
478 }
479
480 /*****************************************************************************
481  * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
482  *****************************************************************************/
483 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
484                               const uint32_t *pi_chan_order_out,
485                               uint32_t i_channel_mask,
486                               int i_channels, int *pi_chan_table )
487 {
488     vlc_bool_t b_chan_reorder = VLC_FALSE;
489     int i, j, k, l;
490
491     if( i_channels > AOUT_CHAN_MAX ) return VLC_FALSE;
492
493     for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
494     {
495         if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
496
497         for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
498         {
499             if( i_channel_mask & pi_chan_order_out[k] ) l++;
500         }
501
502         pi_chan_table[j++] = l;
503     }
504
505     for( i = 0; i < i_channels; i++ )
506     {
507         if( pi_chan_table[i] != i ) b_chan_reorder = VLC_TRUE;
508     }
509
510     return b_chan_reorder;
511 }
512
513 /*****************************************************************************
514  * aout_ChannelReorder :
515  *****************************************************************************/
516 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
517                           int i_channels, const int *pi_chan_table,
518                           int i_bits_per_sample )
519 {
520     uint8_t p_tmp[AOUT_CHAN_MAX * 4];
521     int i, j;
522
523     if( i_bits_per_sample == 8 )
524     {
525         for( i = 0; i < i_buffer / i_channels; i++ )
526         {
527             for( j = 0; j < i_channels; j++ )
528             {
529                 p_tmp[pi_chan_table[j]] = p_buf[j];
530             }
531
532             memcpy( p_buf, p_tmp, i_channels );
533             p_buf += i_channels;
534         }
535     }
536     else if( i_bits_per_sample == 16 )
537     {
538         for( i = 0; i < i_buffer / i_channels / 2; i++ )
539         {
540             for( j = 0; j < i_channels; j++ )
541             {
542                 p_tmp[2 * pi_chan_table[j]]     = p_buf[2 * j];
543                 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
544             }
545
546             memcpy( p_buf, p_tmp, 2 * i_channels );
547             p_buf += 2 * i_channels;
548         }
549     }
550     else if( i_bits_per_sample == 24 )
551     {
552         for( i = 0; i < i_buffer / i_channels / 3; i++ )
553         {
554             for( j = 0; j < i_channels; j++ )
555             {
556                 p_tmp[3 * pi_chan_table[j]]     = p_buf[3 * j];
557                 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
558                 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
559             }
560
561             memcpy( p_buf, p_tmp, 3 * i_channels );
562             p_buf += 3 * i_channels;
563         }
564     }
565     else if( i_bits_per_sample == 32 )
566     {
567         for( i = 0; i < i_buffer / i_channels / 4; i++ )
568         {
569             for( j = 0; j < i_channels; j++ )
570             {
571                 p_tmp[4 * pi_chan_table[j]]     = p_buf[4 * j];
572                 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
573                 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
574                 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
575             }
576
577             memcpy( p_buf, p_tmp, 4 * i_channels );
578             p_buf += 4 * i_channels;
579         }
580     }
581 }