]> git.sesse.net Git - vlc/blob - src/audio_output/audio_output.c
* .cvsignore :
[vlc] / src / audio_output / audio_output.c
1 /******************************************************************************
2  * audio_output.c : audio output thread
3  * (c)1999 VideoLAN
4  ******************************************************************************/
5
6 /* TODO:
7  *
8  * - Passer un certain nombre de "fonctions" (genre add_samples) en macro ou
9  *   inline
10  * - Faire les optimisations dans les fonctions threads :
11  *   = Stocker les "petits calculs" dans des variables au lieu de les refaire
12  *     à chaque boucle
13  *   = Utiliser des tables pour les gros calculs
14  * - Faire une structure différente pour intf/adec fifo
15  * - Rajouter des vlc_cond_signal ?
16  *
17  */
18
19 /******************************************************************************
20  * Preamble
21  ******************************************************************************/
22 #include <unistd.h>
23
24 #include <sys/soundcard.h>
25 #include <stdio.h>                                            /* "intf_msg.h" */
26 #include <stdlib.h>                             /* calloc(), malloc(), free() */
27
28 #include "common.h"
29 #include "config.h"
30 #include "mtime.h"                              /* mtime_t, mdate(), msleep() */
31 #include "vlc_thread.h"
32
33 #include "intf_msg.h"                         /* intf_DbgMsg(), intf_ErrMsg() */
34
35 #include "audio_output.h"
36 #include "audio_dsp.h"
37
38 /******************************************************************************
39  * Local prototypes
40  ******************************************************************************/
41
42 /* Creating as much aout_Thread functions as configurations is one solution,
43  * examining the different cases in the Thread loop of an unique function is
44  * another. I chose the first solution. */
45 void aout_Thread_S8_Mono        ( aout_thread_t * p_aout );
46 void aout_Thread_U8_Mono        ( aout_thread_t * p_aout );
47 void aout_Thread_S16_Mono       ( aout_thread_t * p_aout );
48 void aout_Thread_U16_Mono       ( aout_thread_t * p_aout );
49 void aout_Thread_S8_Stereo      ( aout_thread_t * p_aout );
50 void aout_Thread_U8_Stereo      ( aout_thread_t * p_aout );
51 void aout_Thread_S16_Stereo     ( aout_thread_t * p_aout );
52 void aout_Thread_U16_Stereo     ( aout_thread_t * p_aout );
53
54 static __inline__ void InitializeIncrement( aout_increment_t * p_increment, long l_numerator, long l_denominator );
55 static __inline__ int NextFrame( aout_thread_t * p_aout, aout_fifo_t * p_fifo/*, mtime_t aout_date*/ );
56
57 /******************************************************************************
58  * aout_Open
59  ******************************************************************************/
60 int aout_Open( aout_thread_t * p_aout )
61 {
62     if ( aout_dspOpen( &p_aout->dsp ) )
63     {
64         return( -1 );
65     }
66
67     if ( aout_dspReset( &p_aout->dsp ) )
68     {
69         aout_dspClose( &p_aout->dsp );
70         return( -1 );
71     }
72
73     if ( aout_dspSetFormat( &p_aout->dsp ) )
74     {
75         aout_dspClose( &p_aout->dsp );
76         return( -1 );
77     }
78
79     if ( aout_dspSetChannels( &p_aout->dsp ) )
80     {
81         aout_dspClose( &p_aout->dsp );
82         return( -1 );
83     }
84
85     if ( aout_dspSetRate( &p_aout->dsp ) )
86     {
87         aout_dspClose( &p_aout->dsp );
88         return( -1 );
89     }
90
91     intf_DbgMsg("aout debug: audio device (%s) opened (format=%i, stereo=%i, rate=%li)\n",
92         p_aout->dsp.psz_device,
93         p_aout->dsp.i_format,
94         p_aout->dsp.b_stereo, p_aout->dsp.l_rate);
95
96     return( 0 );
97 }
98
99 /******************************************************************************
100  * aout_SpawnThread
101  ******************************************************************************/
102 int aout_SpawnThread( aout_thread_t * p_aout )
103 {
104     int         i_fifo;
105     long        l_bytes;
106     s64         s64_numerator, s64_denominator;
107     void *      aout_thread = NULL;
108
109     intf_DbgMsg("aout debug: spawning audio output thread (%p)\n", p_aout);
110
111     /* We want the audio output thread to live */
112     p_aout->b_die = 0;
113
114     /* Initialize the fifos lock */
115     vlc_mutex_init( &p_aout->fifos_lock );
116     /* Initialize audio fifos : set all fifos as empty and initialize locks */
117     for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
118     {
119         p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
120         vlc_mutex_init( &p_aout->fifo[i_fifo].data_lock );
121         vlc_cond_init( &p_aout->fifo[i_fifo].data_wait );
122     }
123
124     /* Compute the size (in audio units) of the audio output buffer. Although
125      * AOUT_BUFFER_DURATION is given in microseconds, the output rate is given
126      * in Hz, that's why we need to divide by 10^6 microseconds (1 second) */
127     p_aout->l_units = (long)( ((s64)p_aout->dsp.l_rate * AOUT_BUFFER_DURATION) / 1000000 );
128
129     /* Make aout_thread point to the right thread function, and compute the
130      * byte size of the audio output buffer */
131     switch ( p_aout->dsp.b_stereo )
132     {
133         /* Audio output is mono */
134         case 0:
135             switch ( p_aout->dsp.i_format )
136             {
137                 case AFMT_U8:
138                     l_bytes = 1 * sizeof(u8) * p_aout->l_units;
139                     aout_thread = (void *)aout_Thread_U8_Mono;
140                     break;
141
142                 case AFMT_S8:
143                     l_bytes = 1 * sizeof(s8) * p_aout->l_units;
144                     aout_thread = (void *)aout_Thread_S8_Mono;
145                     break;
146
147                 case AFMT_U16_LE:
148                 case AFMT_U16_BE:
149                     l_bytes = 1 * sizeof(u16) * p_aout->l_units;
150                     aout_thread = (void *)aout_Thread_U16_Mono;
151                     break;
152
153                 case AFMT_S16_LE:
154                 case AFMT_S16_BE:
155                     l_bytes = 1 * sizeof(s16) * p_aout->l_units;
156                     aout_thread = (void *)aout_Thread_S16_Mono;
157                     break;
158
159                 default:
160                     intf_ErrMsg("aout error: unknown audio output format (%i)\n",
161                         p_aout->dsp.i_format);
162                     return( -1 );
163             }
164             break;
165
166         /* Audio output is stereo */
167         case 1:
168             switch ( p_aout->dsp.i_format )
169             {
170                 case AFMT_U8:
171                     l_bytes = 2 * sizeof(u8) * p_aout->l_units;
172                     aout_thread = (void *)aout_Thread_U8_Stereo;
173                     break;
174
175                 case AFMT_S8:
176                     l_bytes = 2 * sizeof(s8) * p_aout->l_units;
177                     aout_thread = (void *)aout_Thread_S8_Stereo;
178                     break;
179
180                 case AFMT_U16_LE:
181                 case AFMT_U16_BE:
182                     l_bytes = 2 * sizeof(u16) * p_aout->l_units;
183                     aout_thread = (void *)aout_Thread_U16_Stereo;
184                     break;
185
186                 case AFMT_S16_LE:
187                 case AFMT_S16_BE:
188                     l_bytes = 2 * sizeof(s16) * p_aout->l_units;
189                     aout_thread = (void *)aout_Thread_S16_Stereo;
190                     break;
191
192                 default:
193                     intf_ErrMsg("aout error: unknown audio output format (%i)\n",
194                         p_aout->dsp.i_format);
195                     return( -1 );
196             }
197             break;
198
199         default:
200             intf_ErrMsg("aout error: unknown number of audio channels (%i)\n",
201                 p_aout->dsp.b_stereo + 1);
202             return( -1 );
203     }
204
205     /* Allocate the memory needed by the audio output buffers, and set to zero
206      * the s32 buffer's memory */
207     if ( (p_aout->buffer = malloc(l_bytes)) == NULL )
208     {
209         intf_ErrMsg("aout error: not enough memory to create the output buffer\n");
210         return( -1 );
211     }
212     if ( (p_aout->s32_buffer = (s32 *)calloc(p_aout->l_units, sizeof(s32) << p_aout->dsp.b_stereo)) == NULL )
213     {
214         intf_ErrMsg("aout error: not enough memory to create the s32 output buffer\n");
215         free( p_aout->buffer );
216         return( -1 );
217     }
218
219     /* Initialize the incremental structure that is used to work out the date
220      * of the first audio unit in the output buffer */
221     s64_numerator = (s64)p_aout->l_units * 1000000;
222     s64_denominator = (s64)p_aout->dsp.l_rate;
223
224     p_aout->date_increment.l_remainder = -(long)s64_denominator;
225
226     p_aout->date_increment.l_euclidean_integer = 0;
227     while ( s64_numerator >= s64_denominator )
228     {
229         p_aout->date_increment.l_euclidean_integer++;
230         s64_numerator -= s64_denominator;
231     }
232
233     p_aout->date_increment.l_euclidean_remainder = (long)s64_numerator;
234
235     p_aout->date_increment.l_euclidean_denominator = (long)s64_denominator;
236
237     /* Before launching the thread, we try to predict the date of the first
238      * audio unit in the first output buffer */
239     p_aout->date = mdate();
240
241     /* Launch the thread */
242     if ( vlc_thread_create( &p_aout->thread_id, "audio output", (vlc_thread_func)aout_thread, p_aout ) )
243     {
244         intf_ErrMsg("aout error: can't spawn audio output thread (%p)\n", p_aout);
245         free( p_aout->buffer );
246         free( p_aout->s32_buffer );
247         return( -1 );
248     }
249
250     intf_DbgMsg("aout debug: audio output thread (%p) spawned\n", p_aout);
251     return( 0 );
252 }
253
254 /******************************************************************************
255  * aout_CancelThread
256  ******************************************************************************/
257 void aout_CancelThread( aout_thread_t * p_aout )
258 {
259     intf_DbgMsg("aout debug: requesting termination of audio output thread (%p)\n", p_aout);
260
261     /* Ask thread to kill itself and wait until it's done */
262     p_aout->b_die = 1;
263     vlc_thread_join( p_aout->thread_id );
264
265     /* Free the allocated memory */
266     free( p_aout->buffer );
267     free( p_aout->s32_buffer );
268 }
269
270 /******************************************************************************
271  * aout_Close
272  ******************************************************************************/
273 void aout_Close( aout_thread_t * p_aout )
274 {
275     aout_dspClose( &p_aout->dsp );
276     intf_DbgMsg("aout debug: audio device (%s) closed\n", p_aout->dsp.psz_device);
277 }
278
279 /******************************************************************************
280  * aout_CreateFifo
281  ******************************************************************************/
282 aout_fifo_t * aout_CreateFifo( aout_thread_t * p_aout, aout_fifo_t * p_fifo )
283 {
284     int i_fifo;
285
286     /* Take the fifos lock */
287     vlc_mutex_lock( &p_aout->fifos_lock );
288
289     /* Looking for a free fifo structure */
290     for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
291     {
292         if ( p_aout->fifo[i_fifo].i_type == AOUT_EMPTY_FIFO)
293         {
294             break;
295         }
296     }
297     if ( i_fifo == AOUT_MAX_FIFOS )
298     {
299         intf_ErrMsg("aout error: no empty fifo available\n");
300         vlc_mutex_unlock( &p_aout->fifos_lock );
301         return( NULL );
302     }
303
304     /* Initialize the new fifo structure */
305     switch ( p_aout->fifo[i_fifo].i_type = p_fifo->i_type )
306     {
307         case AOUT_INTF_MONO_FIFO:
308         case AOUT_INTF_STEREO_FIFO:
309             p_aout->fifo[i_fifo].b_die = 0;
310
311             p_aout->fifo[i_fifo].b_stereo = p_fifo->b_stereo;
312             p_aout->fifo[i_fifo].l_rate = p_fifo->l_rate;
313
314             p_aout->fifo[i_fifo].buffer = p_fifo->buffer;
315
316             p_aout->fifo[i_fifo].l_unit = 0;
317             InitializeIncrement( &p_aout->fifo[i_fifo].unit_increment, p_fifo->l_rate, p_aout->dsp.l_rate );
318             p_aout->fifo[i_fifo].l_units = p_fifo->l_units;
319             break;
320
321         case AOUT_ADEC_MONO_FIFO:
322         case AOUT_ADEC_STEREO_FIFO:
323             p_aout->b_die = 0;
324
325             p_aout->fifo[i_fifo].b_stereo = p_fifo->b_stereo;
326             p_aout->fifo[i_fifo].l_rate = p_fifo->l_rate;
327
328             /* Allocate the memory needed to store the audio frames. As the
329              * fifo is a rotative fifo, we must be able to find out whether the
330              * fifo is full or empty, that's why we must in fact allocate memory
331              * for (AOUT_FIFO_SIZE+1) audio frames. */
332             if ( (p_aout->fifo[i_fifo].buffer = malloc( sizeof(s16)*(AOUT_FIFO_SIZE+1)*AOUT_FRAME_SIZE )) == NULL )
333             {
334                 intf_ErrMsg("aout error: not enough memory to create the frames buffer\n");
335                 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
336                 vlc_mutex_unlock( &p_aout->fifos_lock );
337                 return( NULL );
338             }
339
340             /* Allocate the memory needed to store the dates of the frames */
341             if ( (p_aout->fifo[i_fifo].date = (mtime_t *)malloc( sizeof(mtime_t)*(AOUT_FIFO_SIZE+1) )) == NULL )
342             {
343                 intf_ErrMsg("aout error: not enough memory to create the dates buffer\n");
344                 free( p_aout->fifo[i_fifo].buffer );
345                 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
346                 vlc_mutex_unlock( &p_aout->fifos_lock );
347                 return( NULL );
348             }
349
350             /* Set the fifo's buffer as empty (the first frame that is to be
351              * played is also the first frame that is not to be played) */
352             p_aout->fifo[i_fifo].l_start_frame = 0;
353             /* p_aout->fifo[i_fifo].l_next_frame = 0; */
354             p_aout->fifo[i_fifo].l_end_frame = 0;
355
356             /* Waiting for the audio decoder to compute enough frames to work
357              * out the fifo's current rate (as soon as the decoder has decoded
358              * enough frames, the members of the fifo structure that are not
359              * initialized now will be calculated) */
360             p_aout->fifo[i_fifo].l_unit = 0; /* !! */
361             p_aout->fifo[i_fifo].b_start_frame = 0;
362             p_aout->fifo[i_fifo].b_next_frame = 0;
363             break;
364
365         default:
366             intf_ErrMsg("aout error: unknown fifo type (%i)\n", p_aout->fifo[i_fifo].i_type);
367             p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
368             vlc_mutex_unlock( &p_aout->fifos_lock );
369             return( NULL );
370     }
371
372     /* Release the fifos lock */
373     vlc_mutex_unlock( &p_aout->fifos_lock );
374
375     /* Return the pointer to the fifo structure */
376     intf_DbgMsg("aout debug: audio output fifo (%p) allocated\n", &p_aout->fifo[i_fifo]);
377     return( &p_aout->fifo[i_fifo] );
378 }
379
380 /******************************************************************************
381  * aout_DestroyFifo
382  ******************************************************************************/
383 void aout_DestroyFifo( aout_fifo_t * p_fifo )
384 {
385     intf_DbgMsg("aout debug: requesting destruction of audio output fifo (%p)\n", p_fifo);
386     p_fifo->b_die = 1;
387 }
388
389 /* Here are the local macros */
390
391 #define S32_TO_S16( sample ) \
392     (s16)( (sample) )
393
394 #define UPDATE_INCREMENT( increment, integer ) \
395     if ( ((increment).l_remainder += (increment).l_euclidean_remainder) >= 0 ) \
396     { \
397         (integer) += (increment).l_euclidean_integer + 1; \
398         (increment).l_remainder -= (increment).l_euclidean_denominator; \
399     } \
400     else \
401     { \
402         (integer) += (increment).l_euclidean_integer; \
403     }
404
405 /* Following functions are local */
406
407 /******************************************************************************
408  * InitializeIncrement
409  ******************************************************************************/
410 static __inline__ void InitializeIncrement( aout_increment_t * p_increment, long l_numerator, long l_denominator )
411 {
412     p_increment->l_remainder = -l_denominator;
413
414     p_increment->l_euclidean_integer = 0;
415     while ( l_numerator >= l_denominator )
416     {
417         p_increment->l_euclidean_integer++;
418         l_numerator -= l_denominator;
419     }
420
421     p_increment->l_euclidean_remainder = l_numerator;
422
423     p_increment->l_euclidean_denominator = l_denominator;
424 }
425
426 /******************************************************************************
427  * NextFrame
428  ******************************************************************************/
429 static __inline__ int NextFrame( aout_thread_t * p_aout, aout_fifo_t * p_fifo/*, mtime_t aout_date*/ )
430 {
431     long l_units, l_rate;
432
433     /* We take the lock */
434     vlc_mutex_lock( &p_fifo->data_lock );
435
436     /* Are we looking for a dated start frame ? */
437     if ( !p_fifo->b_start_frame )
438     {
439         while ( p_fifo->l_start_frame != p_fifo->l_end_frame )
440         {
441             if ( p_fifo->date[p_fifo->l_start_frame] != LAST_MDATE )
442             {
443                 p_fifo->b_start_frame = 1;
444                 p_fifo->l_next_frame = (p_fifo->l_start_frame + 1) & AOUT_FIFO_SIZE;
445                 break;
446             }
447             p_fifo->l_start_frame = (p_fifo->l_start_frame + 1) & AOUT_FIFO_SIZE;
448         }
449         if ( p_fifo->l_start_frame == p_fifo->l_end_frame )
450         {
451             vlc_mutex_unlock( &p_fifo->data_lock );
452             return( -1 );
453         }
454     }
455 /*
456     if ( aout_date < p_fifo->date[p_fifo->l_start_frame] )
457     {
458         fprintf(stderr, "+");
459         vlc_mutex_unlock( &p_fifo->data_lock );
460         return( -1 );
461     }
462 */
463     /* We are looking for the next dated frame */
464     while ( p_fifo->l_next_frame != p_fifo->l_end_frame )
465     {
466         if ( p_fifo->date[p_fifo->l_next_frame] != LAST_MDATE )
467         {
468 /*
469             if ( aout_date < p_fifo->date[p_fifo->l_next_frame] )
470             {
471 */
472             if ( p_fifo->date[p_fifo->l_next_frame] - p_fifo->date[p_fifo->l_start_frame] > 1000000 )
473             {
474                 p_fifo->date[p_fifo->l_start_frame] = p_fifo->date[p_fifo->l_next_frame] - ((1000000 * AOUT_FRAME_SIZE * ((mtime_t)((p_fifo->l_next_frame - p_fifo->l_start_frame) & AOUT_FIFO_SIZE)) >> p_fifo->b_stereo) / ((mtime_t)p_fifo->l_rate));
475             }
476             p_fifo->b_next_frame = 1;
477             break;
478 /*
479             }
480             else
481             {
482                 fprintf(stderr, "-");
483                 p_fifo->l_start_frame = p_fifo->l_next_frame;
484             }
485 */
486         }
487         p_fifo->l_next_frame = (p_fifo->l_next_frame + 1) & AOUT_FIFO_SIZE;
488     }
489     if ( p_fifo->l_next_frame == p_fifo->l_end_frame )
490     {
491         if ( (((p_fifo->l_end_frame + 1) - p_fifo->l_start_frame) & AOUT_FIFO_SIZE) == 0 )
492         {
493             p_fifo->l_start_frame = 0;
494             p_fifo->b_start_frame = 0;
495             /* p_fifo->l_next_frame = 0; */
496             /* p_fifo->b_next_frame = 0; */
497             p_fifo->l_end_frame = 0;
498         }
499         vlc_mutex_unlock( &p_fifo->data_lock );
500         return( -1 );
501     }
502
503     l_units = ((p_fifo->l_next_frame - p_fifo->l_start_frame) & AOUT_FIFO_SIZE)
504         * (AOUT_FRAME_SIZE >> p_fifo->b_stereo);
505
506     l_rate = (long)( ((mtime_t)l_units * 1000000)
507         / (p_fifo->date[p_fifo->l_next_frame] - p_fifo->date[p_fifo->l_start_frame]) );
508
509     intf_DbgMsg( "aout debug: %li frame(s), %lli µs, %li Hz\n",
510         (p_fifo->l_next_frame - p_fifo->l_start_frame) & AOUT_FIFO_SIZE,
511         p_fifo->date[p_fifo->l_next_frame] - p_fifo->date[p_fifo->l_start_frame],
512         l_rate );
513
514     InitializeIncrement( &p_fifo->unit_increment, l_rate, p_aout->dsp.l_rate );
515
516     p_fifo->l_units = (((l_units - (p_fifo->l_unit -
517         (p_fifo->l_start_frame * (AOUT_FRAME_SIZE >> p_fifo->b_stereo))))
518         * p_aout->dsp.l_rate) / l_rate) + 1;
519
520     /* We release the lock before leaving */
521     vlc_mutex_unlock( &p_fifo->data_lock );
522     return( 0 );
523 }
524
525 void aout_Thread_S8_Mono( aout_thread_t * p_aout )
526 {
527 }
528
529 void aout_Thread_S8_Stereo( aout_thread_t * p_aout )
530 {
531 }
532
533 void aout_Thread_U8_Mono( aout_thread_t * p_aout )
534 {
535 }
536
537 void aout_Thread_U8_Stereo( aout_thread_t * p_aout )
538 {
539 }
540
541 void aout_Thread_S16_Mono( aout_thread_t * p_aout )
542 {
543 }
544
545 void aout_Thread_S16_Stereo( aout_thread_t * p_aout )
546 {
547     int i_fifo;
548     long l_units;
549     long l_buffer, l_buffer_limit;
550
551     intf_DbgMsg("adec debug: running audio output thread (%p) (pid == %i)\n", p_aout, getpid());
552
553     /* As the s32_buffer was created with calloc(), we don't have to set this
554      * memory to zero and we can immediately jump into the thread's loop */
555     while ( !p_aout->b_die )
556     {
557         vlc_mutex_lock( &p_aout->fifos_lock );
558         for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
559         {
560             switch ( p_aout->fifo[i_fifo].i_type )
561             {
562                 case AOUT_EMPTY_FIFO:
563                     break;
564
565                 case AOUT_INTF_MONO_FIFO:
566                     if ( p_aout->fifo[i_fifo].l_units > p_aout->l_units )
567                     {
568                         l_buffer = 0;
569                         while ( l_buffer < (p_aout->l_units << 1) ) /* p_aout->dsp.b_stereo == 1 */
570                         {
571                             p_aout->s32_buffer[l_buffer++] +=
572                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
573                             p_aout->s32_buffer[l_buffer++] +=
574                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
575                             UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
576                         }
577                         p_aout->fifo[i_fifo].l_units -= p_aout->l_units;
578                     }
579                     else
580                     {
581                         l_buffer = 0;
582                         while ( l_buffer < (p_aout->fifo[i_fifo].l_units << 1) ) /* p_aout->dsp.b_stereo == 1 */
583                         {
584                             p_aout->s32_buffer[l_buffer++] +=
585                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
586                             p_aout->s32_buffer[l_buffer++] +=
587                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
588                             UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
589                         }
590                         free( p_aout->fifo[i_fifo].buffer ); /* !! */
591                         p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
592                         intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]); /* !! */
593                     }
594                     break;
595
596                 case AOUT_INTF_STEREO_FIFO:
597                     if ( p_aout->fifo[i_fifo].l_units > p_aout->l_units )
598                     {
599                         l_buffer = 0;
600                         while ( l_buffer < (p_aout->l_units << 1) ) /* p_aout->dsp.b_stereo == 1 */
601                         {
602                             p_aout->s32_buffer[l_buffer++] +=
603                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
604                             p_aout->s32_buffer[l_buffer++] +=
605                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
606                             UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
607                         }
608                         p_aout->fifo[i_fifo].l_units -= p_aout->l_units;
609                     }
610                     else
611                     {
612                         l_buffer = 0;
613                         while ( l_buffer < (p_aout->fifo[i_fifo].l_units << 1) ) /* p_aout->dsp.b_stereo */
614                         {
615                             p_aout->s32_buffer[l_buffer++] +=
616                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
617                             p_aout->s32_buffer[l_buffer++] +=
618                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
619                             UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
620                         }
621                         free( p_aout->fifo[i_fifo].buffer ); /* !! */
622                         p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
623                         intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]); /* !! */
624                     }
625                     break;
626
627                 case AOUT_ADEC_MONO_FIFO:
628                     l_units = p_aout->l_units;
629                     l_buffer = 0;
630                     while ( l_units > 0 )
631                     {
632                         if ( !p_aout->fifo[i_fifo].b_next_frame )
633                         {
634                             if ( NextFrame(p_aout, &p_aout->fifo[i_fifo]/*, p_aout->date + (mtime_t)((l_buffer >> p_aout->dsp.b_stereo) / p_aout->dsp.l_rate)*/) )
635                             {
636                                 break;
637                             }
638                         }
639
640                         if ( p_aout->fifo[i_fifo].l_units > l_units )
641                         {
642                             l_buffer_limit = p_aout->l_units << 1; /* p_aout->dsp.b_stereo == 1 */
643                             while ( l_buffer < l_buffer_limit )
644                             {
645                                 p_aout->s32_buffer[l_buffer++] +=
646                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
647                                 p_aout->s32_buffer[l_buffer++] +=
648                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
649
650                                 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
651                                 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 0 */
652                                      ((AOUT_FIFO_SIZE + 1) * (AOUT_FRAME_SIZE >> 0)) )
653                                 {
654                                     p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 0 */
655                                         ((AOUT_FIFO_SIZE + 1) * (AOUT_FRAME_SIZE >> 0));
656                                 }
657                             }
658                             p_aout->fifo[i_fifo].l_units -= l_units;
659                             break;
660                         }
661                         else
662                         {
663                             l_buffer_limit = l_buffer + (p_aout->fifo[i_fifo].l_units << 1);
664                             /* p_aout->dsp.b_stereo == 1 */
665                             while ( l_buffer < l_buffer_limit )
666                             {
667                                 p_aout->s32_buffer[l_buffer++] +=
668                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
669                                 p_aout->s32_buffer[l_buffer++] +=
670                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
671
672                                 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
673                                 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 0 */
674                                      ((AOUT_FIFO_SIZE + 1) * (AOUT_FRAME_SIZE >> 0)) )
675                                 {
676                                     p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 0 */
677                                         ((AOUT_FIFO_SIZE + 1) * (AOUT_FRAME_SIZE >> 0));
678                                 }
679                             }
680                             l_units -= p_aout->fifo[i_fifo].l_units;
681
682                             vlc_mutex_lock( &p_aout->fifo[i_fifo].data_lock );
683                             p_aout->fifo[i_fifo].l_start_frame = p_aout->fifo[i_fifo].l_next_frame;
684                             vlc_cond_signal( &p_aout->fifo[i_fifo].data_wait );
685                             vlc_mutex_unlock( &p_aout->fifo[i_fifo].data_lock );
686
687                             /* p_aout->fifo[i_fifo].b_start_frame = 1; */
688                             p_aout->fifo[i_fifo].l_next_frame += 1;
689                             p_aout->fifo[i_fifo].l_next_frame &= AOUT_FIFO_SIZE;
690                             p_aout->fifo[i_fifo].b_next_frame = 0;
691                         }
692                     }
693                     break;
694
695                 case AOUT_ADEC_STEREO_FIFO:
696                     l_units = p_aout->l_units;
697                     l_buffer = 0;
698                     while ( l_units > 0 )
699                     {
700                         if ( !p_aout->fifo[i_fifo].b_next_frame )
701                         {
702                             if ( NextFrame(p_aout, &p_aout->fifo[i_fifo]/*, p_aout->date + (mtime_t)((l_buffer >> p_aout->dsp.b_stereo) / p_aout->dsp.l_rate)*/) )
703                             {
704                                 break;
705                             }
706                         }
707
708                         if ( p_aout->fifo[i_fifo].l_units > l_units )
709                         {
710                             l_buffer_limit = p_aout->l_units << 1; /* p_aout->dsp.b_stereo == 1 */
711                             while ( l_buffer < l_buffer_limit )
712                             {
713                                 p_aout->s32_buffer[l_buffer++] +=
714                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
715                                 p_aout->s32_buffer[l_buffer++] +=
716                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
717
718                                 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
719                                 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 1 */
720                                      ((AOUT_FIFO_SIZE + 1) * (AOUT_FRAME_SIZE >> 1)) )
721                                 {
722                                     p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 1 */
723                                         ((AOUT_FIFO_SIZE + 1) * (AOUT_FRAME_SIZE >> 1));
724                                 }
725                             }
726                             p_aout->fifo[i_fifo].l_units -= l_units;
727                             break;
728                         }
729                         else
730                         {
731                             l_buffer_limit = l_buffer + (p_aout->fifo[i_fifo].l_units << 1);
732                             /* p_aout->dsp.b_stereo == 1 */
733                             while ( l_buffer < l_buffer_limit )
734                             {
735                                 p_aout->s32_buffer[l_buffer++] +=
736                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
737                                 p_aout->s32_buffer[l_buffer++] +=
738                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
739
740                                 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
741                                 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 1 */
742                                      ((AOUT_FIFO_SIZE + 1) * (AOUT_FRAME_SIZE >> 1)) )
743                                 {
744                                     p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 1 */
745                                         ((AOUT_FIFO_SIZE + 1) * (AOUT_FRAME_SIZE >> 1));
746                                 }
747                             }
748                             l_units -= p_aout->fifo[i_fifo].l_units;
749
750                             vlc_mutex_lock( &p_aout->fifo[i_fifo].data_lock );
751                             p_aout->fifo[i_fifo].l_start_frame = p_aout->fifo[i_fifo].l_next_frame;
752                             vlc_cond_signal( &p_aout->fifo[i_fifo].data_wait );
753                             vlc_mutex_unlock( &p_aout->fifo[i_fifo].data_lock );
754
755                             /* p_aout->fifo[i_fifo].b_start_frame = 1; */
756                             p_aout->fifo[i_fifo].l_next_frame += 1;
757                             p_aout->fifo[i_fifo].l_next_frame &= AOUT_FIFO_SIZE;
758                             p_aout->fifo[i_fifo].b_next_frame = 0;
759                         }
760                     }
761                     break;
762
763                 default:
764                     intf_DbgMsg("aout debug: unknown fifo type (%i)\n", p_aout->fifo[i_fifo].i_type);
765                     break;
766             }
767         }
768         vlc_mutex_unlock( &p_aout->fifos_lock );
769
770         l_buffer_limit = p_aout->l_units << 1; /* p_aout->dsp.b_stereo == 1 */
771
772         for ( l_buffer = 0; l_buffer < l_buffer_limit; l_buffer++ )
773         {
774             ((s16 *)p_aout->buffer)[l_buffer] = S32_TO_S16( p_aout->s32_buffer[l_buffer] / AOUT_MAX_FIFOS );
775             p_aout->s32_buffer[l_buffer] = 0;
776         }
777
778         aout_dspGetBufInfo( &p_aout->dsp );
779         if ( p_aout->dsp.buf_info.fragments == p_aout->dsp.buf_info.fragstotal ) /* ?? */
780         {
781             aout_dspPlaySamples( &p_aout->dsp, (byte_t *)p_aout->buffer, sizeof(s16) * l_buffer_limit );
782             p_aout->date = mdate();
783         }
784         else if ( p_aout->dsp.buf_info.bytes >=
785             ((p_aout->dsp.buf_info.fragsize * p_aout->dsp.buf_info.fragstotal) -
786             (sizeof(s16) * l_buffer_limit)) )
787         {
788             aout_dspPlaySamples( &p_aout->dsp, (byte_t *)p_aout->buffer, sizeof(s16) * l_buffer_limit );
789         }
790         else
791         {
792             aout_dspPlaySamples( &p_aout->dsp, (byte_t *)p_aout->buffer, sizeof(s16) * l_buffer_limit );
793             msleep( p_aout->date_increment.l_euclidean_integer );
794         }
795         UPDATE_INCREMENT( p_aout->date_increment, p_aout->date )
796     }
797
798     vlc_mutex_lock( &p_aout->fifos_lock );
799     for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
800     {
801         switch ( p_aout->fifo[i_fifo].i_type )
802         {
803             case AOUT_EMPTY_FIFO:
804                 break;
805
806             case AOUT_INTF_MONO_FIFO:
807             case AOUT_INTF_STEREO_FIFO:
808                 free( p_aout->fifo[i_fifo].buffer ); /* !! */
809                 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
810                 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
811                 break;
812
813             case AOUT_ADEC_MONO_FIFO:
814             case AOUT_ADEC_STEREO_FIFO:
815                 free( p_aout->fifo[i_fifo].buffer );
816                 free( p_aout->fifo[i_fifo].date );
817                 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
818                 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
819                 break;
820
821             default:
822                 break;
823         }
824     }
825     vlc_mutex_unlock( &p_aout->fifos_lock );
826 }
827
828 void aout_Thread_U16_Mono( aout_thread_t * p_aout )
829 {
830 }
831
832 void aout_Thread_U16_Stereo( aout_thread_t * p_aout )
833 {
834 }