1 /*****************************************************************************
2 * audio_output.c : audio output thread
4 *****************************************************************************/
8 * - Passer un certain nombre de "fonctions" (genre add_samples) en macro ou
10 * - Faire les optimisations dans les fonctions threads :
11 * = Stocker les "petits calculs" dans des variables au lieu de les refaire
13 * = Utiliser des tables pour les gros calculs
14 * - Faire une structure différente pour intf/adec fifo
18 /*****************************************************************************
20 *****************************************************************************/
23 #include <stdio.h> /* "intf_msg.h" */
24 #include <stdlib.h> /* calloc(), malloc(), free() */
28 #include "mtime.h" /* mtime_t, mdate(), msleep() */
29 #include "vlc_thread.h"
31 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
33 #include "audio_output.h"
34 #include "audio_sys.h"
37 /*****************************************************************************
39 *****************************************************************************/
41 static int aout_SpawnThread( aout_thread_t * p_aout );
43 /* Creating as much aout_Thread functions as configurations is one solution,
44 * examining the different cases in the Thread loop of an unique function is
45 * another. I chose the first solution. */
46 void aout_Thread_S8_Mono ( aout_thread_t * p_aout );
47 void aout_Thread_U8_Mono ( aout_thread_t * p_aout );
48 void aout_Thread_S16_Mono ( aout_thread_t * p_aout );
49 void aout_Thread_U16_Mono ( aout_thread_t * p_aout );
50 void aout_Thread_S8_Stereo ( aout_thread_t * p_aout );
51 void aout_Thread_U8_Stereo ( aout_thread_t * p_aout );
52 void aout_Thread_S16_Stereo ( aout_thread_t * p_aout );
53 void aout_Thread_U16_Stereo ( aout_thread_t * p_aout );
55 static __inline__ void InitializeIncrement( aout_increment_t * p_increment, long l_numerator, long l_denominator );
56 static __inline__ int NextFrame( aout_thread_t * p_aout, aout_fifo_t * p_fifo, mtime_t aout_date );
58 /*****************************************************************************
59 * aout_CreateThread: initialize audio thread
60 *****************************************************************************/
61 aout_thread_t *aout_CreateThread( int *pi_status )
63 aout_thread_t * p_aout; /* thread descriptor */
65 // int i_status; /* thread status */
67 /* Allocate descriptor */
68 p_aout = (aout_thread_t *) malloc( sizeof(aout_thread_t) );
74 /* initialize method-dependent functions */
75 psz_method = main_GetPszVariable( AOUT_METHOD_VAR, AOUT_DEFAULT_METHOD );
77 if( !strcmp(psz_method, "dummy") )
79 p_aout->p_sys_open = aout_DummySysOpen;
80 p_aout->p_sys_reset = aout_DummySysReset;
81 p_aout->p_sys_setformat = aout_DummySysSetFormat;
82 p_aout->p_sys_setchannels = aout_DummySysSetChannels;
83 p_aout->p_sys_setrate = aout_DummySysSetRate;
84 p_aout->p_sys_getbufinfo = aout_DummySysGetBufInfo;
85 p_aout->p_sys_playsamples = aout_DummySysPlaySamples;
86 p_aout->p_sys_close = aout_DummySysClose;
89 else if( !strcmp(psz_method, "dsp") )
91 p_aout->p_sys_open = aout_DspSysOpen;
92 p_aout->p_sys_reset = aout_DspSysReset;
93 p_aout->p_sys_setformat = aout_DspSysSetFormat;
94 p_aout->p_sys_setchannels = aout_DspSysSetChannels;
95 p_aout->p_sys_setrate = aout_DspSysSetRate;
96 p_aout->p_sys_getbufinfo = aout_DspSysGetBufInfo;
97 p_aout->p_sys_playsamples = aout_DspSysPlaySamples;
98 p_aout->p_sys_close = aout_DspSysClose;
103 intf_ErrMsg( "error: requested audio output method not available\n" );
109 * Initialize audio device
111 if ( p_aout->p_sys_open( p_aout ) )
116 if ( p_aout->p_sys_reset( p_aout ) )
118 p_aout->p_sys_close( p_aout );
122 if ( p_aout->p_sys_setformat( p_aout ) )
124 p_aout->p_sys_close( p_aout );
128 if ( p_aout->p_sys_setchannels( p_aout ) )
130 p_aout->p_sys_close( p_aout );
134 if ( p_aout->p_sys_setrate( p_aout ) )
136 p_aout->p_sys_close( p_aout );
141 /* this code isn't very nice since some values might be uninitialized */
142 /* intf_DbgMsg("aout debug: audio device (%s) opened (format=%i, channels=%i, rate=%li)\n",
145 p_aout->i_channels, p_aout->l_rate); */
147 //?? maybe it would be cleaner to change SpawnThread prototype
148 //?? see vout to handle status correctly - however, it is not critical since
149 //?? this thread is only called in main and all calls are blocking
150 if( aout_SpawnThread( p_aout ) )
152 p_aout->p_sys_close( p_aout );
160 /*****************************************************************************
162 *****************************************************************************/
163 static int aout_SpawnThread( aout_thread_t * p_aout )
167 void * aout_thread = NULL;
169 intf_DbgMsg("aout debug: spawning audio output thread (%p)\n", p_aout);
171 /* We want the audio output thread to live */
174 /* Initialize the fifos lock */
175 vlc_mutex_init( &p_aout->fifos_lock );
176 /* Initialize audio fifos : set all fifos as empty and initialize locks */
177 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
179 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
180 vlc_mutex_init( &p_aout->fifo[i_fifo].data_lock );
181 vlc_cond_init( &p_aout->fifo[i_fifo].data_wait );
184 /* Compute the size (in audio units) of the audio output buffer. Although
185 * AOUT_BUFFER_DURATION is given in microseconds, the output rate is given
186 * in Hz, that's why we need to divide by 10^6 microseconds (1 second) */
187 p_aout->l_units = (long)( ((s64)p_aout->l_rate * AOUT_BUFFER_DURATION) / 1000000 );
188 p_aout->l_msleep = (long)( ((s64)p_aout->l_units * 1000000) / (s64)p_aout->l_rate );
190 /* Make aout_thread point to the right thread function, and compute the
191 * byte size of the audio output buffer */
192 switch ( p_aout->i_channels )
194 /* Audio output is mono */
196 switch ( p_aout->i_format )
199 l_bytes = 1 * sizeof(u8) * p_aout->l_units;
200 aout_thread = (void *)aout_Thread_U8_Mono;
204 l_bytes = 1 * sizeof(s8) * p_aout->l_units;
205 aout_thread = (void *)aout_Thread_S8_Mono;
208 case AOUT_FMT_U16_LE:
209 case AOUT_FMT_U16_BE:
210 l_bytes = 1 * sizeof(u16) * p_aout->l_units;
211 aout_thread = (void *)aout_Thread_U16_Mono;
214 case AOUT_FMT_S16_LE:
215 case AOUT_FMT_S16_BE:
216 l_bytes = 1 * sizeof(s16) * p_aout->l_units;
217 aout_thread = (void *)aout_Thread_S16_Mono;
221 intf_ErrMsg("aout error: unknown audio output format (%i)\n",
227 /* Audio output is stereo */
229 switch ( p_aout->i_format )
232 l_bytes = 2 * sizeof(u8) * p_aout->l_units;
233 aout_thread = (void *)aout_Thread_U8_Stereo;
237 l_bytes = 2 * sizeof(s8) * p_aout->l_units;
238 aout_thread = (void *)aout_Thread_S8_Stereo;
241 case AOUT_FMT_U16_LE:
242 case AOUT_FMT_U16_BE:
243 l_bytes = 2 * sizeof(u16) * p_aout->l_units;
244 aout_thread = (void *)aout_Thread_U16_Stereo;
247 case AOUT_FMT_S16_LE:
248 case AOUT_FMT_S16_BE:
249 l_bytes = 2 * sizeof(s16) * p_aout->l_units;
250 aout_thread = (void *)aout_Thread_S16_Stereo;
254 intf_ErrMsg("aout error: unknown audio output format (%i)\n",
261 intf_ErrMsg("aout error: unknown number of audio channels (%i)\n",
262 p_aout->i_channels );
266 /* Allocate the memory needed by the audio output buffers, and set to zero
267 * the s32 buffer's memory */
268 if ( (p_aout->buffer = malloc(l_bytes)) == NULL )
270 intf_ErrMsg("aout error: not enough memory to create the output buffer\n");
273 if ( (p_aout->s32_buffer = (s32 *)calloc(p_aout->l_units, sizeof(s32) << ( p_aout->i_channels - 1))) == NULL )
275 intf_ErrMsg("aout error: not enough memory to create the s32 output buffer\n");
276 free( p_aout->buffer );
280 /* Before launching the thread, we try to predict the date of the first
281 * audio unit in the first output buffer */
282 p_aout->date = mdate();
284 /* Launch the thread */
285 if ( vlc_thread_create( &p_aout->thread_id, "audio output", (vlc_thread_func_t)aout_thread, p_aout ) )
287 intf_ErrMsg("aout error: can't spawn audio output thread (%p)\n", p_aout);
288 free( p_aout->buffer );
289 free( p_aout->s32_buffer );
293 intf_DbgMsg("aout debug: audio output thread (%p) spawned\n", p_aout);
297 /*****************************************************************************
299 *****************************************************************************/
300 void aout_DestroyThread( aout_thread_t * p_aout, int *pi_status )
302 //???? pi_status is not handled correctly: check vout how to do!
304 intf_DbgMsg("aout debug: requesting termination of audio output thread (%p)\n", p_aout);
306 /* Ask thread to kill itself and wait until it's done */
308 vlc_thread_join( p_aout->thread_id ); // only if pi_status is NULL
310 /* Free the allocated memory */
311 free( p_aout->buffer );
312 free( p_aout->s32_buffer );
314 /* Free the structure */
315 p_aout->p_sys_close( p_aout );
316 intf_DbgMsg("aout debug: audio device (%s) closed\n", p_aout->psz_device);
320 /*****************************************************************************
322 *****************************************************************************/
323 aout_fifo_t * aout_CreateFifo( aout_thread_t * p_aout, aout_fifo_t * p_fifo )
327 /* Take the fifos lock */
328 vlc_mutex_lock( &p_aout->fifos_lock );
330 /* Looking for a free fifo structure */
331 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
333 if ( p_aout->fifo[i_fifo].i_type == AOUT_EMPTY_FIFO)
338 if ( i_fifo == AOUT_MAX_FIFOS )
340 intf_ErrMsg("aout error: no empty fifo available\n");
341 vlc_mutex_unlock( &p_aout->fifos_lock );
345 /* Initialize the new fifo structure */
346 switch ( p_aout->fifo[i_fifo].i_type = p_fifo->i_type )
348 case AOUT_INTF_MONO_FIFO:
349 case AOUT_INTF_STEREO_FIFO:
350 p_aout->fifo[i_fifo].b_die = 0;
352 p_aout->fifo[i_fifo].i_channels = p_fifo->i_channels;
353 p_aout->fifo[i_fifo].l_rate = p_fifo->l_rate;
355 p_aout->fifo[i_fifo].buffer = p_fifo->buffer;
357 p_aout->fifo[i_fifo].l_unit = 0;
358 InitializeIncrement( &p_aout->fifo[i_fifo].unit_increment, p_fifo->l_rate, p_aout->l_rate );
359 p_aout->fifo[i_fifo].l_units = p_fifo->l_units;
362 case AOUT_ADEC_MONO_FIFO:
363 case AOUT_ADEC_STEREO_FIFO:
364 p_aout->fifo[i_fifo].b_die = 0;
366 p_aout->fifo[i_fifo].i_channels = p_fifo->i_channels;
367 p_aout->fifo[i_fifo].l_rate = p_fifo->l_rate;
369 p_aout->fifo[i_fifo].l_frame_size = p_fifo->l_frame_size;
370 /* Allocate the memory needed to store the audio frames. As the
371 * fifo is a rotative fifo, we must be able to find out whether the
372 * fifo is full or empty, that's why we must in fact allocate memory
373 * for (AOUT_FIFO_SIZE+1) audio frames. */
374 if ( (p_aout->fifo[i_fifo].buffer = malloc( sizeof(s16)*(AOUT_FIFO_SIZE+1)*p_fifo->l_frame_size )) == NULL )
376 intf_ErrMsg("aout error: not enough memory to create the frames buffer\n");
377 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
378 vlc_mutex_unlock( &p_aout->fifos_lock );
382 /* Allocate the memory needed to store the dates of the frames */
383 if ( (p_aout->fifo[i_fifo].date = (mtime_t *)malloc( sizeof(mtime_t)*(AOUT_FIFO_SIZE+1) )) == NULL )
385 intf_ErrMsg("aout error: not enough memory to create the dates buffer\n");
386 free( p_aout->fifo[i_fifo].buffer );
387 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
388 vlc_mutex_unlock( &p_aout->fifos_lock );
392 /* Set the fifo's buffer as empty (the first frame that is to be
393 * played is also the first frame that is not to be played) */
394 p_aout->fifo[i_fifo].l_start_frame = 0;
395 /* p_aout->fifo[i_fifo].l_next_frame = 0; */
396 p_aout->fifo[i_fifo].l_end_frame = 0;
398 /* Waiting for the audio decoder to compute enough frames to work
399 * out the fifo's current rate (as soon as the decoder has decoded
400 * enough frames, the members of the fifo structure that are not
401 * initialized now will be calculated) */
402 p_aout->fifo[i_fifo].b_start_frame = 0;
403 p_aout->fifo[i_fifo].b_next_frame = 0;
407 intf_ErrMsg("aout error: unknown fifo type (%i)\n", p_aout->fifo[i_fifo].i_type);
408 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
409 vlc_mutex_unlock( &p_aout->fifos_lock );
413 /* Release the fifos lock */
414 vlc_mutex_unlock( &p_aout->fifos_lock );
416 /* Return the pointer to the fifo structure */
417 intf_DbgMsg("aout debug: audio output fifo (%p) allocated\n", &p_aout->fifo[i_fifo]);
418 return( &p_aout->fifo[i_fifo] );
421 /*****************************************************************************
423 *****************************************************************************/
424 void aout_DestroyFifo( aout_fifo_t * p_fifo )
426 intf_DbgMsg("aout debug: requesting destruction of audio output fifo (%p)\n", p_fifo);
430 /* Here are the local macros */
432 #define UPDATE_INCREMENT( increment, integer ) \
433 if ( ((increment).l_remainder += (increment).l_euclidean_remainder) >= 0 )\
435 (integer) += (increment).l_euclidean_integer + 1; \
436 (increment).l_remainder -= (increment).l_euclidean_denominator; \
440 (integer) += (increment).l_euclidean_integer; \
443 /* Following functions are local */
445 /*****************************************************************************
446 * InitializeIncrement
447 *****************************************************************************/
448 static __inline__ void InitializeIncrement( aout_increment_t * p_increment, long l_numerator, long l_denominator )
450 p_increment->l_remainder = -l_denominator;
452 p_increment->l_euclidean_integer = 0;
453 while ( l_numerator >= l_denominator )
455 p_increment->l_euclidean_integer++;
456 l_numerator -= l_denominator;
459 p_increment->l_euclidean_remainder = l_numerator;
461 p_increment->l_euclidean_denominator = l_denominator;
464 /*****************************************************************************
466 *****************************************************************************/
467 static __inline__ int NextFrame( aout_thread_t * p_aout, aout_fifo_t * p_fifo, mtime_t aout_date )
469 long l_units, l_rate;
471 /* We take the lock */
472 vlc_mutex_lock( &p_fifo->data_lock );
474 /* Are we looking for a dated start frame ? */
475 if ( !p_fifo->b_start_frame )
477 while ( p_fifo->l_start_frame != p_fifo->l_end_frame )
479 if ( p_fifo->date[p_fifo->l_start_frame] != LAST_MDATE )
481 p_fifo->b_start_frame = 1;
482 p_fifo->l_next_frame = (p_fifo->l_start_frame + 1) & AOUT_FIFO_SIZE;
483 p_fifo->l_unit = p_fifo->l_start_frame * (p_fifo->l_frame_size >> (p_fifo->i_channels - 1));
486 p_fifo->l_start_frame = (p_fifo->l_start_frame + 1) & AOUT_FIFO_SIZE;
489 if ( p_fifo->l_start_frame == p_fifo->l_end_frame )
491 vlc_mutex_unlock( &p_fifo->data_lock );
496 /* We are looking for the next dated frame */
497 /* FIXME : is the output fifo full ? */
498 while ( !p_fifo->b_next_frame )
500 while ( p_fifo->l_next_frame != p_fifo->l_end_frame )
502 if ( p_fifo->date[p_fifo->l_next_frame] != LAST_MDATE )
504 p_fifo->b_next_frame = 1;
507 p_fifo->l_next_frame = (p_fifo->l_next_frame + 1) & AOUT_FIFO_SIZE;
510 while ( p_fifo->l_next_frame == p_fifo->l_end_frame )
512 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
515 vlc_mutex_unlock( &p_fifo->data_lock );
521 l_units = ((p_fifo->l_next_frame - p_fifo->l_start_frame) & AOUT_FIFO_SIZE) * (p_fifo->l_frame_size >> (p_fifo->i_channels - 1));
523 l_rate = p_fifo->l_rate + ((aout_date - p_fifo->date[p_fifo->l_start_frame]) / 256);
524 // fprintf( stderr, "aout debug: %lli (%li);\n", aout_date - p_fifo->date[p_fifo->l_start_frame], l_rate );
526 InitializeIncrement( &p_fifo->unit_increment, l_rate, p_aout->l_rate );
528 p_fifo->l_units = (((l_units - (p_fifo->l_unit -
529 (p_fifo->l_start_frame * (p_fifo->l_frame_size >> (p_fifo->i_channels - 1)))))
530 * p_aout->l_rate) / l_rate) + 1;
532 /* We release the lock before leaving */
533 vlc_mutex_unlock( &p_fifo->data_lock );
537 void aout_Thread_S8_Mono( aout_thread_t * p_aout )
541 void aout_Thread_S8_Stereo( aout_thread_t * p_aout )
545 void aout_Thread_U8_Mono( aout_thread_t * p_aout )
549 void aout_Thread_U8_Stereo( aout_thread_t * p_aout )
553 void aout_Thread_S16_Mono( aout_thread_t * p_aout )
557 void aout_Thread_S16_Stereo( aout_thread_t * p_aout )
560 long l_buffer, l_buffer_limit;
561 long l_units, l_bytes;
563 intf_DbgMsg("adec debug: running audio output thread (%p) (pid == %i)\n", p_aout, getpid());
565 /* As the s32_buffer was created with calloc(), we don't have to set this
566 * memory to zero and we can immediately jump into the thread's loop */
567 while ( !p_aout->b_die )
569 vlc_mutex_lock( &p_aout->fifos_lock );
570 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
572 switch ( p_aout->fifo[i_fifo].i_type )
574 case AOUT_EMPTY_FIFO:
577 case AOUT_INTF_MONO_FIFO:
578 if ( p_aout->fifo[i_fifo].l_units > p_aout->l_units )
581 while ( l_buffer < (p_aout->l_units << 1) ) /* p_aout->i_channels - 1 == 1 */
583 p_aout->s32_buffer[l_buffer++] +=
584 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
585 p_aout->s32_buffer[l_buffer++] +=
586 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
587 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
589 p_aout->fifo[i_fifo].l_units -= p_aout->l_units;
594 while ( l_buffer < (p_aout->fifo[i_fifo].l_units << 1) ) /* p_aout->i_channels - 1 == 1 */
596 p_aout->s32_buffer[l_buffer++] +=
597 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
598 p_aout->s32_buffer[l_buffer++] +=
599 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
600 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
602 free( p_aout->fifo[i_fifo].buffer ); /* !! */
603 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
604 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]); /* !! */
608 case AOUT_INTF_STEREO_FIFO:
609 if ( p_aout->fifo[i_fifo].l_units > p_aout->l_units )
612 while ( l_buffer < (p_aout->l_units << 1) ) /* p_aout->i_channels - 1 == 1 */
614 p_aout->s32_buffer[l_buffer++] +=
615 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
616 p_aout->s32_buffer[l_buffer++] +=
617 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
618 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
620 p_aout->fifo[i_fifo].l_units -= p_aout->l_units;
625 while ( l_buffer < (p_aout->fifo[i_fifo].l_units << 1) ) /* p_aout->i_channels - 1 == 1 */
627 p_aout->s32_buffer[l_buffer++] +=
628 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
629 p_aout->s32_buffer[l_buffer++] +=
630 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
631 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
633 free( p_aout->fifo[i_fifo].buffer ); /* !! */
634 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
635 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]); /* !! */
639 case AOUT_ADEC_MONO_FIFO:
640 if ( p_aout->fifo[i_fifo].b_die )
642 free( p_aout->fifo[i_fifo].buffer );
643 free( p_aout->fifo[i_fifo].date );
644 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
645 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
649 l_units = p_aout->l_units;
651 while ( l_units > 0 )
653 if ( !p_aout->fifo[i_fifo].b_next_frame )
655 if ( NextFrame(p_aout, &p_aout->fifo[i_fifo], p_aout->date + ((((mtime_t)(l_buffer >> 1)) * 1000000) / ((mtime_t)p_aout->l_rate))) )
661 if ( p_aout->fifo[i_fifo].l_units > l_units )
663 l_buffer_limit = p_aout->l_units << 1; /* p_aout->i_channels - 1 == 1 */
664 while ( l_buffer < l_buffer_limit )
666 p_aout->s32_buffer[l_buffer++] +=
667 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
668 p_aout->s32_buffer[l_buffer++] +=
669 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
671 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
672 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].i_channels - 1 == 0 */
673 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0)) )
675 p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].i_channels - 1 == 0 */
676 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0));
679 p_aout->fifo[i_fifo].l_units -= l_units;
684 l_buffer_limit = l_buffer + (p_aout->fifo[i_fifo].l_units << 1);
685 /* p_aout->i_channels - 1 == 1 */
686 while ( l_buffer < l_buffer_limit )
688 p_aout->s32_buffer[l_buffer++] +=
689 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
690 p_aout->s32_buffer[l_buffer++] +=
691 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
693 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
694 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].i_channels - 1 == 0 */
695 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0)) )
697 p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].i_channels - 1 == 0 */
698 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0));
701 l_units -= p_aout->fifo[i_fifo].l_units;
703 vlc_mutex_lock( &p_aout->fifo[i_fifo].data_lock );
704 p_aout->fifo[i_fifo].l_start_frame = p_aout->fifo[i_fifo].l_next_frame;
705 vlc_cond_signal( &p_aout->fifo[i_fifo].data_wait );
706 vlc_mutex_unlock( &p_aout->fifo[i_fifo].data_lock );
708 /* p_aout->fifo[i_fifo].b_start_frame = 1; */
709 p_aout->fifo[i_fifo].l_next_frame += 1;
710 p_aout->fifo[i_fifo].l_next_frame &= AOUT_FIFO_SIZE;
711 p_aout->fifo[i_fifo].b_next_frame = 0;
716 case AOUT_ADEC_STEREO_FIFO:
717 if ( p_aout->fifo[i_fifo].b_die )
719 free( p_aout->fifo[i_fifo].buffer );
720 free( p_aout->fifo[i_fifo].date );
721 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
722 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
726 l_units = p_aout->l_units;
728 while ( l_units > 0 )
730 if ( !p_aout->fifo[i_fifo].b_next_frame )
732 if ( NextFrame(p_aout, &p_aout->fifo[i_fifo], p_aout->date + ((((mtime_t)(l_buffer >> 1)) * 1000000) / ((mtime_t)p_aout->l_rate))) )
738 if ( p_aout->fifo[i_fifo].l_units > l_units )
740 l_buffer_limit = p_aout->l_units << 1; /* p_aout->i_channels - 1 == 1 */
741 while ( l_buffer < l_buffer_limit )
743 p_aout->s32_buffer[l_buffer++] +=
744 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
745 p_aout->s32_buffer[l_buffer++] +=
746 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
748 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
749 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].i_channels - 1 == 1 */
750 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1)) )
752 p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].i_channels - 1 == 1 */
753 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1));
756 p_aout->fifo[i_fifo].l_units -= l_units;
761 l_buffer_limit = l_buffer + (p_aout->fifo[i_fifo].l_units << 1);
762 /* p_aout->i_channels - 1 == 1 */
763 while ( l_buffer < l_buffer_limit )
765 p_aout->s32_buffer[l_buffer++] +=
766 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
767 p_aout->s32_buffer[l_buffer++] +=
768 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
770 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
771 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].i_channels - 1 == 1 */
772 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1)) )
774 p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].i_channels - 1 == 1 */
775 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1));
778 l_units -= p_aout->fifo[i_fifo].l_units;
780 vlc_mutex_lock( &p_aout->fifo[i_fifo].data_lock );
781 p_aout->fifo[i_fifo].l_start_frame = p_aout->fifo[i_fifo].l_next_frame;
782 vlc_cond_signal( &p_aout->fifo[i_fifo].data_wait );
783 vlc_mutex_unlock( &p_aout->fifo[i_fifo].data_lock );
785 /* p_aout->fifo[i_fifo].b_start_frame = 1; */
786 p_aout->fifo[i_fifo].l_next_frame += 1;
787 p_aout->fifo[i_fifo].l_next_frame &= AOUT_FIFO_SIZE;
788 p_aout->fifo[i_fifo].b_next_frame = 0;
794 intf_DbgMsg("aout debug: unknown fifo type (%i)\n", p_aout->fifo[i_fifo].i_type);
798 vlc_mutex_unlock( &p_aout->fifos_lock );
800 l_buffer_limit = p_aout->l_units << 1; /* p_aout->i_channels - 1 == 1 */
802 for ( l_buffer = 0; l_buffer < l_buffer_limit; l_buffer++ )
804 ((s16 *)p_aout->buffer)[l_buffer] = (s16)( p_aout->s32_buffer[l_buffer] / AOUT_MAX_FIFOS );
805 p_aout->s32_buffer[l_buffer] = 0;
808 l_bytes = p_aout->p_sys_getbufinfo( p_aout );
809 p_aout->date = mdate() + ((((mtime_t)(l_bytes / 4)) * 1000000) / ((mtime_t)p_aout->l_rate)); /* sizeof(s16) << (p_aout->i_channels - 1) == 4 */
810 p_aout->p_sys_playsamples( p_aout, (byte_t *)p_aout->buffer, l_buffer_limit * sizeof(s16) );
811 if ( l_bytes > (l_buffer_limit * sizeof(s16)) )
813 msleep( p_aout->l_msleep );
817 vlc_mutex_lock( &p_aout->fifos_lock );
818 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
820 switch ( p_aout->fifo[i_fifo].i_type )
822 case AOUT_EMPTY_FIFO:
825 case AOUT_INTF_MONO_FIFO:
826 case AOUT_INTF_STEREO_FIFO:
827 free( p_aout->fifo[i_fifo].buffer ); /* !! */
828 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
829 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
832 case AOUT_ADEC_MONO_FIFO:
833 case AOUT_ADEC_STEREO_FIFO:
834 free( p_aout->fifo[i_fifo].buffer );
835 free( p_aout->fifo[i_fifo].date );
836 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
837 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
844 vlc_mutex_unlock( &p_aout->fifos_lock );
847 void aout_Thread_U16_Mono( aout_thread_t * p_aout )
851 void aout_Thread_U16_Stereo( aout_thread_t * p_aout )