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 <sys/soundcard.h>
24 #include <stdio.h> /* "intf_msg.h" */
25 #include <stdlib.h> /* calloc(), malloc(), free() */
29 #include "mtime.h" /* mtime_t, mdate(), msleep() */
30 #include "vlc_thread.h"
32 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
34 #include "audio_output.h"
35 #include "audio_sys.h"
38 /*****************************************************************************
40 *****************************************************************************/
42 static int aout_SpawnThread( aout_thread_t * p_aout );
44 /* Creating as much aout_Thread functions as configurations is one solution,
45 * examining the different cases in the Thread loop of an unique function is
46 * another. I chose the first solution. */
47 void aout_Thread_S8_Mono ( aout_thread_t * p_aout );
48 void aout_Thread_U8_Mono ( aout_thread_t * p_aout );
49 void aout_Thread_S16_Mono ( aout_thread_t * p_aout );
50 void aout_Thread_U16_Mono ( aout_thread_t * p_aout );
51 void aout_Thread_S8_Stereo ( aout_thread_t * p_aout );
52 void aout_Thread_U8_Stereo ( aout_thread_t * p_aout );
53 void aout_Thread_S16_Stereo ( aout_thread_t * p_aout );
54 void aout_Thread_U16_Stereo ( aout_thread_t * p_aout );
56 static __inline__ void InitializeIncrement( aout_increment_t * p_increment, long l_numerator, long l_denominator );
57 static __inline__ int NextFrame( aout_thread_t * p_aout, aout_fifo_t * p_fifo, mtime_t aout_date );
59 /*****************************************************************************
60 * aout_CreateThread: initialize audio thread
61 *****************************************************************************/
62 aout_thread_t *aout_CreateThread( int *pi_status )
64 aout_thread_t * p_aout; /* thread descriptor */
66 // int i_status; /* thread status */
68 /* Allocate descriptor */
69 p_aout = (aout_thread_t *) malloc( sizeof(aout_thread_t) );
75 /* initialize method-dependent functions */
76 psz_method = main_GetPszVariable( AOUT_METHOD_VAR, AOUT_DEFAULT_METHOD );
78 if( !strcmp(psz_method, "dummy") )
80 p_aout->p_sys_open = aout_DummySysOpen;
81 p_aout->p_sys_reset = aout_DummySysReset;
82 p_aout->p_sys_setformat = aout_DummySysSetFormat;
83 p_aout->p_sys_setchannels = aout_DummySysSetChannels;
84 p_aout->p_sys_setrate = aout_DummySysSetRate;
85 p_aout->p_sys_getbufinfo = aout_DummySysGetBufInfo;
86 p_aout->p_sys_playsamples = aout_DummySysPlaySamples;
87 p_aout->p_sys_close = aout_DummySysClose;
90 else if( !strcmp(psz_method, "dsp") )
92 p_aout->p_sys_open = aout_DspSysOpen;
93 p_aout->p_sys_reset = aout_DspSysReset;
94 p_aout->p_sys_setformat = aout_DspSysSetFormat;
95 p_aout->p_sys_setchannels = aout_DspSysSetChannels;
96 p_aout->p_sys_setrate = aout_DspSysSetRate;
97 p_aout->p_sys_getbufinfo = aout_DspSysGetBufInfo;
98 p_aout->p_sys_playsamples = aout_DspSysPlaySamples;
99 p_aout->p_sys_close = aout_DspSysClose;
104 intf_ErrMsg( "error: requested audio output method not available\n" );
109 //???? kludge to initialize some audio parameters - place this section somewhere
111 p_aout->sys.i_format = AOUT_DEFAULT_FORMAT;
112 p_aout->sys.psz_device = main_GetPszVariable( AOUT_DSP_VAR, AOUT_DSP_DEFAULT );
113 p_aout->sys.b_stereo = main_GetIntVariable( AOUT_STEREO_VAR, AOUT_STEREO_DEFAULT );
114 p_aout->sys.l_rate = main_GetIntVariable( AOUT_RATE_VAR, AOUT_RATE_DEFAULT );
115 // ???? end of kludge
120 if ( p_aout->p_sys_open( &p_aout->sys ) )
125 if ( p_aout->p_sys_reset( &p_aout->sys ) )
127 p_aout->p_sys_close( &p_aout->sys );
131 if ( p_aout->p_sys_setformat( &p_aout->sys ) )
133 p_aout->p_sys_close( &p_aout->sys );
137 if ( p_aout->p_sys_setchannels( &p_aout->sys ) )
139 p_aout->p_sys_close( &p_aout->sys );
143 if ( p_aout->p_sys_setrate( &p_aout->sys ) )
145 p_aout->p_sys_close( &p_aout->sys );
149 intf_DbgMsg("aout debug: audio device (%s) opened (format=%i, stereo=%i, rate=%li)\n",
150 p_aout->sys.psz_device,
151 p_aout->sys.i_format,
152 p_aout->sys.b_stereo, p_aout->sys.l_rate);
154 //?? maybe it would be cleaner to change SpawnThread prototype
155 //?? see vout to handle status correctly - however, it is not critical since
156 //?? this thread is only called in main and all calls are blocking
157 if( aout_SpawnThread( p_aout ) )
159 p_aout->p_sys_close( &p_aout->sys );
167 /*****************************************************************************
169 *****************************************************************************/
170 static int aout_SpawnThread( aout_thread_t * p_aout )
174 void * aout_thread = NULL;
176 intf_DbgMsg("aout debug: spawning audio output thread (%p)\n", p_aout);
178 /* We want the audio output thread to live */
181 /* Initialize the fifos lock */
182 vlc_mutex_init( &p_aout->fifos_lock );
183 /* Initialize audio fifos : set all fifos as empty and initialize locks */
184 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
186 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
187 vlc_mutex_init( &p_aout->fifo[i_fifo].data_lock );
188 vlc_cond_init( &p_aout->fifo[i_fifo].data_wait );
191 /* Compute the size (in audio units) of the audio output buffer. Although
192 * AOUT_BUFFER_DURATION is given in microseconds, the output rate is given
193 * in Hz, that's why we need to divide by 10^6 microseconds (1 second) */
194 p_aout->l_units = (long)( ((s64)p_aout->sys.l_rate * AOUT_BUFFER_DURATION) / 1000000 );
195 p_aout->l_msleep = (long)( ((s64)p_aout->l_units * 1000000) / (s64)p_aout->sys.l_rate );
197 /* Make aout_thread point to the right thread function, and compute the
198 * byte size of the audio output buffer */
199 switch ( p_aout->sys.b_stereo )
201 /* Audio output is mono */
203 switch ( p_aout->sys.i_format )
206 l_bytes = 1 * sizeof(u8) * p_aout->l_units;
207 aout_thread = (void *)aout_Thread_U8_Mono;
211 l_bytes = 1 * sizeof(s8) * p_aout->l_units;
212 aout_thread = (void *)aout_Thread_S8_Mono;
217 l_bytes = 1 * sizeof(u16) * p_aout->l_units;
218 aout_thread = (void *)aout_Thread_U16_Mono;
223 l_bytes = 1 * sizeof(s16) * p_aout->l_units;
224 aout_thread = (void *)aout_Thread_S16_Mono;
228 intf_ErrMsg("aout error: unknown audio output format (%i)\n",
229 p_aout->sys.i_format);
234 /* Audio output is stereo */
236 switch ( p_aout->sys.i_format )
239 l_bytes = 2 * sizeof(u8) * p_aout->l_units;
240 aout_thread = (void *)aout_Thread_U8_Stereo;
244 l_bytes = 2 * sizeof(s8) * p_aout->l_units;
245 aout_thread = (void *)aout_Thread_S8_Stereo;
250 l_bytes = 2 * sizeof(u16) * p_aout->l_units;
251 aout_thread = (void *)aout_Thread_U16_Stereo;
256 l_bytes = 2 * sizeof(s16) * p_aout->l_units;
257 aout_thread = (void *)aout_Thread_S16_Stereo;
261 intf_ErrMsg("aout error: unknown audio output format (%i)\n",
262 p_aout->sys.i_format);
268 intf_ErrMsg("aout error: unknown number of audio channels (%i)\n",
269 p_aout->sys.b_stereo + 1);
273 /* Allocate the memory needed by the audio output buffers, and set to zero
274 * the s32 buffer's memory */
275 if ( (p_aout->buffer = malloc(l_bytes)) == NULL )
277 intf_ErrMsg("aout error: not enough memory to create the output buffer\n");
280 if ( (p_aout->s32_buffer = (s32 *)calloc(p_aout->l_units, sizeof(s32) << p_aout->sys.b_stereo)) == NULL )
282 intf_ErrMsg("aout error: not enough memory to create the s32 output buffer\n");
283 free( p_aout->buffer );
287 /* Before launching the thread, we try to predict the date of the first
288 * audio unit in the first output buffer */
289 p_aout->date = mdate();
291 /* Launch the thread */
292 if ( vlc_thread_create( &p_aout->thread_id, "audio output", (vlc_thread_func_t)aout_thread, p_aout ) )
294 intf_ErrMsg("aout error: can't spawn audio output thread (%p)\n", p_aout);
295 free( p_aout->buffer );
296 free( p_aout->s32_buffer );
300 intf_DbgMsg("aout debug: audio output thread (%p) spawned\n", p_aout);
304 /*****************************************************************************
306 *****************************************************************************/
307 void aout_DestroyThread( aout_thread_t * p_aout, int *pi_status )
309 //???? pi_status is not handled correctly: check vout how to do!
311 intf_DbgMsg("aout debug: requesting termination of audio output thread (%p)\n", p_aout);
313 /* Ask thread to kill itself and wait until it's done */
315 vlc_thread_join( p_aout->thread_id ); // only if pi_status is NULL
317 /* Free the allocated memory */
318 free( p_aout->buffer );
319 free( p_aout->s32_buffer );
321 /* Free the structure */
322 p_aout->p_sys_close( &p_aout->sys );
323 intf_DbgMsg("aout debug: audio device (%s) closed\n", p_aout->sys.psz_device);
327 /*****************************************************************************
329 *****************************************************************************/
330 aout_fifo_t * aout_CreateFifo( aout_thread_t * p_aout, aout_fifo_t * p_fifo )
334 /* Take the fifos lock */
335 vlc_mutex_lock( &p_aout->fifos_lock );
337 /* Looking for a free fifo structure */
338 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
340 if ( p_aout->fifo[i_fifo].i_type == AOUT_EMPTY_FIFO)
345 if ( i_fifo == AOUT_MAX_FIFOS )
347 intf_ErrMsg("aout error: no empty fifo available\n");
348 vlc_mutex_unlock( &p_aout->fifos_lock );
352 /* Initialize the new fifo structure */
353 switch ( p_aout->fifo[i_fifo].i_type = p_fifo->i_type )
355 case AOUT_INTF_MONO_FIFO:
356 case AOUT_INTF_STEREO_FIFO:
357 p_aout->fifo[i_fifo].b_die = 0;
359 p_aout->fifo[i_fifo].b_stereo = p_fifo->b_stereo;
360 p_aout->fifo[i_fifo].l_rate = p_fifo->l_rate;
362 p_aout->fifo[i_fifo].buffer = p_fifo->buffer;
364 p_aout->fifo[i_fifo].l_unit = 0;
365 InitializeIncrement( &p_aout->fifo[i_fifo].unit_increment, p_fifo->l_rate, p_aout->sys.l_rate );
366 p_aout->fifo[i_fifo].l_units = p_fifo->l_units;
369 case AOUT_ADEC_MONO_FIFO:
370 case AOUT_ADEC_STEREO_FIFO:
371 p_aout->fifo[i_fifo].b_die = 0;
373 p_aout->fifo[i_fifo].b_stereo = p_fifo->b_stereo;
374 p_aout->fifo[i_fifo].l_rate = p_fifo->l_rate;
376 p_aout->fifo[i_fifo].l_frame_size = p_fifo->l_frame_size;
377 /* Allocate the memory needed to store the audio frames. As the
378 * fifo is a rotative fifo, we must be able to find out whether the
379 * fifo is full or empty, that's why we must in fact allocate memory
380 * for (AOUT_FIFO_SIZE+1) audio frames. */
381 if ( (p_aout->fifo[i_fifo].buffer = malloc( sizeof(s16)*(AOUT_FIFO_SIZE+1)*p_fifo->l_frame_size )) == NULL )
383 intf_ErrMsg("aout error: not enough memory to create the frames buffer\n");
384 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
385 vlc_mutex_unlock( &p_aout->fifos_lock );
389 /* Allocate the memory needed to store the dates of the frames */
390 if ( (p_aout->fifo[i_fifo].date = (mtime_t *)malloc( sizeof(mtime_t)*(AOUT_FIFO_SIZE+1) )) == NULL )
392 intf_ErrMsg("aout error: not enough memory to create the dates buffer\n");
393 free( p_aout->fifo[i_fifo].buffer );
394 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
395 vlc_mutex_unlock( &p_aout->fifos_lock );
399 /* Set the fifo's buffer as empty (the first frame that is to be
400 * played is also the first frame that is not to be played) */
401 p_aout->fifo[i_fifo].l_start_frame = 0;
402 /* p_aout->fifo[i_fifo].l_next_frame = 0; */
403 p_aout->fifo[i_fifo].l_end_frame = 0;
405 /* Waiting for the audio decoder to compute enough frames to work
406 * out the fifo's current rate (as soon as the decoder has decoded
407 * enough frames, the members of the fifo structure that are not
408 * initialized now will be calculated) */
409 p_aout->fifo[i_fifo].b_start_frame = 0;
410 p_aout->fifo[i_fifo].b_next_frame = 0;
414 intf_ErrMsg("aout error: unknown fifo type (%i)\n", p_aout->fifo[i_fifo].i_type);
415 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
416 vlc_mutex_unlock( &p_aout->fifos_lock );
420 /* Release the fifos lock */
421 vlc_mutex_unlock( &p_aout->fifos_lock );
423 /* Return the pointer to the fifo structure */
424 intf_DbgMsg("aout debug: audio output fifo (%p) allocated\n", &p_aout->fifo[i_fifo]);
425 return( &p_aout->fifo[i_fifo] );
428 /*****************************************************************************
430 *****************************************************************************/
431 void aout_DestroyFifo( aout_fifo_t * p_fifo )
433 intf_DbgMsg("aout debug: requesting destruction of audio output fifo (%p)\n", p_fifo);
437 /* Here are the local macros */
439 #define UPDATE_INCREMENT( increment, integer ) \
440 if ( ((increment).l_remainder += (increment).l_euclidean_remainder) >= 0 )\
442 (integer) += (increment).l_euclidean_integer + 1; \
443 (increment).l_remainder -= (increment).l_euclidean_denominator; \
447 (integer) += (increment).l_euclidean_integer; \
450 /* Following functions are local */
452 /*****************************************************************************
453 * InitializeIncrement
454 *****************************************************************************/
455 static __inline__ void InitializeIncrement( aout_increment_t * p_increment, long l_numerator, long l_denominator )
457 p_increment->l_remainder = -l_denominator;
459 p_increment->l_euclidean_integer = 0;
460 while ( l_numerator >= l_denominator )
462 p_increment->l_euclidean_integer++;
463 l_numerator -= l_denominator;
466 p_increment->l_euclidean_remainder = l_numerator;
468 p_increment->l_euclidean_denominator = l_denominator;
471 /*****************************************************************************
473 *****************************************************************************/
474 static __inline__ int NextFrame( aout_thread_t * p_aout, aout_fifo_t * p_fifo, mtime_t aout_date )
476 long l_units, l_rate;
478 /* We take the lock */
479 vlc_mutex_lock( &p_fifo->data_lock );
481 /* Are we looking for a dated start frame ? */
482 if ( !p_fifo->b_start_frame )
484 while ( p_fifo->l_start_frame != p_fifo->l_end_frame )
486 if ( p_fifo->date[p_fifo->l_start_frame] != LAST_MDATE )
488 p_fifo->b_start_frame = 1;
489 p_fifo->l_next_frame = (p_fifo->l_start_frame + 1) & AOUT_FIFO_SIZE;
490 p_fifo->l_unit = p_fifo->l_start_frame * (p_fifo->l_frame_size >> p_fifo->b_stereo);
493 p_fifo->l_start_frame = (p_fifo->l_start_frame + 1) & AOUT_FIFO_SIZE;
496 if ( p_fifo->l_start_frame == p_fifo->l_end_frame )
498 vlc_mutex_unlock( &p_fifo->data_lock );
503 /* We are looking for the next dated frame */
504 /* FIXME : is the output fifo full ? */
505 while ( !p_fifo->b_next_frame )
507 while ( p_fifo->l_next_frame != p_fifo->l_end_frame )
509 if ( p_fifo->date[p_fifo->l_next_frame] != LAST_MDATE )
511 p_fifo->b_next_frame = 1;
514 p_fifo->l_next_frame = (p_fifo->l_next_frame + 1) & AOUT_FIFO_SIZE;
517 while ( p_fifo->l_next_frame == p_fifo->l_end_frame )
519 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
522 vlc_mutex_unlock( &p_fifo->data_lock );
528 l_units = ((p_fifo->l_next_frame - p_fifo->l_start_frame) & AOUT_FIFO_SIZE) * (p_fifo->l_frame_size >> p_fifo->b_stereo);
530 l_rate = p_fifo->l_rate + ((aout_date - p_fifo->date[p_fifo->l_start_frame]) / 256);
531 // fprintf( stderr, "aout debug: %lli (%li);\n", aout_date - p_fifo->date[p_fifo->l_start_frame], l_rate );
533 InitializeIncrement( &p_fifo->unit_increment, l_rate, p_aout->sys.l_rate );
535 p_fifo->l_units = (((l_units - (p_fifo->l_unit -
536 (p_fifo->l_start_frame * (p_fifo->l_frame_size >> p_fifo->b_stereo))))
537 * p_aout->sys.l_rate) / l_rate) + 1;
539 /* We release the lock before leaving */
540 vlc_mutex_unlock( &p_fifo->data_lock );
544 void aout_Thread_S8_Mono( aout_thread_t * p_aout )
548 void aout_Thread_S8_Stereo( aout_thread_t * p_aout )
552 void aout_Thread_U8_Mono( aout_thread_t * p_aout )
556 void aout_Thread_U8_Stereo( aout_thread_t * p_aout )
560 void aout_Thread_S16_Mono( aout_thread_t * p_aout )
564 void aout_Thread_S16_Stereo( aout_thread_t * p_aout )
567 long l_buffer, l_buffer_limit;
568 long l_units, l_bytes;
570 intf_DbgMsg("adec debug: running audio output thread (%p) (pid == %i)\n", p_aout, getpid());
572 /* As the s32_buffer was created with calloc(), we don't have to set this
573 * memory to zero and we can immediately jump into the thread's loop */
574 while ( !p_aout->b_die )
576 vlc_mutex_lock( &p_aout->fifos_lock );
577 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
579 switch ( p_aout->fifo[i_fifo].i_type )
581 case AOUT_EMPTY_FIFO:
584 case AOUT_INTF_MONO_FIFO:
585 if ( p_aout->fifo[i_fifo].l_units > p_aout->l_units )
588 while ( l_buffer < (p_aout->l_units << 1) ) /* p_aout->sys.b_stereo == 1 */
590 p_aout->s32_buffer[l_buffer++] +=
591 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
592 p_aout->s32_buffer[l_buffer++] +=
593 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
594 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
596 p_aout->fifo[i_fifo].l_units -= p_aout->l_units;
601 while ( l_buffer < (p_aout->fifo[i_fifo].l_units << 1) ) /* p_aout->sys.b_stereo == 1 */
603 p_aout->s32_buffer[l_buffer++] +=
604 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
605 p_aout->s32_buffer[l_buffer++] +=
606 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
607 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
609 free( p_aout->fifo[i_fifo].buffer ); /* !! */
610 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
611 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]); /* !! */
615 case AOUT_INTF_STEREO_FIFO:
616 if ( p_aout->fifo[i_fifo].l_units > p_aout->l_units )
619 while ( l_buffer < (p_aout->l_units << 1) ) /* p_aout->sys.b_stereo == 1 */
621 p_aout->s32_buffer[l_buffer++] +=
622 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
623 p_aout->s32_buffer[l_buffer++] +=
624 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
625 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
627 p_aout->fifo[i_fifo].l_units -= p_aout->l_units;
632 while ( l_buffer < (p_aout->fifo[i_fifo].l_units << 1) ) /* p_aout->sys.b_stereo */
634 p_aout->s32_buffer[l_buffer++] +=
635 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
636 p_aout->s32_buffer[l_buffer++] +=
637 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
638 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
640 free( p_aout->fifo[i_fifo].buffer ); /* !! */
641 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
642 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]); /* !! */
646 case AOUT_ADEC_MONO_FIFO:
647 if ( p_aout->fifo[i_fifo].b_die )
649 free( p_aout->fifo[i_fifo].buffer );
650 free( p_aout->fifo[i_fifo].date );
651 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
652 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
656 l_units = p_aout->l_units;
658 while ( l_units > 0 )
660 if ( !p_aout->fifo[i_fifo].b_next_frame )
662 if ( NextFrame(p_aout, &p_aout->fifo[i_fifo], p_aout->date + ((((mtime_t)(l_buffer >> 1)) * 1000000) / ((mtime_t)p_aout->sys.l_rate))) )
668 if ( p_aout->fifo[i_fifo].l_units > l_units )
670 l_buffer_limit = p_aout->l_units << 1; /* p_aout->sys.b_stereo == 1 */
671 while ( l_buffer < l_buffer_limit )
673 p_aout->s32_buffer[l_buffer++] +=
674 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
675 p_aout->s32_buffer[l_buffer++] +=
676 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
678 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
679 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 0 */
680 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0)) )
682 p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 0 */
683 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0));
686 p_aout->fifo[i_fifo].l_units -= l_units;
691 l_buffer_limit = l_buffer + (p_aout->fifo[i_fifo].l_units << 1);
692 /* p_aout->sys.b_stereo == 1 */
693 while ( l_buffer < l_buffer_limit )
695 p_aout->s32_buffer[l_buffer++] +=
696 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
697 p_aout->s32_buffer[l_buffer++] +=
698 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
700 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
701 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 0 */
702 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0)) )
704 p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 0 */
705 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0));
708 l_units -= p_aout->fifo[i_fifo].l_units;
710 vlc_mutex_lock( &p_aout->fifo[i_fifo].data_lock );
711 p_aout->fifo[i_fifo].l_start_frame = p_aout->fifo[i_fifo].l_next_frame;
712 vlc_cond_signal( &p_aout->fifo[i_fifo].data_wait );
713 vlc_mutex_unlock( &p_aout->fifo[i_fifo].data_lock );
715 /* p_aout->fifo[i_fifo].b_start_frame = 1; */
716 p_aout->fifo[i_fifo].l_next_frame += 1;
717 p_aout->fifo[i_fifo].l_next_frame &= AOUT_FIFO_SIZE;
718 p_aout->fifo[i_fifo].b_next_frame = 0;
723 case AOUT_ADEC_STEREO_FIFO:
724 if ( p_aout->fifo[i_fifo].b_die )
726 free( p_aout->fifo[i_fifo].buffer );
727 free( p_aout->fifo[i_fifo].date );
728 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
729 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
733 l_units = p_aout->l_units;
735 while ( l_units > 0 )
737 if ( !p_aout->fifo[i_fifo].b_next_frame )
739 if ( NextFrame(p_aout, &p_aout->fifo[i_fifo], p_aout->date + ((((mtime_t)(l_buffer >> 1)) * 1000000) / ((mtime_t)p_aout->sys.l_rate))) )
745 if ( p_aout->fifo[i_fifo].l_units > l_units )
747 l_buffer_limit = p_aout->l_units << 1; /* p_aout->sys.b_stereo == 1 */
748 while ( l_buffer < l_buffer_limit )
750 p_aout->s32_buffer[l_buffer++] +=
751 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
752 p_aout->s32_buffer[l_buffer++] +=
753 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
755 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
756 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 1 */
757 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1)) )
759 p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 1 */
760 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1));
763 p_aout->fifo[i_fifo].l_units -= l_units;
768 l_buffer_limit = l_buffer + (p_aout->fifo[i_fifo].l_units << 1);
769 /* p_aout->sys.b_stereo == 1 */
770 while ( l_buffer < l_buffer_limit )
772 p_aout->s32_buffer[l_buffer++] +=
773 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
774 p_aout->s32_buffer[l_buffer++] +=
775 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
777 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
778 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 1 */
779 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1)) )
781 p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 1 */
782 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1));
785 l_units -= p_aout->fifo[i_fifo].l_units;
787 vlc_mutex_lock( &p_aout->fifo[i_fifo].data_lock );
788 p_aout->fifo[i_fifo].l_start_frame = p_aout->fifo[i_fifo].l_next_frame;
789 vlc_cond_signal( &p_aout->fifo[i_fifo].data_wait );
790 vlc_mutex_unlock( &p_aout->fifo[i_fifo].data_lock );
792 /* p_aout->fifo[i_fifo].b_start_frame = 1; */
793 p_aout->fifo[i_fifo].l_next_frame += 1;
794 p_aout->fifo[i_fifo].l_next_frame &= AOUT_FIFO_SIZE;
795 p_aout->fifo[i_fifo].b_next_frame = 0;
801 intf_DbgMsg("aout debug: unknown fifo type (%i)\n", p_aout->fifo[i_fifo].i_type);
805 vlc_mutex_unlock( &p_aout->fifos_lock );
807 l_buffer_limit = p_aout->l_units << 1; /* p_aout->sys.b_stereo == 1 */
809 for ( l_buffer = 0; l_buffer < l_buffer_limit; l_buffer++ )
811 ((s16 *)p_aout->buffer)[l_buffer] = (s16)( p_aout->s32_buffer[l_buffer] / AOUT_MAX_FIFOS );
812 p_aout->s32_buffer[l_buffer] = 0;
815 l_bytes = p_aout->p_sys_getbufinfo( &p_aout->sys );
816 p_aout->date = mdate() + ((((mtime_t)(l_bytes / 4)) * 1000000) / ((mtime_t)p_aout->sys.l_rate)); /* sizeof(s16) << p_aout->sys.b_stereo == 4 */
817 p_aout->p_sys_playsamples( &p_aout->sys, (byte_t *)p_aout->buffer, l_buffer_limit * sizeof(s16) );
818 if ( l_bytes > (l_buffer_limit * sizeof(s16)) )
820 msleep( p_aout->l_msleep );
824 vlc_mutex_lock( &p_aout->fifos_lock );
825 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
827 switch ( p_aout->fifo[i_fifo].i_type )
829 case AOUT_EMPTY_FIFO:
832 case AOUT_INTF_MONO_FIFO:
833 case AOUT_INTF_STEREO_FIFO:
834 free( p_aout->fifo[i_fifo].buffer ); /* !! */
835 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
836 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
839 case AOUT_ADEC_MONO_FIFO:
840 case AOUT_ADEC_STEREO_FIFO:
841 free( p_aout->fifo[i_fifo].buffer );
842 free( p_aout->fifo[i_fifo].date );
843 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
844 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
851 vlc_mutex_unlock( &p_aout->fifos_lock );
854 void aout_Thread_U16_Mono( aout_thread_t * p_aout )
858 void aout_Thread_U16_Stereo( aout_thread_t * p_aout )