]> git.sesse.net Git - vlc/blob - src/audio_output/audio_output.c
e7df9795970a2709ef386a30553d3175cf0978d9
[vlc] / src / audio_output / audio_output.c
1 /*****************************************************************************
2  * audio_output.c : audio output thread
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  *
6  * Authors:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *****************************************************************************/
23
24 /* TODO:
25  *
26  * - Passer un certain nombre de "fonctions" (genre add_samples) en macro ou
27  *   inline
28  * - Faire les optimisations dans les fonctions threads :
29  *   = Stocker les "petits calculs" dans des variables au lieu de les refaire
30  *     à chaque boucle
31  *   = Utiliser des tables pour les gros calculs
32  * - Faire une structure différente pour intf/adec fifo
33  *
34  */
35
36 /*****************************************************************************
37  * Preamble
38  *****************************************************************************/
39 #include <unistd.h>                                              /* getpid() */
40
41 #include <stdio.h>                                           /* "intf_msg.h" */
42 #include <stdlib.h>                            /* calloc(), malloc(), free() */
43
44 #include <dlfcn.h>                                                /* plugins */
45
46 #include "common.h"
47 #include "config.h"
48 #include "mtime.h"                             /* mtime_t, mdate(), msleep() */
49 #include "threads.h"
50
51 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
52
53 #include "audio_output.h"
54 #include "main.h"
55
56 /*****************************************************************************
57  * Local prototypes
58  *****************************************************************************/
59
60 static int aout_SpawnThread( aout_thread_t * p_aout );
61
62 /* Creating as much aout_Thread functions as configurations is one solution,
63  * examining the different cases in the Thread loop of an unique function is
64  * another. I chose the first solution. */
65 void aout_Thread_S8_Mono        ( aout_thread_t * p_aout );
66 void aout_Thread_U8_Mono        ( aout_thread_t * p_aout );
67 void aout_Thread_S16_Mono       ( aout_thread_t * p_aout );
68 void aout_Thread_U16_Mono       ( aout_thread_t * p_aout );
69 void aout_Thread_S8_Stereo      ( aout_thread_t * p_aout );
70 void aout_Thread_U8_Stereo      ( aout_thread_t * p_aout );
71 void aout_Thread_S16_Stereo     ( aout_thread_t * p_aout );
72 void aout_Thread_U16_Stereo     ( aout_thread_t * p_aout );
73
74 static __inline__ void InitializeIncrement( aout_increment_t * p_increment, long l_numerator, long l_denominator );
75 static __inline__ int NextFrame( aout_thread_t * p_aout, aout_fifo_t * p_fifo, mtime_t aout_date );
76
77 /*****************************************************************************
78  * aout_CreateThread: initialize audio thread
79  *****************************************************************************/
80 aout_thread_t *aout_CreateThread( int *pi_status )
81 {
82     aout_thread_t * p_aout;                             /* thread descriptor */
83     char * psz_method;
84     char * psz_plugin;
85 #if 0
86     int             i_status;                                 /* thread status */
87 #endif
88
89     /* Allocate descriptor */
90     p_aout = (aout_thread_t *) malloc( sizeof(aout_thread_t) );
91     if( p_aout == NULL )
92     {
93         return( NULL );
94     }
95
96     /* Initialize method-dependent functions */
97     psz_method = main_GetPszVariable( AOUT_METHOD_VAR, AOUT_DEFAULT_METHOD );
98
99     psz_plugin = malloc( sizeof("./audio_output/aout_.so") + strlen(psz_method) );
100     sprintf( psz_plugin, "./audio_output/aout_%s.so", psz_method );
101
102     p_aout->p_aout_plugin = dlopen( psz_plugin, RTLD_NOW | RTLD_GLOBAL );
103
104     if( p_aout->p_aout_plugin == NULL )
105     {
106         intf_ErrMsg( "error: could not open audio plugin %s\n", psz_plugin );
107         free( psz_plugin );
108         free( p_aout );
109         return( NULL );
110     }
111     free( psz_plugin );
112
113     /* Get plugins */
114     p_aout->p_sys_open =         dlsym(p_aout->p_aout_plugin, "aout_SysOpen");
115     p_aout->p_sys_reset =        dlsym(p_aout->p_aout_plugin, "aout_SysReset");
116     p_aout->p_sys_setformat =    dlsym(p_aout->p_aout_plugin, "aout_SysSetFormat");
117     p_aout->p_sys_setchannels =  dlsym(p_aout->p_aout_plugin, "aout_SysSetChannels");
118     p_aout->p_sys_setrate =      dlsym(p_aout->p_aout_plugin, "aout_SysSetRate");
119     p_aout->p_sys_getbufinfo =   dlsym(p_aout->p_aout_plugin, "aout_SysGetBufInfo");
120     p_aout->p_sys_playsamples =  dlsym(p_aout->p_aout_plugin, "aout_SysPlaySamples");
121     p_aout->p_sys_close =        dlsym(p_aout->p_aout_plugin, "aout_SysClose");
122
123     /*
124      * Initialize audio device
125      */
126     if ( p_aout->p_sys_open( p_aout ) )
127     {
128         dlclose( p_aout->p_aout_plugin );
129         free( p_aout );
130         return( NULL );
131     }
132
133     p_aout->b_stereo = ( p_aout->i_channels == 2 ) ? 1 : 0; /* FIXME: only works
134                                                    for i_channels == 1 or 2 ??*/
135
136     if ( p_aout->p_sys_reset( p_aout ) )
137     {
138         p_aout->p_sys_close( p_aout );
139         dlclose( p_aout->p_aout_plugin );
140         free( p_aout );
141         return( NULL );
142     }
143     if ( p_aout->p_sys_setformat( p_aout ) )
144     {
145         p_aout->p_sys_close( p_aout );
146         dlclose( p_aout->p_aout_plugin );
147         free( p_aout );
148         return( NULL );
149     }
150     if ( p_aout->p_sys_setchannels( p_aout ) )
151     {
152         p_aout->p_sys_close( p_aout );
153         dlclose( p_aout->p_aout_plugin );
154         free( p_aout );
155         return( NULL );
156     }
157     if ( p_aout->p_sys_setrate( p_aout ) )
158     {
159         p_aout->p_sys_close( p_aout );
160         dlclose( p_aout->p_aout_plugin );
161         free( p_aout );
162         return( NULL );
163     }
164
165     /* FIXME: maybe it would be cleaner to change SpawnThread prototype
166      * see vout to handle status correctly ?? however, it is not critical since
167      * this thread is only called in main and all calls are blocking */
168     if( aout_SpawnThread( p_aout ) )
169     {
170         p_aout->p_sys_close( p_aout );
171         dlclose( p_aout->p_aout_plugin );
172         free( p_aout );
173         return( NULL );
174     }
175
176     return( p_aout );
177 }
178
179 /*****************************************************************************
180  * aout_SpawnThread
181  *****************************************************************************/
182 static int aout_SpawnThread( aout_thread_t * p_aout )
183 {
184     int             i_fifo;
185     long            l_bytes;
186     void *          aout_thread = NULL;
187
188     intf_DbgMsg("aout debug: spawning audio output thread (%p)\n", p_aout);
189
190     /* We want the audio output thread to live */
191     p_aout->b_die = 0;
192
193     /* Initialize the fifos lock */
194     vlc_mutex_init( &p_aout->fifos_lock );
195     /* Initialize audio fifos : set all fifos as empty and initialize locks */
196     for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
197     {
198         p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
199         vlc_mutex_init( &p_aout->fifo[i_fifo].data_lock );
200         vlc_cond_init( &p_aout->fifo[i_fifo].data_wait );
201     }
202
203     /* Compute the size (in audio units) of the audio output buffer. Although
204      * AOUT_BUFFER_DURATION is given in microseconds, the output rate is given
205      * in Hz, that's why we need to divide by 10^6 microseconds (1 second) */
206     p_aout->l_units = (long)( ((s64)p_aout->l_rate * AOUT_BUFFER_DURATION) / 1000000 );
207     p_aout->l_msleep = (long)( ((s64)p_aout->l_units * 1000000) / (s64)p_aout->l_rate );
208
209     /* Make aout_thread point to the right thread function, and compute the
210      * byte size of the audio output buffer */
211     switch ( p_aout->i_channels )
212     {
213         /* Audio output is mono */
214         case 1:
215             switch ( p_aout->i_format )
216             {
217                 case AOUT_FMT_U8:
218                     l_bytes = 1 * sizeof(u8) * p_aout->l_units;
219                     aout_thread = (void *)aout_Thread_U8_Mono;
220                     break;
221
222                 case AOUT_FMT_S8:
223                     l_bytes = 1 * sizeof(s8) * p_aout->l_units;
224                     aout_thread = (void *)aout_Thread_S8_Mono;
225                     break;
226
227                 case AOUT_FMT_U16_LE:
228                 case AOUT_FMT_U16_BE:
229                     l_bytes = 1 * sizeof(u16) * p_aout->l_units;
230                     aout_thread = (void *)aout_Thread_U16_Mono;
231                     break;
232
233                 case AOUT_FMT_S16_LE:
234                 case AOUT_FMT_S16_BE:
235                     l_bytes = 1 * sizeof(s16) * p_aout->l_units;
236                     aout_thread = (void *)aout_Thread_S16_Mono;
237                     break;
238
239                 default:
240                     intf_ErrMsg( "aout error: unknown audio output format (%i)\n",
241                                  p_aout->i_format );
242                     return( -1 );
243             }
244             break;
245
246         /* Audio output is stereo */
247         case 2:
248             switch ( p_aout->i_format )
249             {
250                 case AOUT_FMT_U8:
251                     l_bytes = 2 * sizeof(u8) * p_aout->l_units;
252                     aout_thread = (void *)aout_Thread_U8_Stereo;
253                     break;
254
255                 case AOUT_FMT_S8:
256                     l_bytes = 2 * sizeof(s8) * p_aout->l_units;
257                     aout_thread = (void *)aout_Thread_S8_Stereo;
258                     break;
259
260                 case AOUT_FMT_U16_LE:
261                 case AOUT_FMT_U16_BE:
262                     l_bytes = 2 * sizeof(u16) * p_aout->l_units;
263                     aout_thread = (void *)aout_Thread_U16_Stereo;
264                     break;
265
266                 case AOUT_FMT_S16_LE:
267                 case AOUT_FMT_S16_BE:
268                     l_bytes = 2 * sizeof(s16) * p_aout->l_units;
269                     aout_thread = (void *)aout_Thread_S16_Stereo;
270                     break;
271
272                 default:
273                     intf_ErrMsg("aout error: unknown audio output format (%i)\n",
274                         p_aout->i_format);
275                     return( -1 );
276             }
277             break;
278
279         default:
280             intf_ErrMsg("aout error: unknown number of audio channels (%i)\n",
281                 p_aout->i_channels );
282             return( -1 );
283     }
284
285     /* Allocate the memory needed by the audio output buffers, and set to zero
286      * the s32 buffer's memory */
287     if ( (p_aout->buffer = malloc(l_bytes)) == NULL )
288     {
289         intf_ErrMsg("aout error: not enough memory to create the output buffer\n");
290         return( -1 );
291     }
292     if ( (p_aout->s32_buffer = (s32 *)calloc(p_aout->l_units, sizeof(s32) << ( p_aout->b_stereo))) == NULL )
293     {
294         intf_ErrMsg("aout error: not enough memory to create the s32 output buffer\n");
295         free( p_aout->buffer );
296         return( -1 );
297     }
298
299     /* Before launching the thread, we try to predict the date of the first
300      * audio unit in the first output buffer */
301     p_aout->date = mdate() - 1000000;
302
303     /* Launch the thread */
304     if ( vlc_thread_create( &p_aout->thread_id, "audio output", (vlc_thread_func_t)aout_thread, p_aout ) )
305     {
306         intf_ErrMsg("aout error: can't spawn audio output thread (%p)\n", p_aout);
307         free( p_aout->buffer );
308         free( p_aout->s32_buffer );
309         return( -1 );
310     }
311
312     intf_DbgMsg("aout debug: audio output thread (%p) spawned\n", p_aout);
313     return( 0 );
314 }
315
316 /*****************************************************************************
317  * aout_DestroyThread
318  *****************************************************************************/
319 void aout_DestroyThread( aout_thread_t * p_aout, int *pi_status )
320 {
321     /* FIXME: pi_status is not handled correctly: check vout how to do!?? */
322
323     intf_DbgMsg("aout debug: requesting termination of audio output thread (%p)\n", p_aout);
324
325     /* Ask thread to kill itself and wait until it's done */
326     p_aout->b_die = 1;
327     vlc_thread_join( p_aout->thread_id ); /* only if pi_status is NULL */
328
329     /* Free the allocated memory */
330     free( p_aout->buffer );
331     free( p_aout->s32_buffer );
332
333     /* Free the structure */
334     p_aout->p_sys_close( p_aout );
335     intf_DbgMsg("aout debug: audio device (%s) closed\n", p_aout->psz_device);
336
337     /* Close plugin */
338     dlclose( p_aout->p_aout_plugin );
339
340     /* Free structure */
341     free( p_aout );
342 }
343
344 /*****************************************************************************
345  * aout_CreateFifo
346  *****************************************************************************/
347 aout_fifo_t * aout_CreateFifo( aout_thread_t * p_aout, aout_fifo_t * p_fifo )
348 {
349     int i_fifo;
350
351     /* Take the fifos lock */
352     vlc_mutex_lock( &p_aout->fifos_lock );
353
354     /* Looking for a free fifo structure */
355     for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
356     {
357         if ( p_aout->fifo[i_fifo].i_type == AOUT_EMPTY_FIFO)
358         {
359             break;
360         }
361     }
362     if ( i_fifo == AOUT_MAX_FIFOS )
363     {
364         intf_ErrMsg("aout error: no empty fifo available\n");
365         vlc_mutex_unlock( &p_aout->fifos_lock );
366         return( NULL );
367     }
368
369     /* Initialize the new fifo structure */
370     switch ( p_aout->fifo[i_fifo].i_type = p_fifo->i_type )
371     {
372         case AOUT_INTF_MONO_FIFO:
373         case AOUT_INTF_STEREO_FIFO:
374             p_aout->fifo[i_fifo].b_die = 0;
375
376             p_aout->fifo[i_fifo].i_channels = p_fifo->i_channels;
377             p_aout->fifo[i_fifo].b_stereo = p_fifo->b_stereo;
378             p_aout->fifo[i_fifo].l_rate = p_fifo->l_rate;
379
380             p_aout->fifo[i_fifo].buffer = p_fifo->buffer;
381
382             p_aout->fifo[i_fifo].l_unit = 0;
383             InitializeIncrement( &p_aout->fifo[i_fifo].unit_increment, p_fifo->l_rate, p_aout->l_rate );
384             p_aout->fifo[i_fifo].l_units = p_fifo->l_units;
385             break;
386
387         case AOUT_ADEC_MONO_FIFO:
388         case AOUT_ADEC_STEREO_FIFO:
389             p_aout->fifo[i_fifo].b_die = 0;
390
391             p_aout->fifo[i_fifo].i_channels = p_fifo->i_channels;
392             p_aout->fifo[i_fifo].b_stereo = p_fifo->b_stereo;
393             p_aout->fifo[i_fifo].l_rate = p_fifo->l_rate;
394
395             p_aout->fifo[i_fifo].l_frame_size = p_fifo->l_frame_size;
396             /* Allocate the memory needed to store the audio frames. As the
397              * fifo is a rotative fifo, we must be able to find out whether the
398              * fifo is full or empty, that's why we must in fact allocate memory
399              * for (AOUT_FIFO_SIZE+1) audio frames. */
400             if ( (p_aout->fifo[i_fifo].buffer = malloc( sizeof(s16)*(AOUT_FIFO_SIZE+1)*p_fifo->l_frame_size )) == NULL )
401             {
402                 intf_ErrMsg("aout error: not enough memory to create the frames buffer\n");
403                 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
404                 vlc_mutex_unlock( &p_aout->fifos_lock );
405                 return( NULL );
406             }
407
408             /* Allocate the memory needed to store the dates of the frames */
409             if ( (p_aout->fifo[i_fifo].date = (mtime_t *)malloc( sizeof(mtime_t)*(AOUT_FIFO_SIZE+1) )) == NULL )
410             {
411                 intf_ErrMsg("aout error: not enough memory to create the dates buffer\n");
412                 free( p_aout->fifo[i_fifo].buffer );
413                 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
414                 vlc_mutex_unlock( &p_aout->fifos_lock );
415                 return( NULL );
416             }
417
418             /* Set the fifo's buffer as empty (the first frame that is to be
419              * played is also the first frame that is not to be played) */
420             p_aout->fifo[i_fifo].l_start_frame = 0;
421             /* p_aout->fifo[i_fifo].l_next_frame = 0; */
422             p_aout->fifo[i_fifo].l_end_frame = 0;
423
424             /* Waiting for the audio decoder to compute enough frames to work
425              * out the fifo's current rate (as soon as the decoder has decoded
426              * enough frames, the members of the fifo structure that are not
427              * initialized now will be calculated) */
428             p_aout->fifo[i_fifo].b_start_frame = 0;
429             p_aout->fifo[i_fifo].b_next_frame = 0;
430             break;
431
432         default:
433             intf_ErrMsg("aout error: unknown fifo type (%i)\n", p_aout->fifo[i_fifo].i_type);
434             p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
435             vlc_mutex_unlock( &p_aout->fifos_lock );
436             return( NULL );
437     }
438
439     /* Release the fifos lock */
440     vlc_mutex_unlock( &p_aout->fifos_lock );
441
442     /* Return the pointer to the fifo structure */
443     intf_DbgMsg("aout debug: audio output fifo (%p) allocated\n", &p_aout->fifo[i_fifo]);
444     return( &p_aout->fifo[i_fifo] );
445 }
446
447 /*****************************************************************************
448  * aout_DestroyFifo
449  *****************************************************************************/
450 void aout_DestroyFifo( aout_fifo_t * p_fifo )
451 {
452     intf_DbgMsg("aout debug: requesting destruction of audio output fifo (%p)\n", p_fifo);
453     p_fifo->b_die = 1;
454 }
455
456 /* Here are the local macros */
457
458 #define UPDATE_INCREMENT( increment, integer ) \
459     if ( ((increment).l_remainder += (increment).l_euclidean_remainder) >= 0 )\
460     { \
461         (integer) += (increment).l_euclidean_integer + 1; \
462         (increment).l_remainder -= (increment).l_euclidean_denominator; \
463     } \
464     else \
465     { \
466         (integer) += (increment).l_euclidean_integer; \
467     }
468
469 /* Following functions are local */
470
471 /*****************************************************************************
472  * InitializeIncrement
473  *****************************************************************************/
474 static __inline__ void InitializeIncrement( aout_increment_t * p_increment, long l_numerator, long l_denominator )
475 {
476     p_increment->l_remainder = -l_denominator;
477
478     p_increment->l_euclidean_integer = 0;
479     while ( l_numerator >= l_denominator )
480     {
481         p_increment->l_euclidean_integer++;
482         l_numerator -= l_denominator;
483     }
484
485     p_increment->l_euclidean_remainder = l_numerator;
486
487     p_increment->l_euclidean_denominator = l_denominator;
488 }
489
490 /*****************************************************************************
491  * NextFrame
492  *****************************************************************************/
493 static __inline__ int NextFrame( aout_thread_t * p_aout, aout_fifo_t * p_fifo, mtime_t aout_date )
494 {
495     long l_units, l_rate;
496
497     /* We take the lock */
498     vlc_mutex_lock( &p_fifo->data_lock );
499
500     /* Are we looking for a dated start frame ? */
501     if ( !p_fifo->b_start_frame )
502     {
503         while ( p_fifo->l_start_frame != p_fifo->l_end_frame )
504         {
505             if ( p_fifo->date[p_fifo->l_start_frame] != LAST_MDATE )
506             {
507                 p_fifo->b_start_frame = 1;
508                 p_fifo->l_next_frame = (p_fifo->l_start_frame + 1) & AOUT_FIFO_SIZE;
509                 p_fifo->l_unit = p_fifo->l_start_frame * (p_fifo->l_frame_size >> (p_fifo->b_stereo));
510                 break;
511             }
512             p_fifo->l_start_frame = (p_fifo->l_start_frame + 1) & AOUT_FIFO_SIZE;
513         }
514
515         if ( p_fifo->l_start_frame == p_fifo->l_end_frame )
516         {
517             vlc_mutex_unlock( &p_fifo->data_lock );
518             return( -1 );
519         }
520     }
521
522     /* We are looking for the next dated frame */
523     /* FIXME : is the output fifo full ?? */
524     while ( !p_fifo->b_next_frame )
525     {
526         while ( p_fifo->l_next_frame != p_fifo->l_end_frame )
527         {
528             if ( p_fifo->date[p_fifo->l_next_frame] != LAST_MDATE )
529             {
530                 p_fifo->b_next_frame = 1;
531                 break;
532             }
533             p_fifo->l_next_frame = (p_fifo->l_next_frame + 1) & AOUT_FIFO_SIZE;
534         }
535
536         while ( p_fifo->l_next_frame == p_fifo->l_end_frame )
537         {
538             vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
539             if ( p_fifo->b_die )
540             {
541                 vlc_mutex_unlock( &p_fifo->data_lock );
542                 return( -1 );
543             }
544         }
545     }
546
547     l_units = ((p_fifo->l_next_frame - p_fifo->l_start_frame) & AOUT_FIFO_SIZE) * (p_fifo->l_frame_size >> (p_fifo->b_stereo));
548
549     l_rate = p_fifo->l_rate + ((aout_date - p_fifo->date[p_fifo->l_start_frame]) / 256);
550 //    fprintf( stderr, "aout debug: %lli (%li);\n", aout_date - p_fifo->date[p_fifo->l_start_frame], l_rate );
551
552     InitializeIncrement( &p_fifo->unit_increment, l_rate, p_aout->l_rate );
553
554     p_fifo->l_units = (((l_units - (p_fifo->l_unit -
555         (p_fifo->l_start_frame * (p_fifo->l_frame_size >> (p_fifo->b_stereo)))))
556         * p_aout->l_rate) / l_rate) + 1;
557
558     /* We release the lock before leaving */
559     vlc_mutex_unlock( &p_fifo->data_lock );
560     return( 0 );
561 }
562
563 void aout_Thread_S8_Mono( aout_thread_t * p_aout )
564 {
565 }
566
567 void aout_Thread_S8_Stereo( aout_thread_t * p_aout )
568 {
569 }
570
571 void aout_Thread_U8_Mono( aout_thread_t * p_aout )
572 {
573 }
574
575 void aout_Thread_U8_Stereo( aout_thread_t * p_aout )
576 {
577     int i_fifo;
578     long l_buffer, l_buffer_limit;
579     long l_units, l_bytes;
580
581     intf_DbgMsg("adec debug: ********aout_Thread_U8_Stereo********\n");
582     intf_DbgMsg("adec debug: running audio output thread (%p) (pid == %i)\n", p_aout, getpid());
583
584     /* As the s32_buffer was created with calloc(), we don't have to set this
585      * memory to zero and we can immediately jump into the thread's loop */
586     while ( !p_aout->b_die )
587     {
588         vlc_mutex_lock( &p_aout->fifos_lock );
589         for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
590         {
591             switch ( p_aout->fifo[i_fifo].i_type )
592             {
593                 case AOUT_EMPTY_FIFO:
594                     break;
595
596                 case AOUT_INTF_MONO_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->b_stereo == 1 */
601                         {
602                             p_aout->s32_buffer[l_buffer++] +=
603                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
604                             p_aout->s32_buffer[l_buffer++] +=
605                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
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->b_stereo == 1 */
614                         {
615                             p_aout->s32_buffer[l_buffer++] +=
616                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
617                             p_aout->s32_buffer[l_buffer++] +=
618                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
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_INTF_STEREO_FIFO:
628                     if ( p_aout->fifo[i_fifo].l_units > p_aout->l_units )
629                     {
630                         l_buffer = 0;
631                         while ( l_buffer < (p_aout->l_units << 1) ) /* p_aout->b_stereo == 1 */
632                         {
633                             p_aout->s32_buffer[l_buffer++] +=
634                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
635                             p_aout->s32_buffer[l_buffer++] +=
636                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
637                             UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
638                         }
639                         p_aout->fifo[i_fifo].l_units -= p_aout->l_units;
640                     }
641                     else
642                     {
643                         l_buffer = 0;
644                         while ( l_buffer < (p_aout->fifo[i_fifo].l_units << 1) ) /* p_aout->b_stereo == 1 */
645                         {
646                             p_aout->s32_buffer[l_buffer++] +=
647                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
648                             p_aout->s32_buffer[l_buffer++] +=
649                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
650                             UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
651                         }
652                         free( p_aout->fifo[i_fifo].buffer ); /* !! */
653                         p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
654                         intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]); /* !! */
655                     }
656                     break;
657
658                 case AOUT_ADEC_MONO_FIFO:
659                     if ( p_aout->fifo[i_fifo].b_die )
660                     {
661                         free( p_aout->fifo[i_fifo].buffer );
662                         free( p_aout->fifo[i_fifo].date );
663                         p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
664                         intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
665                         continue;
666                     }
667
668                     l_units = p_aout->l_units;
669                     l_buffer = 0;
670                     while ( l_units > 0 )
671                     {
672                         if ( !p_aout->fifo[i_fifo].b_next_frame )
673                         {
674                             if ( NextFrame(p_aout, &p_aout->fifo[i_fifo], p_aout->date + ((((mtime_t)(l_buffer >> 1)) * 1000000) / ((mtime_t)p_aout->l_rate))) )
675                             {
676                                 break;
677                             }
678                         }
679
680                         if ( p_aout->fifo[i_fifo].l_units > l_units )
681                         {
682                             l_buffer_limit = p_aout->l_units << 1; /* p_aout->b_stereo == 1 */
683                             while ( l_buffer < l_buffer_limit )
684                             {
685                                 p_aout->s32_buffer[l_buffer++] +=
686                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
687                                 p_aout->s32_buffer[l_buffer++] +=
688                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
689
690                                 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
691                                 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 0 */
692                                      ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0)) )
693                                 {
694                                     p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 0 */
695                                         ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0));
696                                 }
697                             }
698                             p_aout->fifo[i_fifo].l_units -= l_units;
699                             break;
700                         }
701                         else
702                         {
703                             l_buffer_limit = l_buffer + (p_aout->fifo[i_fifo].l_units << 1);
704                             /* p_aout->b_stereo == 1 */
705                             while ( l_buffer < l_buffer_limit )
706                             {
707                                 p_aout->s32_buffer[l_buffer++] +=
708                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
709                                 p_aout->s32_buffer[l_buffer++] +=
710                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
711
712                                 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
713                                 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 0 */
714                                      ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0)) )
715                                 {
716                                     p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 0 */
717                                         ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0));
718                                 }
719                             }
720                             l_units -= p_aout->fifo[i_fifo].l_units;
721
722                             vlc_mutex_lock( &p_aout->fifo[i_fifo].data_lock );
723                             p_aout->fifo[i_fifo].l_start_frame = p_aout->fifo[i_fifo].l_next_frame;
724                             vlc_cond_signal( &p_aout->fifo[i_fifo].data_wait );
725                             vlc_mutex_unlock( &p_aout->fifo[i_fifo].data_lock );
726
727                             /* p_aout->fifo[i_fifo].b_start_frame = 1; */
728                             p_aout->fifo[i_fifo].l_next_frame += 1;
729                             p_aout->fifo[i_fifo].l_next_frame &= AOUT_FIFO_SIZE;
730                             p_aout->fifo[i_fifo].b_next_frame = 0;
731                         }
732                     }
733                     break;
734
735                 case AOUT_ADEC_STEREO_FIFO:
736                     if ( p_aout->fifo[i_fifo].b_die )
737                     {
738                         free( p_aout->fifo[i_fifo].buffer );
739                         free( p_aout->fifo[i_fifo].date );
740                         p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
741                         intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
742                         continue;
743                     }
744
745                     l_units = p_aout->l_units;
746                     l_buffer = 0;
747                     while ( l_units > 0 )
748                     {
749                         if ( !p_aout->fifo[i_fifo].b_next_frame )
750                         {
751                             if ( NextFrame(p_aout, &p_aout->fifo[i_fifo], p_aout->date + ((((mtime_t)(l_buffer >> 1)) * 1000000) / ((mtime_t)p_aout->l_rate))) )
752                             {
753                                 break;
754                             }
755                         }
756
757                         if ( p_aout->fifo[i_fifo].l_units > l_units )
758                         {
759                             l_buffer_limit = p_aout->l_units << 1; /* p_aout->b_stereo == 1 */
760                             while ( l_buffer < l_buffer_limit )
761                             {
762                                 p_aout->s32_buffer[l_buffer++] +=
763                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
764                                 p_aout->s32_buffer[l_buffer++] +=
765                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
766
767                                 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
768                                 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 1 */
769                                      ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1)) )
770                                 {
771                                     p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 1 */
772                                         ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1));
773                                 }
774                             }
775                             p_aout->fifo[i_fifo].l_units -= l_units;
776                             break;
777                         }
778                         else
779                         {
780                             l_buffer_limit = l_buffer + (p_aout->fifo[i_fifo].l_units << 1);
781                             /* p_aout->b_stereo == 1 */
782                             while ( l_buffer < l_buffer_limit )
783                             {
784                                 p_aout->s32_buffer[l_buffer++] +=
785                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
786                                 p_aout->s32_buffer[l_buffer++] +=
787                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
788
789                                 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
790                                 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 1 */
791                                      ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1)) )
792                                 {
793                                     p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 1 */
794                                         ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1));
795                                 }
796                             }
797                             l_units -= p_aout->fifo[i_fifo].l_units;
798
799                             vlc_mutex_lock( &p_aout->fifo[i_fifo].data_lock );
800                             p_aout->fifo[i_fifo].l_start_frame = p_aout->fifo[i_fifo].l_next_frame;
801                             vlc_cond_signal( &p_aout->fifo[i_fifo].data_wait );
802                             vlc_mutex_unlock( &p_aout->fifo[i_fifo].data_lock );
803
804                             /* p_aout->fifo[i_fifo].b_start_frame = 1; */
805                             p_aout->fifo[i_fifo].l_next_frame += 1;
806                             p_aout->fifo[i_fifo].l_next_frame &= AOUT_FIFO_SIZE;
807                             p_aout->fifo[i_fifo].b_next_frame = 0;
808                         }
809                     }
810                     break;
811
812             default:
813                     intf_DbgMsg("aout debug: unknown fifo type (%i)\n", p_aout->fifo[i_fifo].i_type);
814                     break;
815             }
816         }
817         vlc_mutex_unlock( &p_aout->fifos_lock );
818
819         l_buffer_limit = p_aout->l_units  << 1 ; /* p_aout->b_stereo == 1 */
820
821         for ( l_buffer = 0; l_buffer < l_buffer_limit; l_buffer++ )
822         {
823             ((u8 *)p_aout->buffer)[l_buffer] = (u8)( (p_aout->s32_buffer[l_buffer] / 256) + 128 );
824             p_aout->s32_buffer[l_buffer] = 0;
825         }
826         l_bytes = p_aout->p_sys_getbufinfo( p_aout, l_buffer_limit );
827         p_aout->date = mdate() + ((((mtime_t)(l_bytes / 2 )) * 1000000) / ((mtime_t)p_aout->l_rate)); /* sizeof(u8) << (p_aout->b_stereo) == 2 */
828         p_aout->p_sys_playsamples( p_aout, (byte_t *)p_aout->buffer, l_buffer_limit * sizeof(u8) );
829         if ( l_bytes > (l_buffer_limit * sizeof(u8)) )
830         {
831             msleep( p_aout->l_msleep );
832         }
833     }
834
835     vlc_mutex_lock( &p_aout->fifos_lock );
836     for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
837     {
838         switch ( p_aout->fifo[i_fifo].i_type )
839         {
840             case AOUT_EMPTY_FIFO:
841                 break;
842
843             case AOUT_INTF_MONO_FIFO:
844             case AOUT_INTF_STEREO_FIFO:
845                 free( p_aout->fifo[i_fifo].buffer ); /* !! */
846                 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
847                 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
848                 break;
849
850             case AOUT_ADEC_MONO_FIFO:
851             case AOUT_ADEC_STEREO_FIFO:
852                 free( p_aout->fifo[i_fifo].buffer );
853                 free( p_aout->fifo[i_fifo].date );
854                 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
855                 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
856                 break;
857
858             default:
859                 break;
860         }
861     }
862     vlc_mutex_unlock( &p_aout->fifos_lock );
863
864 }
865
866 void aout_Thread_S16_Mono( aout_thread_t * p_aout )
867 {
868 }
869
870 void aout_Thread_S16_Stereo( aout_thread_t * p_aout )
871 {
872     int i_fifo;
873     long l_buffer, l_buffer_limit;
874     long l_units, l_bytes;
875
876     intf_DbgMsg("adec debug: ********aout_Thread_S16_Stereo********\n");
877     intf_DbgMsg("adec debug: running audio output thread (%p) (pid == %i)\n", p_aout, getpid());
878
879     /* As the s32_buffer was created with calloc(), we don't have to set this
880      * memory to zero and we can immediately jump into the thread's loop */
881     while ( !p_aout->b_die )
882     {
883         vlc_mutex_lock( &p_aout->fifos_lock );
884         for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
885         {
886             switch ( p_aout->fifo[i_fifo].i_type )
887             {
888                 case AOUT_EMPTY_FIFO:
889                     break;
890
891                 case AOUT_INTF_MONO_FIFO:
892                     if ( p_aout->fifo[i_fifo].l_units > p_aout->l_units )
893                     {
894                         l_buffer = 0;
895                         while ( l_buffer < (p_aout->l_units << 1) ) /* p_aout->b_stereo == 1 */
896                         {
897                             p_aout->s32_buffer[l_buffer++] +=
898                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
899                             p_aout->s32_buffer[l_buffer++] +=
900                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
901                             UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
902                         }
903                         p_aout->fifo[i_fifo].l_units -= p_aout->l_units;
904                     }
905                     else
906                     {
907                         l_buffer = 0;
908                         while ( l_buffer < (p_aout->fifo[i_fifo].l_units << 1) ) /* p_aout->b_stereo == 1 */
909                         {
910                             p_aout->s32_buffer[l_buffer++] +=
911                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
912                             p_aout->s32_buffer[l_buffer++] +=
913                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
914                             UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
915                         }
916                         free( p_aout->fifo[i_fifo].buffer ); /* !! */
917                         p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
918                         intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]); /* !! */
919                     }
920                     break;
921
922                 case AOUT_INTF_STEREO_FIFO:
923                     if ( p_aout->fifo[i_fifo].l_units > p_aout->l_units )
924                     {
925                         l_buffer = 0;
926                         while ( l_buffer < (p_aout->l_units << 1) ) /* p_aout->b_stereo == 1 */
927                         {
928                             p_aout->s32_buffer[l_buffer++] +=
929                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
930                             p_aout->s32_buffer[l_buffer++] +=
931                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
932                             UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
933                         }
934                         p_aout->fifo[i_fifo].l_units -= p_aout->l_units;
935                     }
936                     else
937                     {
938                         l_buffer = 0;
939                         while ( l_buffer < (p_aout->fifo[i_fifo].l_units << 1) ) /* p_aout->b_stereo == 1 */
940                         {
941                             p_aout->s32_buffer[l_buffer++] +=
942                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
943                             p_aout->s32_buffer[l_buffer++] +=
944                                 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
945                             UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
946                         }
947                         free( p_aout->fifo[i_fifo].buffer ); /* !! */
948                         p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
949                         intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]); /* !! */
950                     }
951                     break;
952
953                 case AOUT_ADEC_MONO_FIFO:
954                     if ( p_aout->fifo[i_fifo].b_die )
955                     {
956                         free( p_aout->fifo[i_fifo].buffer );
957                         free( p_aout->fifo[i_fifo].date );
958                         p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
959                         intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
960                         continue;
961                     }
962
963                     l_units = p_aout->l_units;
964                     l_buffer = 0;
965                     while ( l_units > 0 )
966                     {
967                         if ( !p_aout->fifo[i_fifo].b_next_frame )
968                         {
969                             if ( NextFrame(p_aout, &p_aout->fifo[i_fifo], p_aout->date + ((((mtime_t)(l_buffer >> 1)) * 1000000) / ((mtime_t)p_aout->l_rate))) )
970                             {
971                                 break;
972                             }
973                         }
974
975                         if ( p_aout->fifo[i_fifo].l_units > l_units )
976                         {
977                             l_buffer_limit = p_aout->l_units << 1; /* p_aout->b_stereo == 1 */
978                             while ( l_buffer < l_buffer_limit )
979                             {
980                                 p_aout->s32_buffer[l_buffer++] +=
981                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
982                                 p_aout->s32_buffer[l_buffer++] +=
983                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
984
985                                 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
986                                 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 0 */
987                                      ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0)) )
988                                 {
989                                     p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 0 */
990                                         ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0));
991                                 }
992                             }
993                             p_aout->fifo[i_fifo].l_units -= l_units;
994                             break;
995                         }
996                         else
997                         {
998                             l_buffer_limit = l_buffer + (p_aout->fifo[i_fifo].l_units << 1);
999                             /* p_aout->b_stereo == 1 */
1000                             while ( l_buffer < l_buffer_limit )
1001                             {
1002                                 p_aout->s32_buffer[l_buffer++] +=
1003                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
1004                                 p_aout->s32_buffer[l_buffer++] +=
1005                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
1006
1007                                 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
1008                                 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 0 */
1009                                      ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0)) )
1010                                 {
1011                                     p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 0 */
1012                                         ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0));
1013                                 }
1014                             }
1015                             l_units -= p_aout->fifo[i_fifo].l_units;
1016
1017                             vlc_mutex_lock( &p_aout->fifo[i_fifo].data_lock );
1018                             p_aout->fifo[i_fifo].l_start_frame = p_aout->fifo[i_fifo].l_next_frame;
1019                             vlc_cond_signal( &p_aout->fifo[i_fifo].data_wait );
1020                             vlc_mutex_unlock( &p_aout->fifo[i_fifo].data_lock );
1021
1022                             /* p_aout->fifo[i_fifo].b_start_frame = 1; */
1023                             p_aout->fifo[i_fifo].l_next_frame += 1;
1024                             p_aout->fifo[i_fifo].l_next_frame &= AOUT_FIFO_SIZE;
1025                             p_aout->fifo[i_fifo].b_next_frame = 0;
1026                         }
1027                     }
1028                     break;
1029
1030                 case AOUT_ADEC_STEREO_FIFO:
1031                     if ( p_aout->fifo[i_fifo].b_die )
1032                     {
1033                         free( p_aout->fifo[i_fifo].buffer );
1034                         free( p_aout->fifo[i_fifo].date );
1035                         p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
1036                         intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
1037                         continue;
1038                     }
1039
1040                     l_units = p_aout->l_units;
1041                     l_buffer = 0;
1042                     while ( l_units > 0 )
1043                     {
1044                         if ( !p_aout->fifo[i_fifo].b_next_frame )
1045                         {
1046                             if ( NextFrame(p_aout, &p_aout->fifo[i_fifo], p_aout->date + ((((mtime_t)(l_buffer >> 1)) * 1000000) / ((mtime_t)p_aout->l_rate))) )
1047                             {
1048                                 break;
1049                             }
1050                         }
1051
1052                         if ( p_aout->fifo[i_fifo].l_units > l_units )
1053                         {
1054                             l_buffer_limit = p_aout->l_units << 1; /* p_aout->b_stereo == 1 */
1055                             while ( l_buffer < l_buffer_limit )
1056                             {
1057                                 p_aout->s32_buffer[l_buffer++] +=
1058                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
1059                                 p_aout->s32_buffer[l_buffer++] +=
1060                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
1061
1062                                 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
1063                                 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 1 */
1064                                      ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1)) )
1065                                 {
1066                                     p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 1 */
1067                                         ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1));
1068                                 }
1069                             }
1070                             p_aout->fifo[i_fifo].l_units -= l_units;
1071                             break;
1072                         }
1073                         else
1074                         {
1075                             l_buffer_limit = l_buffer + (p_aout->fifo[i_fifo].l_units << 1);
1076                             /* p_aout->b_stereo == 1 */
1077                             while ( l_buffer < l_buffer_limit )
1078                             {
1079                                 p_aout->s32_buffer[l_buffer++] +=
1080                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
1081                                 p_aout->s32_buffer[l_buffer++] +=
1082                                     (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
1083
1084                                 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
1085                                 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 1 */
1086                                      ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1)) )
1087                                 {
1088                                     p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 1 */
1089                                         ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1));
1090                                 }
1091                             }
1092                             l_units -= p_aout->fifo[i_fifo].l_units;
1093
1094                             vlc_mutex_lock( &p_aout->fifo[i_fifo].data_lock );
1095                             p_aout->fifo[i_fifo].l_start_frame = p_aout->fifo[i_fifo].l_next_frame;
1096                             vlc_cond_signal( &p_aout->fifo[i_fifo].data_wait );
1097                             vlc_mutex_unlock( &p_aout->fifo[i_fifo].data_lock );
1098
1099                             /* p_aout->fifo[i_fifo].b_start_frame = 1; */
1100                             p_aout->fifo[i_fifo].l_next_frame += 1;
1101                             p_aout->fifo[i_fifo].l_next_frame &= AOUT_FIFO_SIZE;
1102                             p_aout->fifo[i_fifo].b_next_frame = 0;
1103                         }
1104                     }
1105                     break;
1106
1107             default:
1108                     intf_DbgMsg("aout debug: unknown fifo type (%i)\n", p_aout->fifo[i_fifo].i_type);
1109                     break;
1110             }
1111         }
1112         vlc_mutex_unlock( &p_aout->fifos_lock );
1113
1114         l_buffer_limit = p_aout->l_units << 1; /* p_aout->b_stereo == 1 */
1115
1116         for ( l_buffer = 0; l_buffer < l_buffer_limit; l_buffer++ )
1117         {
1118             ((s16 *)p_aout->buffer)[l_buffer] = (s16)( p_aout->s32_buffer[l_buffer] / AOUT_MAX_FIFOS );
1119             p_aout->s32_buffer[l_buffer] = 0;
1120         }
1121
1122         l_bytes = p_aout->p_sys_getbufinfo( p_aout, l_buffer_limit );
1123         p_aout->date = -1000000 + mdate() + ((((mtime_t)(l_bytes / 4)) * 1000000) / ((mtime_t)p_aout->l_rate)); /* sizeof(s16) << (p_aout->b_stereo) == 4 */
1124         p_aout->p_sys_playsamples( p_aout, (byte_t *)p_aout->buffer, l_buffer_limit * sizeof(s16) );
1125         if ( l_bytes > (l_buffer_limit * sizeof(s16)) )
1126         {
1127             msleep( p_aout->l_msleep );
1128         }
1129     }
1130
1131     vlc_mutex_lock( &p_aout->fifos_lock );
1132     for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
1133     {
1134         switch ( p_aout->fifo[i_fifo].i_type )
1135         {
1136             case AOUT_EMPTY_FIFO:
1137                 break;
1138
1139             case AOUT_INTF_MONO_FIFO:
1140             case AOUT_INTF_STEREO_FIFO:
1141                 free( p_aout->fifo[i_fifo].buffer ); /* !! */
1142                 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
1143                 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
1144                 break;
1145
1146             case AOUT_ADEC_MONO_FIFO:
1147             case AOUT_ADEC_STEREO_FIFO:
1148                 free( p_aout->fifo[i_fifo].buffer );
1149                 free( p_aout->fifo[i_fifo].date );
1150                 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
1151                 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
1152                 break;
1153
1154             default:
1155                 break;
1156         }
1157     }
1158     vlc_mutex_unlock( &p_aout->fifos_lock );
1159 }
1160
1161 void aout_Thread_U16_Mono( aout_thread_t * p_aout )
1162 {
1163 }
1164
1165 void aout_Thread_U16_Stereo( aout_thread_t * p_aout )
1166 {
1167 }